-3

I have a string of binary numbers coming into my Arduino. I need to convert them into an array. The data represents the columns of light in an LED display. In my program I already have a working function that takes an array and uses the data to display words to the screen. The data needs to be formatted like shown below:

My char string could look a few different ways. Here are the examples:

char CurrentWord = "11111110001000000100011111110000000B00000001000001111111110000010000000";

Or

char CurrentWord = "1111111 0001000 0001000 1111111 0000000 B0000000 1000001 1111111 1000001 0000000";

Or even:

char CurrentWord = "B1111111 B0001000 B0001000 B1111111 B0000000 B0000000 B1000001 B1111111 B1000001 B0000000";

The above examples would produce the word "Hi" on the screen. In order for the diplay to work however the data must be converted into an array. so it must look like this:

int CurrentWordInt[] = {B1111111, B0001000, B0001000, B1111111, B0000000, B0000000, B1000001, B1111111, B1000001, B0000000};

How can I do this?

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Tanner Ewing
  • 337
  • 4
  • 18
  • does the arduino sdk have strsep (or strtok)? – Joshua Smith Nov 10 '11 at 21:10
  • Should this be tagged objective-c? – jrturton Nov 10 '11 at 21:13
  • 3
    This looks similar to the question you asked two days ago. Maybe you should use the answers there! http://stackoverflow.com/questions/8043482/converting-a-string-to-data-from-a-lookup-table – Michael Wheeler Nov 10 '11 at 21:15
  • Do your have 7 bit bytes, or is that just a typo? – Jim Tshr Nov 10 '11 at 21:22
  • 1
    where is the objective C problem? – D33pN16h7 Nov 10 '11 at 21:22
  • There are 7 bit bytes because the displays are only 7 digits. Arduino also only supports strtok_r (re-entrant safe) – Tanner Ewing Nov 10 '11 at 21:24
  • How many seven segment modules do you have? what technic is the board using to display the text? Maybe you need to have a "screen framebuffer" if you have sufficient memory. Why are you sending binary strings to the board? you can have a translation table for characters... here are a lot of questions... how do you the board to display the text if the string sent to the board is larger than the display... do you want to scroll it? – joy Nov 10 '11 at 21:40
  • Why it is an int array, why it is not a char array? why do you need so much data to display hi? – joy Nov 10 '11 at 21:46
  • There are 10 5x7 dot matrix displays. The 7 bit bytes represent a column of LEDs. 1 = ON. 0 = OFF. How do I do a translation table?? That sounds like it may be much better. – Tanner Ewing Nov 10 '11 at 21:49
  • char alphabet[256] = {B0000000, B1110011, .... all 256 chars and then you can use this to translate char bitarray_c=alphabet['c']; – joy Nov 10 '11 at 22:17
  • Please don't tag questions like this (which would need code to answer properly) with multiple language tags. C, C++, or Objective-C -- which is it? Answers for one are generally not suitable for another. – ildjarn Nov 10 '11 at 22:23
  • upper example is for 7 segment led display, i will write one and for 5x7 led matrix display. – joy Nov 10 '11 at 22:25
  • Why binary strings instead of actual binary? This is an Arduino. This is embedded hardware. Never use a byte when a bit would do the job. – Zan Lynx Nov 10 '11 at 22:52

4 Answers4

0

You can use something like this:

char alphabet[256][5] = {{B0000000,
                          B0000000,
                          B0000000,
                          B0000000,
                          B0000000},
                          ...
                          //hLetter
                         {B1111111,
                          B0001000,
                          B0001000,
                          B0001000,
                          B1111111},
                          ....
                        };

or

char letter_H[5] = {B1111111,
                    B0001000,
                    B0001000,
                    B0001000,
                    B1111111}

char* translate_to_bitarray(char c)
{
  switch(c)
  {
  case 'H':
  case 'h':
   return letter_H;
  ...
  }
}

and then to make a function to translate a whole string...

Advice: Use a "framebuffer" - an array of 10 x 5 chars or 10 structs. Send to Arduino, normal, C strings. Convert characters from string, as needed, and put them in the frame buffer. In this way you can create scroll effect, very easily. Use a struct to keep character information.

struct character
{
  char _column1;
  char _column2;
  char _column3;
  char _column4;
  char _column5;
}
joy
  • 1,569
  • 8
  • 11
0

If the question is how to make C++ parse binary, then use this macro:

#define B(in) ( ((in&(1<< 0))>> 0) \
               |((in&(1<< 3))>> 2) \
               |((in&(1<< 6))>> 4) \
               |((in&(1<< 9))>> 6) \
               |((in&(1<<12))>> 8) \
               |((in&(1<<15))>>10) \
               |((in&(1<<18))>>12) \
               |((in&(1<<21))>>14) \
               |((in&(1<<24))>>16) \
              )


