# Text Input How To?

**URL:** <https://discourse.processing.org/t/text-input-how-to/32031>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 31, 2021, 4:36pm UTC](https://discourse.processing.org/t/text-input-how-to/32031 "2021-08-31T16:36:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [August 31, 2021, 4:36pm UTC](https://discourse.processing.org/t/text-input-how-to/32031/1 "2021-08-31T16:36:07Z")

</div>

Hi guys, I am stuck with something here.  
I am making an Eeprom editor for my for my DIY guitar effect switcher and I have implemented an eeprom emulation for it. That is working great. The emulated eeprom bytes are stored in a text file, each line represents a byte.  
Each Preset on the device has a “Preset Name” using 10 characters. They are stored in 10 consecutive bytes in the eeprom, or in this case the text file which is loaded to a variable in processing.  
What I would like to do is to read the eeprom values into a text field, edit them and then save them back to the variable.  
I have looked at several examples, but didn’t find one that does something similar ☹  
Could you please set me on the right path? I am a beginner. Thank you.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 31, 2021, 10:20pm UTC](https://discourse.processing.org/t/text-input-how-to/32031/2 "2021-08-31T22:20:34Z")

</div>

> [@jhsa](#):
>
> What I would like to do is to read the eeprom values into a text field, edit them and then save them back to the variable.

One way of doing this:

```auto
// Bit manipulation
// v1.0.0
// GLV 2021-08-31

byte num = byte(0xF0);

boolean b;

void setup() 
  {
  size(500, 100);
  textSize(48);
  textAlign(CENTER, CENTER);
  
  println(binary(num, 8));
  }

void draw() 
  {
  background(0);
  
  int xt = mouseX;
  
  for (int bit = 0; bit<8; bit++) 
    {
    int x = 350-50*bit+80;   
    int y = 50-10;
    
    if (xt > x-10 && xt < x+10)
      {
      fill(0, 255, 0);  
      if(b)
        { 
        num ^= 1 << bit; // Updates num
        b = false;
        println(binary(num, 8));
        }
      }
    else
      {
      fill(255);
      }
      
    text((num >>> bit) & 1, x, y);
    }
  }  
  
void mouseClicked()
  {
  b = true;
  }

```

Bit manipulation comes easy to me because of my background.  
This may be a challenge for others.

The ^ is a bitwise exclusive OR

If you are using Processing 3 versions change `0b00000001` to `1`

[Summary of Operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html)

`:)`

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 1, 2021, 1:15am UTC](https://discourse.processing.org/t/text-input-how-to/32031/3 "2021-09-01T01:15:04Z")

</div>

As far as i understand you are reading the bits?  
I think I might not have explained well what I am doing.  
each preset name has 10 characters. Every character from the name is stored in one byte of the eeprom. So, 10 consecutive bytes.  
Anyway, I think I managed to do something in a very unorthodox way.  
This is how I read my emulated eeprom.

```auto
PresetNames_read = ((BankNr + PstNr) + (BankNr * 8)) * 10 + 6366; //

```

Then I read the characters stored in the eeprom:

```auto
PresetName = ""; // This must stay here.
for (int address = PresetNames_read; address < PresetNames_read + 10; address ++)
  {
     read_value = read_data[address];
PresetName = PresetName + char(read_value);

  }
  
println(PresetName);

```

Then:

```auto
text(PresetName, 546, 16);

```

To Edit and save the preset names to the Eeprom I added a text field and detect the controller event, then:

```auto
Letters = theControlEvent.getStringValue();

    if(Letters.length() < 11) {
    //println(PresetName);
    
    for (int address = PresetNames_read; address < PresetNames_read + 10; address ++) 
  {

    EepromWrite((p_address + (2 * address)), 32 );

    }
    int i = 0;

    //for (int address = PresetNames_read; address < PresetNames_read + 10; address ++) 
    for (int address = PresetNames_read; address < PresetNames_read + Letters.length(); address ++) 
  {
    char Char = Letters.charAt(i);
   // read_data[address] = Letters.charAt(i);
    EepromWrite((p_address + (2 * address)), Char );
    i++;
    }
     PresetName = Letters;
      
   cp5.get(Textfield.class,"Input_PresetName").clear();
    }
    else if(Letters.length() > 10) {
      println("Max 10 Characters");
      delay(1000);
      cp5.get(Textfield.class,"Input_PresetName").clear();
    }
    Letters = "";

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 1, 2021, 9:32am UTC](https://discourse.processing.org/t/text-input-how-to/32031/4 "2021-09-01T09:32:36Z")

</div>

> [@jhsa](#):
>
> As far as i understand you are reading the bits?

I extracted the bits from a byte.  
You can toggle a bit with a mouse click and it is saved back to the byte.

This is a very simple way to edit bits.

You can wrap this any way you like.

`:)`

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 1, 2021, 11:09am UTC](https://discourse.processing.org/t/text-input-how-to/32031/5 "2021-09-01T11:09:26Z")

</div>

Yeah, but in this case I needed to deal with the characters of a byte, not bits. 🙂 Each character takes a byte of the eeprom.

But talking about bits, and if someone come from Arduino like me, I did find a very easy way of editing bits. 🙂 Have a look: I think it couldn’t be easier if we just make a couple of little functions with a couple of lines. 🙂

```auto
int bitRead(int b, int bitPos)
    {
      int x = b & (1 << bitPos);
      return x == 0 ? 0 : 1;
    }
    
    int bitWrite(int b, int bitPos, int bitVal)
    {
      int x = 0;
      if (bitVal == 0) {
       x = b & ~(1 << bitPos);           
      }
      else if(bitVal == 1) {    
       x = b | (1 << bitPos);       
    }
    return x;
    }

```

Now if we have to read or write the bits of a variable that represents a byte, for example:

```auto
// read a bit from variable "myByte"

int x = bitRead(myByte, 3);

// Now write it back

myByte = bitWrite(myByte, 3, x);

```

Simple, and user friendly. 🙂
