// https://wokwi.com/projects/409960228911809537
// Code from https://forum.arduino.cc/t/serial-input-basics-updated/382007/3#example-6-program-to-receive-binary-data-5
// Other simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754

/*

Example 6 - Program to receive binary data

Binary data
So far we have been receiving character data - for example 
the number 121 is represented by the characters '1', '2' and '1'. 
It is also possible to send that value as binary data in a single 
byte - it happens to be the Ascii value for the character 'y'. 
Note that 121 in decimal is the same as 0x79 in HEX.

Note that if you are sending binary data it is quite likely that 
you will need to send as data the same values that are used for 
the start- and end-markers. That goes beyond the scope of this 
Tutorial and one way of doing it is illustrated in the demo here:
https://forum.arduino.cc/t/demo-of-pc-arduino-comms-using-python/219184

The examples that follow assume that the binary data will NEVER include 
the byte values used for the start- and end-markers. For simplicity 
I will continue to use < and > as the markers. The byte values for 
those characters are 0x3C and 0x3E. This will allow you to test the 
program from the Serial Monitor by sending, for example, <24y> which 
will be interpreted by the receiving program as the binary values 
0x32, 0x34 and 0x79. These are the Ascii codes for 2, 4 and y.

Of course it would be more usual for binary data to be sent by another 
computer program - on another Arduino or on a PC.

*/


// Example 6 - Receiving binary data

const byte numBytes = 32;
byte receivedBytes[numBytes];
byte numReceived = 0;

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvBytesWithStartEndMarkers();
    showNewData();
}

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 0x3C;
    byte endMarker = 0x3E;
    byte rb;
   

    while (Serial.available() > 0 && newData == false) {
        rb = Serial.read();

        if (recvInProgress == true) {
            if (rb != endMarker) {
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= numBytes) {
                    ndx = numBytes - 1;
                }
            }
            else {
                receivedBytes[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                numReceived = ndx;  // save the number for use when printing
                ndx = 0;
                newData = true;
            }
        }

        else if (rb == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in (HEX values)... ");
        for (byte n = 0; n < numReceived; n++) {
            Serial.print(receivedBytes[n], HEX);
            Serial.print(' ');
        }
        Serial.println();
        newData = false;
    }
}