Shop OBEX P1 Docs P2 Docs Learn Events
Badge / oLED display binary — Parallax Forums

Badge / oLED display binary

I'm setting up my badge to wear in a class. To make a little more educational, I'd like to display a letter on line one and that letter's ASCII binary equivalent on line 2.
I could brute-force code for the 20 letters & their binaries as a string. But it would be more useful to be able to use any letter by making an array of all letters.

The letter is no problem but I can't get the binary equivalent to display from an array. I've fooled around with many possibilities but no success. Other sources suggest a function to build the binary but they are complex. I'm hoping to use the char array and just change the print format specifier.

The following produces a "b" on both lines.

#include "simpletools.h" // Include simpletools library
#include "badgetools.h" // Include badgetools library
void main() // Main function
{
char letterAsChar[] = {'a' ,'b' ,'c' ,'d' ,'e'};
char letterAsBin[] = {0b01100001,0b01100010,0b01100011,0b01100100,0b01100101};
badge_setup(); // Call badge setup
screen_auto(OFF); // Auto-update off
oledprint(" "); //clear screen
pause(1000);
cursor(0, 0); // Column 0, line 0 - top left
oledprint("%c",letterAsChar[1]); // Print from char
cursor(0, 1); // Column 0, line 1 bottom left
oledprint("%c",letterAsBin[1]); // print from binary
screen_update(); // Copy to display
}

Comments

  • Changing to the following should work (changes on the 6th, and 14th lines):
    #include "simpletools.h" // Include simpletools library
    #include "badgetools.h" // Include badgetools library
    void main() // Main function
    {
        char letterAsChar[] = {'a' ,'b' ,'c' ,'d' ,'e'};
        char const * letterAsBin[] = {"0b01100001","0b01100010","0b01100011","0b01100100","0b01100101"};
        badge_setup(); // Call badge setup
        screen_auto(OFF); // Auto-update off
        oledprint(" "); //clear screen
        pause(1000);
        cursor(0, 0); // Column 0, line 0 - top left
        oledprint("%c",letterAsChar[1]); // Print from char
        cursor(0, 1); // Column 0, line 1 bottom left
        oledprint("%s",letterAsBin[1]); // print from binary
        screen_update(); // Copy to display
    }
    

    With the code you posted, your letterAsBin array is simply an array of 5 characters initialized using binary values. With the above changes it is now an array of 5 pointers to C style (NULL terminated) strings. Each array element is declared as a pointer to constant characters because attempting to modify string literals is underfined behavior. (STR30-C. Do not attempt to modify string literals). oledprint()'s format string was updated to print what is now a string rather than a character.
  • Thanks. At first was problem but worked after removing "0b" from beginning of each element of the letterAsBin[].
Sign In or Register to comment.