Toggles to Binary Byte to Hexadimal in string format

Liam Joy Niemeijer's icon

Dear Max community,

I'm working on a project where I use a 64 channel USB GPIO board which communicates via Serial. I've worked it out to get the intercommunication working. Now for me to adjust settings of all these pins in one command, I need to send the module a Hexadecimal value.

From the user manual:

" Control all GPIOs in a single operation. A hexadecimal value (xxxxxxxxxxxxxxxx) must be specified with desired bit positions set to 0 or 1. A value 0 at a bit position will turn off the corresponding GPIO. A value 1 at a bit position will turn on the corresponding GPIO "

"value 0000000000000000 (binary 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000) means all GPIO are OFF. A value ffffffffffffffff (binary 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111) means all GPIO are ON."

What I'm trying to do is to configure a patch which lets me toggle a GPIO on or off with a regular Max toggle button, which then converts it to bytes, which then converts it to a Hexadecimal message.

I've got pretty far with this, actually. The only step I can't figure out is how to turn the Hexadecimal-displayed value from the integer box into a string message of the same characters. In my example below I'm working on it with one byte. And used "p bin" from somewhere on this forum to change a string byte into an integer and then display the output in Hexadecimals. If I output the Hexadecimal value to a message box, though, it changes back into it's decimal value. Any idea on how to get the Hex in string?

Cheers!

Max Patch
Copy patch and select New From Clipboard in Max.

pdelges's icon

You can use sprintf but I don't understand why you need this. A number is a number… and serial expects base10 numbers.

Max Patch
Copy patch and select New From Clipboard in Max.

Liam Joy Niemeijer's icon

Thanks a lot! So the reason is that I need to send the module the following, for example: gpio writeall xxxxxxxxxxxxxxxx where the x's are values between 0 and F. So "gio writeall ffffffffffffffff" Sets all GPIO to high state and "gpio writeall 0000000000000000" sets them all to low. The module expects a 'readable string command' , which I translate from string to ASCII to send it through the Serial object. So the module expects the actual string (or ascii numbers that belong to that string) of the Hex-values:

A hexadecimal value (xxxxxxxxxxxxxxxx) must be specified with desired bit positions set to 0 or 1 with no “0x” prepended (eg 0000000000000002, 00000000000000ff)

Now i'm almost there with the conversion of the Hexadecimals to the Strings. The only issue left is that it leaves out the leading 0's. Is there a way to keep those during the process?

Then also, when I read out all the 64 gpio's with "gpio readall", I also get back a hexadecimal value. I would like to convert/route that to a BANG or 0-1 value for each individual GPIO, so I can start a specific action when a specific switch is turned over. Any ideas on how to accomplish this?

I hope I'm being sufficiently clear with my question, if not, please let me know!

Thanks

pdelges's icon

You'll find some ideas in the following patch.

Note that I had to make a fast but ugly change in your [p bin] as it outputs all steps instead of the final result (ugly because of your use of thresh, introducing a delay).

Max Patch
Copy patch and select New From Clipboard in Max.

Liam Joy Niemeijer's icon

Man you are awesome! I now route the returning HEX value through your patch and linking each of the 64 IO's [spray 64 ] to toggle buttons. It works flawlessly! Is it worth to paste the patch somewhere for others to use?

Thanks!