diff --git a/Accelerometer.cpp b/Accelerometer.cpp index 63b41feb09f74b951be3ceac77698bb91ff93146..70d0fd3d7dcb3a1ef075bd2984257efb637221b7 100644 --- a/Accelerometer.cpp +++ b/Accelerometer.cpp @@ -34,7 +34,7 @@ void Accelerometer :: Read_Accelerometer() { void Accelerometer :: Set_Packet() { pkt.pin = pin; - pkt.val = pin; + pkt.val = acc_val; } int_packet Accelerometer :: Get_Packet() { diff --git a/Configuration.h b/Configuration.h index 29cb4f91274d3ff2943d3af646c8f4d2392e6e1b..a5222a168251a46317ef6eaed5b3754a907a91e2 100644 --- a/Configuration.h +++ b/Configuration.h @@ -8,7 +8,7 @@ #include <Arduino.h> /* Runtime Configuration */ -#define DELAY 0 +#define DELAY 10 /* Debug Options */ #define DEBUG 0 diff --git a/SerialCommunication.cpp b/SerialCommunication.cpp index d9d9a4ba0858d728d84d6046af69c391bfd403ea..22f45cdb43945378dbbf2a18300b254780eaf30d 100644 --- a/SerialCommunication.cpp +++ b/SerialCommunication.cpp @@ -16,7 +16,7 @@ SerialCommunication::SerialCommunication() { header = 0x12FF; end_byte = 0x34EE; ptr = send_pkt; - message_size = 0; + packet_size = 0; } SerialCommunication::~SerialCommunication() {} @@ -27,12 +27,12 @@ void SerialCommunication::Serial_Setup() { } void SerialCommunication::Read_Update_Request() { - /* while (Serial.available() > 0) { */ - if (Serial.available() > 0) { + while (Serial.available() > 0) { + /* if (Serial.available() > 0) { */ serial_in = (char)Serial.read(); if (serial_in == PING) { update_request = true; - /* break; */ + break; } else if (serial_in != PING) { update_request = false; } @@ -44,8 +44,8 @@ void SerialCommunication::Set_Update_Request(bool _update_request) { } void SerialCommunication::Reset_Buffer() { - ptr -= message_size; - message_size = 0; + ptr -= packet_size; + packet_size = 0; } bool SerialCommunication::Get_Update_Request() { @@ -75,9 +75,9 @@ int SerialCommunication::Set_Send_Pkt(int_packet packet_out) { memcpy(send_pkt + hdr_size + pkt_size + chk_size, &end_byte, edr_size); - message_size += len; + packet_size += len; - if (message_size > DEFAULT_BUFFER_LEN - 100) { + if (packet_size > DEFAULT_BUFFER_LEN - 100) { Reset_Buffer(); } @@ -87,10 +87,17 @@ int SerialCommunication::Set_Send_Pkt(int_packet packet_out) { int SerialCommunication::Send_Packet() { int bytesSent = 0; if (Serial.availableForWrite()) { - bytesSent = Serial.write(send_pkt, message_size); + Serial.print("Send_Packet :: packet size: "); + Serial.println(packet_size); + bytesSent = Serial.write(send_pkt, packet_size); Serial.flush(); } - free(send_pkt); - message_size = 0; + /* free(send_pkt); */ + Reset_Buffer(); + Serial.println(); return bytesSent; } + +uint16_t SerialCommunication::Get_Packet_Size() { + return packet_size; +} diff --git a/SerialCommunication.h b/SerialCommunication.h index 729f4fb3b488c3b5f1c52aab41f80d8417eb5289..ae0b8c1563924cd1fa553874e897bb2f6d133c99 100644 --- a/SerialCommunication.h +++ b/SerialCommunication.h @@ -18,12 +18,13 @@ class SerialCommunication { bool Get_Update_Request(); int Set_Send_Pkt(int_packet packet_out); int Send_Packet(); + uint16_t Get_Packet_Size(); private: char serial_in; bool update_request; // a byte is 8 bits 0xFF is 255, represented in 8 bits. uint16_t checksum, header, end_byte; - uint16_t message_size; + uint16_t packet_size; unsigned char send_pkt[DEFAULT_BUFFER_LEN]; unsigned char *ptr; const int hdr_size = sizeof(header); diff --git a/multisensoryarduino.ino b/multisensoryarduino.ino index bf4cebcc7a237bc24710e378a12c9ad7a26bfcb5..7f716af1b16a243e37b438386e8a2878404aef4d 100644 --- a/multisensoryarduino.ino +++ b/multisensoryarduino.ino @@ -11,8 +11,11 @@ using namespace std; +bool update_request = false; int vibration_output = 0; int pot_val = 0; +int_packet ds_pkt_out; +int_packet acc_pkt_out; VibrationMotor VibeMotor(LED_BUILTIN, 0); Potentiometer Pot(POT0_PIN); @@ -35,6 +38,7 @@ void setup() { void loop() { for(int i = 0; i < 3; i++) { DSwitch[i].Read_DIPSwitch(); + Accel[i].Read_Accelerometer(); } Pot.Read_Potentiometer(); pot_val = Pot.Get_Reading(); @@ -43,25 +47,29 @@ void loop() { VibeMotor.Drive_Motor(); vibration_output = VibeMotor.Get_Amplitude(); - SerCom.Read_Update_Request(); - if (SerCom.Get_Update_Request()) { - for (int j = 0 ; j < 3; j++) { - DSwitch[j].Set_Packet(); - Accel[j].Set_Packet(); - int_packet ds_pkt_out = DSwitch[j].Get_Packet(); - int_packet acc_pkt_out = Accel[j].Get_Packet(); - SerCom.Set_Send_Pkt(ds_pkt_out); - SerCom.Set_Send_Pkt(acc_pkt_out); - } - Pot.Set_Packet(); - VibeMotor.Set_Packet(); + for (int j = 0 ; j < 3; j++) { + DSwitch[j].Set_Packet(); + Accel[j].Set_Packet(); + ds_pkt_out = DSwitch[j].Get_Packet(); + acc_pkt_out = Accel[j].Get_Packet(); + SerCom.Set_Send_Pkt(ds_pkt_out); + SerCom.Set_Send_Pkt(acc_pkt_out); + } + Pot.Set_Packet(); + VibeMotor.Set_Packet(); - int_packet pot_pkt_out = Pot.Get_Packet(); - int_packet vm_pkt_out = VibeMotor.Get_Packet(); + int_packet pot_pkt_out = Pot.Get_Packet(); + int_packet vm_pkt_out = VibeMotor.Get_Packet(); - SerCom.Set_Send_Pkt(pot_pkt_out); - SerCom.Set_Send_Pkt(vm_pkt_out); + SerCom.Set_Send_Pkt(pot_pkt_out); + SerCom.Set_Send_Pkt(vm_pkt_out); + SerCom.Read_Update_Request(); + update_request = SerCom.Get_Update_Request(); + + Serial.print("packet size: "); + Serial.println(SerCom.Get_Packet_Size()); + if (update_request) { SerCom.Send_Packet(); }