-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?

13
  • does the arduino sdk have strsep (or strtok)? Commented Nov 10, 2011 at 21:10
  • Should this be tagged objective-c? Commented Nov 10, 2011 at 21:13
  • 3
    This looks similar to the question you asked two days ago. Maybe you should use the answers there! stackoverflow.com/questions/8043482/… Commented Nov 10, 2011 at 21:15
  • Do your have 7 bit bytes, or is that just a typo? Commented Nov 10, 2011 at 21:22
  • 1
    where is the objective C problem? Commented Nov 10, 2011 at 21:22

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Him has to convert from a character to a 5x7 matrix led display. Here is a matrix 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...
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.

Comments

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;
    }
}

Comments

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;
}

2 Comments

I don' think C++ will parse B1111111
Him has a special compiler for embedded devices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.