I'm grappling with types between two different libraries.
The gist is that I'm using the ArduinoWebsockets library to receive information coming from my API that I then want to render out to an LED matrix. An example message would look like this:
1
2
4
8
16
32
64
128
It's important to point out that the data here is a list of string version of numbers, not integers.
I.e. if you look at the binary version of 1
from the list, it's actually 110001
which is the ASCII code for the character 1
.
This is because the message coming back from the websocket server is pulled out as a const char *
.
The LED matrix function I'm calling has a signature of:
void Write_Max7219(unsigned char address, unsigned char dat)
So I need to send in the values as unsigned char
s.
I want to:
- parse this information line by line
- convert the ASCII character for the number into its integer equivalent
- pass the value to a different function to write the data to an LED matrix
I'm not sure how exactly to do this. I've been grappling with the type const char pointer vs regular char pointer and how to properly parse the return delimited string.
Any help would be greatly appreciated!!
Clarification
Based on some of the comments I don't think I explained the reason I'm trying to do this conversion very well. Here's (hopefully) a better explanation:
The reason I want to convert it is because I'm not displaying the number character as a string, I want to use the binary representation to control the lights in a row of the matrix.
The number 1 or 0b00000001 would light up only the rightmost led on the 8 led row of the matrix.
The idea is that I could send over rows of numbers where each row corresponds to the row on the LED matrix.
e.g.
1 -> 0b00000001
2 -> 0b00000010
4 -> 0b00000100
8 -> 0b00001000
16 -> 0b00010000
32 -> 0b00100000
64 -> 0b01000000
128 -> 0b10000000
But the messages I get from the websocket server are coming through as the string representations of the numbers (ASCII or UTF) vs the numbers themselves, so I can't just pass the numbers into the function for the LED matrix because the underlying binary is different.
The conversion is so that I can use the binary representation of 1 (0b00000001) vs the binary representation of '1' ( 00110001).
UPDATE AND RESOLUTION
So I've been on a big troubleshooting journey. When @ocrdu said:
BTW I would try to get the websocket to send the correct single bytes instead of string representations of numbers, if at all possible.
I thought "yeah, that's the real way of fixing it and understanding what's going on", so I started tracing the issue back. Without going into all of the detail it turns out that the problem I was having was actually starting at the test client.
I use firecamp and websocat as websocket client testing tools. I was trying to send the binary data via both tools, but when I dug in via Wireshark I found that the data in flight coming from the client itself was made up of the hex values for the encoded characters of the 1
s and 0
s.
After researching and asking a separate stackoverflow question I figured out a way of sending the actual binary data from the client, specifically printf
.
e.g.:
printf "\x01\x02\x03\x04\x05\x06\x07\x08" | websocat -b ws://localhost:3002
Now that this works I can start going back through the rest of my Arduino code and have it parse the binary data directly instead of converting the encoded values to their binary counterpoints.
All of that said, I do appreciate all of the help. I may not have needed to do the conversions for this particular instance, but I want to get better with my C types, conversions, and general understanding so the knowledge gained here is still beneficial. Thanks everyone :bows:
unsigned char
is one byte and your bit mask values fit into one byte.(char) strtoul(strData)
cplusplus.com/reference/cstdlib/strtoul