I am trying to send a 50x50 bitmap bytearray from my python client to arduino where it should be displayed on a screen.
I need to receive 350 bytes to have the data to show the bitmap but as soon as i initialise the array with> 255 elements it does weird things (i guess because of the memory):
When initialising the array with size 254 it does the expected:
Transmission of 350 completed in 2.805466s (arduino) Received: 253 elements
with size 350 I only get
"Received: 93 elements"
How can i split the bytes i receive with the available memory and then build the bitmap accordingly or what is a better way to do it? I would like to have it transmitted via serial rather than building the qrcode in arduino (as i couldn't get the libraries required working in my setup).
My python client does basically this:
for element in qrCodeBytes:
countElementsSent+=1
ser.write(element)
print("Sending " + str(element))
ser.write("$".encode()) #stop char
My arduino code is based on the popular Serial input basics guide https://forum.arduino.cc/index.php?topic=288234.0
const byte numChars = 254;
byte receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(114000);
Serial.setTimeout(10000);
Serial.println("<Arduino is ready>");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay();
display.display();
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '$';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '$'; // terminate the string
Serial.println("Received: " + String(ndx) + " elements");
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println("This just in ... ");
display.clearDisplay();
drawBitMap(receivedChars,50,50,0);
display.display();
newData = false;
}
}
void drawBitMap(unsigned char *img, int width, int height, int position){
if (position ==0){
display.drawBitmap(128/2-(width/2), 64/2-(height/2), img, width, height, 1);
} else if (position == 1){
display.drawBitmap(128-62,1, img, width, height, 1);
} else if (position == 2){
display.drawBitmap(0,5, img, width, height, 1);
}
}
1 Answer 1
You use type byte
for the variable ndx
. Type byte is 8 bits, so the max value is 255. The variable ndx
is incremented and after 255 it continues counting from 0.
Explore related questions
See similar questions with these tags.
it does weird things
const byte numChars = 350;
? max byte is 255. and you have ndx as byte too.