// // Communication parameters // // Tx/Rx const int T_S = 100; const int TX_DELAY = 5000; const int OVERSAMPLING = 2; // Messageing const int LEN_MSG = 4; const int LEN_MSG_TYPE = 2; const int LEN_SEQ_NBR = 2; const int LEN_ADDR = 4; const int LEN_CHECKSUM = 4; // Flags const int LEN_FLAG = 11; const int START_FLAG[] = {1,1,1,0,0,0,1,0,0,1,0}; // Shorter and flags design for teh IR medium would be better const int START_FLAG_MASK[] = {1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,1,1,-1,-1}; // Oversampling = 2, TO-DO - Generate dynamically const int STOP_FLAG[] = {0,0,0,1,1,1,0,1,1,0,1}; // Shorter and flags design for teh IR medium would be better const int STOP_FLAG_MASK[] = {-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,-1,-1,1,1,1,1,-1,-1,1,1}; // Oversampling = 2, TO-DO - Generate dynamically const int CORRELATION_THLD = 10; // A/D Converter const int AD_TH = 900; // Buffers const int LEN_BUFFER = LEN_FLAG*OVERSAMPLING*2 + LEN_ADDR*OVERSAMPLING*2 + LEN_MSG_TYPE*OVERSAMPLING + LEN_SEQ_NBR*OVERSAMPLING + LEN_MSG*OVERSAMPLING + LEN_CHECKSUM*OVERSAMPLING; // // Hardware // // LEDs const int LED_B = 10; const int LED_G = 11; const int LED_R = 12; const int DEB_1 = 7; const int DEB_2 = 8; const int DEB_3 = 9; const int PIN_RX = 0; const int PIN_TX = 13; // Address const int PIN_ADDR[] = {3,4,5,6}; // // Messaging // const int MSG_TYPE_ACK = 1; const int MSG_TYPE_DATA = 2; const int LEN_FRAME = LEN_ADDR*2 + LEN_MSG_TYPE + LEN_SEQ_NBR + LEN_MSG + LEN_CHECKSUM; // Rx message int rx_msg[LEN_FRAME]; int rx_msg_ptr = 0; int rx_msg_len = 0; int rx_msg_from = -1; int rx_msg_to = -1; int rx_msg_type = -1; int rx_msg_payload = -1; int rx_msg_seq_nbr = -1; boolean rx_msg_checksum = -1; // Tx message int tx_msg[LEN_FRAME]; int tx_msg_ptr = 0; int tx_msg_ctr = 0; int tx_msg_from = -1; int tx_msg_to = -1; int tx_msg_type = -1; int tx_msg_payload = -1; int tx_msg_seq_nbr = -1; // // Runtime // // States const int NONE = -1; // No state const int RECEIVE = 0; // Rx: Receive message with a timeout. const int SEND = 1; // Tx: Transmitt what is in the const int PROCESS = 2; // Process payload const int ACK_SEND = 3; // Handle ACK const int ACK_REC = 4; // Handle ACK const int PRODUCE = 5; // Produce content/messahe to send const int ACT = 6; // Act on payload const int WAIT = 7; // Wait const int DEBUG = 8; // Print all system proporties // Rx buffer const int START_FLAG_LEN_BUFFER = 11; int rx_buffer[LEN_BUFFER]; // Seize of 2 flags and max message int start_flag_ptr = -1; int stop_flag_ptr = -1; int rx_buffer_ptr = 0; // Address int address_bin[LEN_ADDR]; // Address is dynamically assigned using DIP switches. int address_dec = -1; // Runtime variables int i, j, result, corr, mean, sensor_value, start_point, index; // Not very readable, sorry :) boolean outcome; // Channel state boolean ch_available = 0; int ch_state[] = {-1,-1,-1,-1}; // Timekeeping unsigned long timer, time, t_s_delay_temp; // State int state = NONE; // // Device functionality // int current_led = -1; // // Code // void setup() { Serial.begin(9600); pinMode(LED_B, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_R, OUTPUT); pinMode(DEB_1, OUTPUT); pinMode(DEB_2, OUTPUT); pinMode(DEB_3, OUTPUT); pinMode(PIN_TX, OUTPUT); // Address pins for (i=0; i=start_point; i--){ Serial.print(data[i]); result += data[i]*pow(2.0, j) + data[i]*0.01; // +0.01 for float to int rouding error j++; } Serial.print(" -> "); Serial.println((int)result); return result; } // Decimal to binary with destination array with pointers void dec_to_bin(int number, int size_of_bin, int dest[], int start_point){ Serial.print("\t [F] Dedcimal to Binary: Number "); Serial.println(number); Serial.print("\t"); for(i=0; i AD_TH ? 0 : 1; } // Correlate signal with flag int correlate(const int mask[]){ corr = 0; start_point = (LEN_BUFFER + rx_buffer_ptr - LEN_FLAG * OVERSAMPLING) % LEN_BUFFER; for (i=0; i< LEN_FLAG * OVERSAMPLING; i++){ corr += mask[i] * rx_buffer[(start_point + i) % LEN_BUFFER]; } if (corr>=CORRELATION_THLD){ // Set automatically based on oversampling rate, flag, and mask. Serial.println("[Rx] Flag found"); return rx_buffer_ptr; } return -1; }