Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const unsigned int MAX_INPUT = 50;
- String Line[10];
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- //check for terminal input via display.
- while (Serial.available () > 0) {
- processIncomingByte (Serial.read ());
- }
- }
- void processIncomingByte (const byte inByte)
- {
- static char input_line [MAX_INPUT];
- static unsigned int input_pos = 0;
- switch (inByte)
- {
- case '\n': // end of text
- input_line [input_pos] = 0; // terminating null byte
- // terminator reached! process input_line here ...
- process_data (input_line);
- // reset buffer for next time
- input_pos = 0;
- break;
- case '\r': // discard carriage return
- break;
- default:
- // keep adding if not full ... allow for terminating null byte
- if (input_pos < (MAX_INPUT - 1))
- input_line [input_pos++] = inByte;
- break;
- } // end of switch
- }
- void process_data(char str[])
- {
- char *pch;
- //text parsing
- int counter = 0;
- pch = strtok(str, " ,.-");
- //read through the input and split it into new lines based on where spaces and other symbols are.
- while (pch != NULL)
- {
- //set line to be equal to the split.
- Line[counter] = pch;
- pch = strtok(NULL, "= ,.>");
- counter++;
- }
- Serial.println(Line[0]);
- Serial.println(Line[1]);
- //if the command is the one we are looking for then run the hex command
- if (Line[0] == "HEX") {
- ConvertHex(Line[1]);
- }
- }
- long ConvertHex(String input)
- {
- //how many hex chars there are. (if you change this youll need to redo the math at the bottom.
- int HexValues[4];
- //print how many get recived.
- Serial.println(input.length());
- //for the ammount of hex chars run through them and convert them into their individual base 10 values.
- for (int i = 0; i <= input.length() - 1; i++)
- {
- switch (input.charAt(i))
- {
- case 'A':
- HexValues[i] = 10;
- break;
- case 'B':
- HexValues[i] = 11;
- break;
- case 'C':
- HexValues[i] = 12;
- break;
- case 'D':
- HexValues[i] = 13;
- break;
- case 'E':
- HexValues[i] = 14;
- break;
- case 'F':
- HexValues[i] = 15;
- break;
- default:
- //if value is equal to its base 10 counter part then leave it be and convert it to int.
- HexValues[i] = (input.charAt(i) - '0');
- break;
- }
- //print the value of each hex symbol on its own.
- Serial.print("value:");
- Serial.println(HexValues[i]);
- }
- // add up all of the values and multiply based of their current place and to convert them to base 10.
- Serial.print("Total value:");
- Serial.println(HexValues[0] * 4096 + HexValues[1] * 256 + HexValues[2] * 16 + HexValues[3]);
- return (HexValues[0] * 4096 + HexValues[1] * 256 + HexValues[2] * 16 + HexValues[3]);
- //if you want to change this to do 24 bit hex inputs youll need to redo the math. here is where i got the equation from.
- //https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=1111
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement