0x prefix to serial write byte
in
Programming Questions
•
3 months ago
I'm trying to interface processing with a display with a very simple protocol. The values that the display receives have to be hex, and all writes to the display need to have a 0x prefix to tell the display to expect hex. I've got various tests running on the display when hardcoding it, for example -
myPort.write(0x00); // turns one line to all black
I'm now looking to be able to display images onto the display with a pixel array, I've used a version of ladyadas thermal printer bitmap (only runs on processing 1.5.1) converter sketch to take pixel data and create hex calls for each vertical column on the display. See original sketch below.
- // Convert image to a C header file suitable for the Adafruit_Thermal library.
- // This is NOT an Arduino sketch. Runs in Processing IDE (www.processing.org)
- void setup() {
- String filename, basename;
- PImage img;
- PrintWriter output;
- int i, x, y, b, rowBytes, totalBytes, lastBit, sum, n;
- // Select and load image
- filename = selectInput("Select image file to convert:");
- println("Loading image...");
- img = loadImage(filename);
- // Morph filename into output filename and base name for data
- x = filename.lastIndexOf('.');
- if(x > 0) filename = filename.substring(0, x); // Strip current extension
- x = filename.lastIndexOf('/');
- if(x > 0) basename = filename.substring(x + 1); // Strip path
- else basename = filename;
- filename += ".h"; // Append '.h' to output filename
- println("Writing output to " + filename);
- // Calculate output size
- rowBytes = (img.width + 7) / 8;
- totalBytes = rowBytes * img.height;
- // Convert image to B&W, make pixels readable
- img.filter(THRESHOLD);
- img.loadPixels();
- // Open header file for output (NOTE: WILL CLOBBER EXISTING .H FILE, if any)
- output = createWriter(filename);
- // Write image dimensions and beginning of array
- output.println("#ifndef _" + basename + "_h_");
- output.println("#define _" + basename + "_h_");
- output.println();
- output.println("#define " + basename + "_width " + img.width);
- output.println("#define " + basename + "_height " + img.height);
- output.println();
- output.print("static PROGMEM prog_uchar " + basename + "_data[] = {");
- // Generate body of array
- for(i=n=y=0; y<img.height; y++) { // Each row...
- output.print("\n ");
- for(x=0; x<rowBytes; x++) { // Each 8-pixel block within row...
- lastBit = (x < rowBytes - 1) ? 1 : (1 << (rowBytes * 8 - img.width));
- sum = 0; // Clear accumulated 8 bits
- for(b=128; b>=lastBit; b >>= 1) { // Each pixel within block...
- if((img.pixels[i++] & 1) == 0) sum |= b; // If black pixel, set bit
- }
- output.format("0x%02X", sum); // Write accumulated bits
- if(++n < totalBytes) output.print(',');
- }
- }
- // End array, close file, exit program
- output.println();
- output.println("};");
- output.println();
- output.println("#endif // _" + basename + "_h_");
- output.flush();
- output.close();
- println("Done!");
- exit();
- }
And my modified version with port writes instead of output to file writes - It's replicating this line with a myPort.write that I am struggling with:
- output.format("0x%02X", sum); // Write accumulated bits
- if(++n < totalBytes) output.print(',');
- import processing.serial.*;
- Serial myPort;
- // Convert image to a C header file suitable for the Adafruit_Thermal library.
- // This is NOT an Arduino sketch. Runs in Processing IDE (www.processing.org)
- void setup() {
- println(Serial.list());
- // Open the port you are using at the rate you want:
- myPort = new Serial(this, Serial.list()[0], 57600);
- String filename, basename;
- PImage img;
- PrintWriter output;
- int i, x, y, b, rowBytes, totalBytes, lastBit, sum, n;
- // Select and load image
- filename = selectInput("Select image file to convert:");
- println("Loading image...");
- img = loadImage(filename);
- // Morph filename into output filename and base name for data
- x = filename.lastIndexOf('.');
- if(x > 0) filename = filename.substring(0, x); // Strip current extension
- x = filename.lastIndexOf('/');
- if(x > 0) basename = filename.substring(x + 1); // Strip path
- else basename = filename;
- filename += ".h"; // Append '.h' to output filename
- println("Writing output to " + filename);
- // Calculate output size
- rowBytes = (img.width + 7) / 8;
- totalBytes = rowBytes * img.height;
- // Convert image to B&W, make pixels readable
- img.filter(THRESHOLD);
- img.loadPixels();
- //
- // // Open header file for output (NOTE: WILL CLOBBER EXISTING .H FILE, if any)
- // output = createWriter(filename);
- //
- // // Write image dimensions and beginning of array
- // output.println("#ifndef _" + basename + "_h_");
- // output.println("#define _" + basename + "_h_");
- // output.println();
- // output.println("#define " + basename + "_width " + img.width);
- // output.println("#define " + basename + "_height " + img.height);
- // output.println();
- // output.print("static PROGMEM prog_uchar " + basename + "_data[] = {");
- // Generate body of array
- myPort.write(0x80); //header
- for(i=n=y=0; y<img.height; y++) { // Each row...
- //output.print("\n ");
- for(x=0; x<rowBytes; x++) { // Each 8-pixel block within row...
- lastBit = (x < rowBytes - 1) ? 1 : (1 << (rowBytes * 8 - img.width));
- sum = 0; // Clear accumulated 8 bits
- for(b=128; b>=lastBit; b >>= 1) { // Each pixel within block...
- if((img.pixels[i++] & 1) == 0) sum |= b; // If black pixel, set bit
- }
- println("0x" + hex(sum,2)); // Write accumulated bits
- myPort.write("0x" + hex(sum,2)); // Write accumulated bits
- //if(++n < totalBytes) output.print(',');
- }
- }
- myPort.write(0x8F); //end byte
- // End array, close file, exit program
- // output.println();
- // output.println("};");
- // output.println();
- // output.println("#endif // _" + basename + "_h_");
- // output.flush();
- // output.close();
- println("Done!");
- exit();
- }
- myPort.write("0x" + hex(sum,2)); // Write accumulated bits
This line shows up as a random pattern on the display and not what I need, however printing this serially - it looks like the correct format. I think the byte is being converted to an int when I add the 0x to it so I've tried to add the 0x in various ways and convert it to a byte after this, but without any luck.
Anyone got any ideas?
1