Lab_interaccio/2016/hybridPlay/hybridPlay1.0.0/Serial.ino

144 lines
3.5 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
// Serial INIT
void initSerial(){
Serial1.begin(SERIAL_BAUD_VEL);
if(debugMode){
Serial.begin(SERIAL_BAUD_VEL);
}
}
void receiveBluetoothSerial(){
while(Serial.available()>0){
Serial1.write(Serial.read());// input from Serial to Serial1
}
while(Serial1.available()>0){
int ascii = Serial1.read();
Serial.print(char(ascii)); //input from Serial1 to Serial
}
}
// function to send the given integer value to the serial port
void sendBinaryInt(int value){
// send the two byte that comprise an integer
Serial1.write(lowByte(value));
Serial1.write(highByte(value));
}
void sendOneByteInt(int value){
Serial1.write(lowByte(value));
}
// BLUETOOTH SERIAL SEND
void sendBluetoothSerial(){
Serial1.write(HEADER); // 1 BYTE
sendBinaryInt(valIR); // 2 BYTES
sendBinaryInt(valBattery); // 2 BYTES
Serial1.write(FOOTER); // 1 BYTE
Serial1.flush();
}
// USB DEBUG
void printData(){
Serial.print("IR ");
Serial.print(intToHexString(valIR));
Serial.print(", Battery ");
Serial.print(intToHexString(valBattery));
Serial.println();
}
/* Example message:
*
* 72 -- the character 'H', the HEADER
*
* Two BYTES for each analog pin:
* 0 -- pin 0 has an integer value of 0 so this is sent as two bytes
* 100 -- pin 1 has a value of 100, sent as a byte of 100 and a byte of 0
* 500 -- pin 2 has a value of 500, the reminder of 500 divided by 256 is 244,
* the number of times 500 can be divided by 256 is 1, so here
* we have the first byte of 244 and the second byte of 1
*
* 70 -- the character 'F', the FOOTER
*/
/* RECEIVING (on the other side)
*
* We have to wait for a message of X BYTES to arrive,
* and start the reading from the HEADER
*
* then we read the (X-2)/2 analog values (2 BYTES each), re-converting binary to integer,
* where:
*
* finalValue = least significant byte + most significant byte*256
*/
boolean findInResponse(const char *toMatch,
unsigned int timeOut = 1000) {
int byteRead;
unsigned long timeOutTarget; // in milliseconds
for (unsigned int offset = 0; offset < strlen(toMatch); offset++) {
timeOutTarget = millis() + timeOut; // Doesn't handle timer wrapping
while (!Serial1.available()) {
// Wait, with optional time out.
if (timeOut > 0) {
if (millis() > timeOutTarget) {
return false;
}
}
delay(1); // This seems to improve reliability slightly
}
byteRead = Serial1.read();
//Serial.print((char)byteRead);
delay(1); // Removing logging may affect timing slightly
if (byteRead != toMatch[offset]) {
offset = 0;
// Ignore character read if it's not a match for the start of the string
if (byteRead != toMatch[offset]) {
offset = -1;
}
continue;
}
}
return true;
}
boolean sendBle(const char* command, const char* expectedResponse)
{
Serial1.print(command);
delay(20);
Serial1.flush();
Serial1.println();
// TODO: Handle other responses
// (e.g. autoconnect message before it's turned off,
// DHCP messages, and/or ERR etc)
if (!findInResponse(expectedResponse, 3000)) {
return false;
}
//findInResponse(expectedResponse);
return true;
}
void skipRemainderOfResponse(unsigned int timeOut) {
unsigned long time = millis();
while (((millis()-time)<timeOut))
{
if (Serial1.available())
{
byte temp = Serial1.read();
//Serial.write(temp);
time = millis();
}
}
}