#include <iostream>
int main() {
    int CurrentWordInt[] = {B(01111111), B(00001000), B(0001000), B(01111111), B(0000000)}; 
    std::cout << CurrentWordInt[0] << ' ';
    std::cout << CurrentWordInt[1] << ' ';
    std::cout << CurrentWordInt[2] << ' ';
    std::cout << CurrentWordInt[3] << ' ';
    std::cout << CurrentWordInt[4] << ' ';
}

Displays 127 8 8 127 0
Note that this macro requires all inputs to be a zero followed by seven ones/zeros.

If that isn't your question, then I don't know what you're asking of us.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Him has to convert from a character to a 5x7 matrix led display. Here is a matrix http://www.lc-led.com/View/itemNumber/489. Every bit with value of 1, represents an ON led. In order to display a character him has to use 35 bits... but that is "not possible" and him has to use 40 bits. Of course it is possible with bit structs but will result in more complex code and anyway to compiler will allocate 40bytes for the structure... – joy Nov 10 '11 at 23:11
0
class bitbuffer {
    char buffer;
    char held_bits;
    char* input;
public:
    bitbuffer(char* in) :held_bits(0), buffer(0), input(in) {}
    unsigned long long read(unsigned char bits) { 
        unsigned long long result = 0;
        //if the buffer doesn't hold enough bits
        while (bits > held_bits) {
            //grab the all bits in the buffer
            bits -= held_bits;
            result |= ((unsigned long long)buffer) << bits;
            buffer = *(input++);
            held_bits = (char)std::cin.gcount() * CHAR_BIT;
        }
        //append the bits left to the end of the result
        result |= buffer >> (held_bits-bits);
        //remove those bits from the buffer
        held_bits -= bits;
        buffer &= (1ull<<held_bits)-1;
        return result;
    };
};

int main() {
    char* CurrentWord = data from stream
    bitbuffer bitter(CurrentWord);
    int CurrentWordInt[10];
    for(int i=0; i<10; ++i) {
        CurrentWordInt[i] = bitter.read(7); //reads next 7 bits
    }
}

Extrapolated from my answer at How to read bitN integer data from a binary file? Note this doesn't check boundries and will read past the end of the array given.

Community
  • 1
  • 1
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

c++ version:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int binstrToInt(const char* str){
    int wk = 0;
    while(*str){
        wk = wk * 2 + (*str++ - '0');
    }
    return wk;
}

vector<int> binstrToVector(const char* str, const size_t size){
    vector<int> v;
    istringstream iss(str);

    while(iss){
        ostringstream oss;
        for(int i=0;i<size;){
            char x;
            iss >> x;
            if(x == 'B' || x == 'b') continue;
            oss << x;
            ++i;
        }
        v.push_back(binstrToInt(oss.str().c_str()));
    }
    return v;
}

int main(){
    const char* binstr = "11111110001000000100011111110000000B00000001000001111111110000010000000";
    vector<int> v = binstrToVector(binstr, 7);

    for(int i=0;i<v.size();++i){
        cout << v[i] << endl;
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70