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.
One way of doing this:
// 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
:)
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.
PresetNames_read = ((BankNr + PstNr) + (BankNr * 8)) * 10 + 6366; //
Then I read the characters stored in the eeprom:
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:
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:
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 = "";
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.
:)
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.
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:
// read a bit from variable "myByte"
int x = bitRead(myByte, 3);
// Now write it back
myByte = bitWrite(myByte, 3, x);
Simple, and user friendly.