kumar singh
/
Dealer_20Mar
BLE Transmitter not working
Fork of Dealer_23Feb by
main.cpp@13:8955f2e95021, 2017-02-12 (annotated)
- Committer:
- NarendraSingh
- Date:
- Sun Feb 12 02:57:25 2017 +0000
- Revision:
- 13:8955f2e95021
- Parent:
- 12:6107b32b0729
- Child:
- 15:a448e955b8f3
Before implementing beacon minor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
NarendraSingh | 11:77e595130230 | 2 | #include "rtos.h" |
NarendraSingh | 11:77e595130230 | 3 | #include "OBD.h" |
NarendraSingh | 11:77e595130230 | 4 | #include "Lora.h" |
NarendraSingh | 11:77e595130230 | 5 | #include "Accelerometer.h" |
NarendraSingh | 11:77e595130230 | 6 | #include "Beacon.h" |
NarendraSingh | 11:77e595130230 | 7 | #include "main.h" |
NarendraSingh | 11:77e595130230 | 8 | |
NarendraSingh | 11:77e595130230 | 9 | //Datatype typecasting |
NarendraSingh | 11:77e595130230 | 10 | typedef unsigned char uint8; |
NarendraSingh | 11:77e595130230 | 11 | typedef unsigned int uint16; |
NarendraSingh | 11:77e595130230 | 12 | |
NarendraSingh | 11:77e595130230 | 13 | //peripheral connection |
emilmont | 1:491820ee784d | 14 | DigitalOut led1(LED1); |
emilmont | 1:491820ee784d | 15 | DigitalOut led2(LED2); |
NarendraSingh | 11:77e595130230 | 16 | |
NarendraSingh | 11:77e595130230 | 17 | //Configure Serial port |
NarendraSingh | 11:77e595130230 | 18 | RawSerial LORA_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway |
NarendraSingh | 11:77e595130230 | 19 | RawSerial DEBUG_UART(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 11:77e595130230 | 20 | RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module |
NarendraSingh | 13:8955f2e95021 | 21 | RawSerial BLE_RECEIVER_UART(PA_0, PA_1);//USART4_TX->PA_0,USART4_RX->PA_1 : Used for Lora module command sending and reception from gateway |
NarendraSingh | 11:77e595130230 | 22 | |
NarendraSingh | 11:77e595130230 | 23 | //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); |
NarendraSingh | 11:77e595130230 | 24 | InterruptIn CheckIn_Interrupt(PC_13); |
NarendraSingh | 11:77e595130230 | 25 | |
NarendraSingh | 11:77e595130230 | 26 | uint8 Ticker_Count = 0; //Variable to count for timer overflows |
NarendraSingh | 11:77e595130230 | 27 | |
NarendraSingh | 11:77e595130230 | 28 | |
NarendraSingh | 11:77e595130230 | 29 | uint8 OBD_Plugin_Detected = FALSE; |
emilmont | 1:491820ee784d | 30 | |
NarendraSingh | 11:77e595130230 | 31 | //Create Object for structure of Lora Packet to be sent |
NarendraSingh | 11:77e595130230 | 32 | |
NarendraSingh | 11:77e595130230 | 33 | static uint16 Calculate_Wheels_RPM(uint8* Buffer); |
NarendraSingh | 11:77e595130230 | 34 | void flip_Packet_Sending_Flag(); |
NarendraSingh | 11:77e595130230 | 35 | void Lora_Periodic_Packet_Sending_thread(void const *args); |
NarendraSingh | 11:77e595130230 | 36 | |
NarendraSingh | 11:77e595130230 | 37 | void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); |
NarendraSingh | 13:8955f2e95021 | 38 | void Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 13:8955f2e95021 | 39 | |
NarendraSingh | 13:8955f2e95021 | 40 | |
NarendraSingh | 11:77e595130230 | 41 | /*************************Accelerometer related definitions***********************************/ |
NarendraSingh | 11:77e595130230 | 42 | //Accelerometer related definitions |
NarendraSingh | 13:8955f2e95021 | 43 | |
NarendraSingh | 11:77e595130230 | 44 | #define DOUBLE_TAP_INTERRUPT 0x20 |
NarendraSingh | 11:77e595130230 | 45 | #define ACTIVITY_INTERRUPT 0x10 |
NarendraSingh | 11:77e595130230 | 46 | #define INACTIVITY_INTERRUPT 0x08 |
NarendraSingh | 11:77e595130230 | 47 | |
NarendraSingh | 11:77e595130230 | 48 | #define TAP_THRESHOLD 75 |
NarendraSingh | 11:77e595130230 | 49 | #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity |
NarendraSingh | 11:77e595130230 | 50 | #define INACTIVITY_THRESHOLD 50 |
NarendraSingh | 11:77e595130230 | 51 | |
NarendraSingh | 11:77e595130230 | 52 | #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt |
NarendraSingh | 11:77e595130230 | 53 | #define LATENT_TIME 0x15 // The interrupt latency |
NarendraSingh | 11:77e595130230 | 54 | #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected |
NarendraSingh | 11:77e595130230 | 55 | #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt |
NarendraSingh | 11:77e595130230 | 56 | // Here the value 5 indicates literally 5 secs |
NarendraSingh | 11:77e595130230 | 57 | #define X_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 58 | #define Y_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 59 | #define Z_AXIS_OFFSET 0x05 |
NarendraSingh | 11:77e595130230 | 60 | |
NarendraSingh | 11:77e595130230 | 61 | |
NarendraSingh | 11:77e595130230 | 62 | I2C i2c(PB_9, PB_8); |
NarendraSingh | 11:77e595130230 | 63 | RawSerial DEBUG_UART1(PA_9, PA_10);//USART1_TX->PA_9,USART1_RX->PA_10 |
NarendraSingh | 11:77e595130230 | 64 | |
NarendraSingh | 11:77e595130230 | 65 | InterruptIn activity(PB_0); |
NarendraSingh | 11:77e595130230 | 66 | InterruptIn inactivity(PA_4); // As for now only this is used |
NarendraSingh | 11:77e595130230 | 67 | DigitalOut led(LED1); |
NarendraSingh | 11:77e595130230 | 68 | |
NarendraSingh | 11:77e595130230 | 69 | const int slave_address_acc = 0xA6; |
NarendraSingh | 11:77e595130230 | 70 | char axis_data[6] = {0,0,0,0,0,0}; |
NarendraSingh | 11:77e595130230 | 71 | |
NarendraSingh | 11:77e595130230 | 72 | char interrupt_source[2]; |
NarendraSingh | 11:77e595130230 | 73 | char axis_data_start_address[2] = {0x32, 0}; |
NarendraSingh | 11:77e595130230 | 74 | char intr_source_address[2] = {0x30, 0}; |
NarendraSingh | 11:77e595130230 | 75 | char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
NarendraSingh | 11:77e595130230 | 76 | char all_interrupt_enable_command[2] = {0x2E, 0x38}; |
NarendraSingh | 11:77e595130230 | 77 | char activity_interrupt_disable_command[2] = {0x2E, 0x08}; |
NarendraSingh | 11:77e595130230 | 78 | char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
NarendraSingh | 11:77e595130230 | 79 | char accelerometer_status_registered = 0; |
NarendraSingh | 11:77e595130230 | 80 | unsigned int interrupt_source_duplicate; |
NarendraSingh | 11:77e595130230 | 81 | |
NarendraSingh | 11:77e595130230 | 82 | char threshold_offset_command[5]; |
NarendraSingh | 11:77e595130230 | 83 | char act_inact_time_config_command[8]; |
NarendraSingh | 11:77e595130230 | 84 | char interrupt_enable_command[3]; |
NarendraSingh | 11:77e595130230 | 85 | char tap_axis_enable_command[2]; |
NarendraSingh | 11:77e595130230 | 86 | char baud_rate_command[2]; |
NarendraSingh | 11:77e595130230 | 87 | char data_format_command[2]; |
NarendraSingh | 11:77e595130230 | 88 | char measure_bit_on_command[2]; |
NarendraSingh | 11:77e595130230 | 89 | |
NarendraSingh | 11:77e595130230 | 90 | |
NarendraSingh | 11:77e595130230 | 91 | char previous_state = 0; |
NarendraSingh | 11:77e595130230 | 92 | char current_state = 0; |
NarendraSingh | 11:77e595130230 | 93 | |
NarendraSingh | 11:77e595130230 | 94 | unsigned char vehicle_speed = 25; // Kmph |
NarendraSingh | 11:77e595130230 | 95 | unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph |
NarendraSingh | 11:77e595130230 | 96 | |
NarendraSingh | 11:77e595130230 | 97 | unsigned char x_axis, y_axis, z_axis; |
NarendraSingh | 11:77e595130230 | 98 | |
NarendraSingh | 11:77e595130230 | 99 | unsigned char Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 100 | uint8 OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 101 | unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown |
NarendraSingh | 11:77e595130230 | 102 | void Accelerometer_Process_thread();//void const *args) ; |
NarendraSingh | 11:77e595130230 | 103 | |
NarendraSingh | 13:8955f2e95021 | 104 | #define BLE_RECEIVER_UART_RX_Size 100 |
NarendraSingh | 13:8955f2e95021 | 105 | uint8 BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 13:8955f2e95021 | 106 | char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; |
NarendraSingh | 13:8955f2e95021 | 107 | |
NarendraSingh | 11:77e595130230 | 108 | //This function is Interrupt routine for detecting motion(acceleartion), i.e. either starts from rest/vehicle stops afeter running or if sudden jurk is detected |
NarendraSingh | 11:77e595130230 | 109 | void interrupt_activity_inactivity() |
NarendraSingh | 11:77e595130230 | 110 | { |
NarendraSingh | 11:77e595130230 | 111 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
NarendraSingh | 11:77e595130230 | 112 | Motion_Detect_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 113 | } |
NarendraSingh | 13:8955f2e95021 | 114 | |
NarendraSingh | 13:8955f2e95021 | 115 | |
NarendraSingh | 11:77e595130230 | 116 | /************************************************************************/ |
NarendraSingh | 11:77e595130230 | 117 | uint8 Command_Sent[100]; |
NarendraSingh | 11:77e595130230 | 118 | uint8 Command_Length_Sent; |
NarendraSingh | 12:6107b32b0729 | 119 | uint8 Checkin_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 120 | void Extract_Received_Lora_Response(void); |
NarendraSingh | 11:77e595130230 | 121 | |
NarendraSingh | 11:77e595130230 | 122 | //This function is Interrupt routine for detecting OBD Plugin and Out |
NarendraSingh | 11:77e595130230 | 123 | void Handle_CheckIn_Interrupt() |
NarendraSingh | 11:77e595130230 | 124 | { |
NarendraSingh | 11:77e595130230 | 125 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 126 | DEBUG_UART.printf("Movement_Detected"); |
NarendraSingh | 11:77e595130230 | 127 | } |
NarendraSingh | 11:77e595130230 | 128 | |
NarendraSingh | 11:77e595130230 | 129 | //Declare Ticker for sending lora packet |
NarendraSingh | 11:77e595130230 | 130 | Ticker Lora_Packet_Sending_Ticker; |
NarendraSingh | 11:77e595130230 | 131 | void flip_Packet_Sending_Flag(void) |
NarendraSingh | 11:77e595130230 | 132 | { //flip function |
NarendraSingh | 11:77e595130230 | 133 | if(Ticker_Count < 5) |
NarendraSingh | 11:77e595130230 | 134 | { |
NarendraSingh | 11:77e595130230 | 135 | Ticker_Count++; |
NarendraSingh | 11:77e595130230 | 136 | } |
NarendraSingh | 11:77e595130230 | 137 | else |
NarendraSingh | 11:77e595130230 | 138 | { |
NarendraSingh | 11:77e595130230 | 139 | Ticker_Count = 0; |
NarendraSingh | 11:77e595130230 | 140 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 11:77e595130230 | 141 | } |
NarendraSingh | 11:77e595130230 | 142 | } |
NarendraSingh | 11:77e595130230 | 143 | |
NarendraSingh | 11:77e595130230 | 144 | // called every time a byte is received from lora module. |
NarendraSingh | 11:77e595130230 | 145 | void Lora_onDataRx() |
NarendraSingh | 11:77e595130230 | 146 | { |
NarendraSingh | 11:77e595130230 | 147 | while (LORA_UART.readable()) |
NarendraSingh | 11:77e595130230 | 148 | { // while there is data waiting |
NarendraSingh | 11:77e595130230 | 149 | LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer |
NarendraSingh | 11:77e595130230 | 150 | //DEBUG_UART.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); |
NarendraSingh | 11:77e595130230 | 151 | if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) |
NarendraSingh | 11:77e595130230 | 152 | { |
NarendraSingh | 11:77e595130230 | 153 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 154 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 155 | Lora_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 156 | } |
emilmont | 1:491820ee784d | 157 | } |
NarendraSingh | 13:8955f2e95021 | 158 | } |
NarendraSingh | 11:77e595130230 | 159 | |
NarendraSingh | 13:8955f2e95021 | 160 | |
NarendraSingh | 11:77e595130230 | 161 | // called every time a byte is received from Beacon Module. |
NarendraSingh | 11:77e595130230 | 162 | void Beacon_onDataRx() |
NarendraSingh | 11:77e595130230 | 163 | { |
NarendraSingh | 11:77e595130230 | 164 | while (Beacon_UART.readable()) |
NarendraSingh | 11:77e595130230 | 165 | { // while there is data waiting |
NarendraSingh | 11:77e595130230 | 166 | Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer |
NarendraSingh | 11:77e595130230 | 167 | //DEBUG_UART.putc(LORA_UART_RX_Buffer[Beacon_RxBuffer_End_Pos-1]); |
NarendraSingh | 11:77e595130230 | 168 | if(Beacon_RxBuffer_End_Pos >= 100) |
NarendraSingh | 11:77e595130230 | 169 | { |
NarendraSingh | 11:77e595130230 | 170 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 171 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 172 | Beacon_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 173 | } |
emilmont | 1:491820ee784d | 174 | } |
emilmont | 1:491820ee784d | 175 | } |
NarendraSingh | 11:77e595130230 | 176 | |
NarendraSingh | 13:8955f2e95021 | 177 | void BLE_Receiver_onDataRx(void) |
NarendraSingh | 13:8955f2e95021 | 178 | { |
NarendraSingh | 13:8955f2e95021 | 179 | while (BLE_RECEIVER_UART.readable()) |
NarendraSingh | 13:8955f2e95021 | 180 | { // while there is data waiting |
NarendraSingh | 13:8955f2e95021 | 181 | BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer |
NarendraSingh | 13:8955f2e95021 | 182 | //DEBUG_UART.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); |
NarendraSingh | 13:8955f2e95021 | 183 | if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) |
NarendraSingh | 13:8955f2e95021 | 184 | { |
NarendraSingh | 13:8955f2e95021 | 185 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 13:8955f2e95021 | 186 | // For now just throw everything away. |
NarendraSingh | 13:8955f2e95021 | 187 | BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 13:8955f2e95021 | 188 | } |
NarendraSingh | 12:6107b32b0729 | 189 | } |
NarendraSingh | 13:8955f2e95021 | 190 | } |
NarendraSingh | 13:8955f2e95021 | 191 | |
NarendraSingh | 13:8955f2e95021 | 192 | int main() |
NarendraSingh | 11:77e595130230 | 193 | { |
NarendraSingh | 11:77e595130230 | 194 | DEBUG_UART.baud(115200); |
NarendraSingh | 11:77e595130230 | 195 | DEBUG_UART.printf("%s","Debugging started"); |
NarendraSingh | 11:77e595130230 | 196 | LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); |
NarendraSingh | 13:8955f2e95021 | 197 | //BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); |
NarendraSingh | 11:77e595130230 | 198 | |
NarendraSingh | 11:77e595130230 | 199 | //Create a thread to read vehicle data |
NarendraSingh | 11:77e595130230 | 200 | //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread); |
NarendraSingh | 11:77e595130230 | 201 | |
NarendraSingh | 11:77e595130230 | 202 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 3.0); // call flip_Packet_Sending_Flag function every 5 seconds |
NarendraSingh | 11:77e595130230 | 203 | |
NarendraSingh | 11:77e595130230 | 204 | // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge |
NarendraSingh | 11:77e595130230 | 205 | //led2_thread is executing concurrently with main at this point |
NarendraSingh | 11:77e595130230 | 206 | CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt); |
NarendraSingh | 11:77e595130230 | 207 | inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
NarendraSingh | 13:8955f2e95021 | 208 | Initialize_Beacon_Module(); |
NarendraSingh | 11:77e595130230 | 209 | Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here |
NarendraSingh | 13:8955f2e95021 | 210 | } |
NarendraSingh | 13:8955f2e95021 | 211 | |
NarendraSingh | 13:8955f2e95021 | 212 | //Function to be called when Interrupt is genearted for motion sensing, checkin |
NarendraSingh | 11:77e595130230 | 213 | void Initialize_Packets_Sent_Count(void) |
NarendraSingh | 11:77e595130230 | 214 | { |
NarendraSingh | 11:77e595130230 | 215 | Motion_Packet_Sent_Count = 0x00; |
NarendraSingh | 11:77e595130230 | 216 | CheckIN_Packet_Sent_Count = 0x00; |
NarendraSingh | 11:77e595130230 | 217 | } |
NarendraSingh | 11:77e595130230 | 218 | |
NarendraSingh | 12:6107b32b0729 | 219 | uint8 Status_Packet_Wait_Count = 0; |
NarendraSingh | 11:77e595130230 | 220 | void Lora_Periodic_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 221 | { |
NarendraSingh | 11:77e595130230 | 222 | DEBUG_UART.printf("Periodic packet sending intiialized"); |
NarendraSingh | 11:77e595130230 | 223 | Set_Up_Lora_Network_Configuration(); |
NarendraSingh | 11:77e595130230 | 224 | Initialize_lora_Packets(); |
NarendraSingh | 11:77e595130230 | 225 | while (true) |
NarendraSingh | 11:77e595130230 | 226 | { |
NarendraSingh | 11:77e595130230 | 227 | if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) //check if packet to be sent is Heartbeat packet |
NarendraSingh | 11:77e595130230 | 228 | { |
NarendraSingh | 12:6107b32b0729 | 229 | if(Send_Lora_Packet_Flag) //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 11:77e595130230 | 230 | { |
NarendraSingh | 12:6107b32b0729 | 231 | Status_Packet_Wait_Count++; |
NarendraSingh | 12:6107b32b0729 | 232 | if(Status_Packet_Wait_Count < 5) |
NarendraSingh | 12:6107b32b0729 | 233 | { |
NarendraSingh | 12:6107b32b0729 | 234 | Send_HeartBeat_Packet(); //call function to send heartbeat packet |
NarendraSingh | 12:6107b32b0729 | 235 | DEBUG_UART.printf("Sent HeartBeat Packet"); |
NarendraSingh | 12:6107b32b0729 | 236 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 12:6107b32b0729 | 237 | while(AT_Response_Receive_Status) |
NarendraSingh | 12:6107b32b0729 | 238 | Get_Lora_Response(); |
NarendraSingh | 12:6107b32b0729 | 239 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 12:6107b32b0729 | 240 | DEBUG_UART.printf("Heartbeat Packet Response Received"); |
NarendraSingh | 12:6107b32b0729 | 241 | } |
NarendraSingh | 12:6107b32b0729 | 242 | else |
NarendraSingh | 12:6107b32b0729 | 243 | { |
NarendraSingh | 12:6107b32b0729 | 244 | Status_Packet_Wait_Count = 0; |
NarendraSingh | 13:8955f2e95021 | 245 | Send_Vehicle_Status_Packet(); //call function to send heartbeat packet |
NarendraSingh | 12:6107b32b0729 | 246 | DEBUG_UART.printf("Sent Status Packet"); |
NarendraSingh | 12:6107b32b0729 | 247 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 12:6107b32b0729 | 248 | while(AT_Response_Receive_Status) |
NarendraSingh | 12:6107b32b0729 | 249 | Get_Lora_Response(); |
NarendraSingh | 12:6107b32b0729 | 250 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 12:6107b32b0729 | 251 | DEBUG_UART.printf("Status Packet Response Received"); |
NarendraSingh | 12:6107b32b0729 | 252 | } |
NarendraSingh | 11:77e595130230 | 253 | } |
NarendraSingh | 11:77e595130230 | 254 | } |
NarendraSingh | 11:77e595130230 | 255 | else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) //check if packet to be sent is motion packet |
NarendraSingh | 11:77e595130230 | 256 | { |
NarendraSingh | 11:77e595130230 | 257 | if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 11:77e595130230 | 258 | { |
NarendraSingh | 11:77e595130230 | 259 | Send_Motion_Packet(); |
NarendraSingh | 11:77e595130230 | 260 | DEBUG_UART.printf("Sent Motion Packet"); //call function to send periodic motion packet |
NarendraSingh | 11:77e595130230 | 261 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 262 | while(AT_Response_Receive_Status) |
NarendraSingh | 11:77e595130230 | 263 | Get_Lora_Response(); |
NarendraSingh | 11:77e595130230 | 264 | DEBUG_UART.printf("Motion Packet Response Received"); |
NarendraSingh | 11:77e595130230 | 265 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 11:77e595130230 | 266 | if(Motion_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 11:77e595130230 | 267 | { |
NarendraSingh | 11:77e595130230 | 268 | DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat"); |
NarendraSingh | 11:77e595130230 | 269 | Motion_Packet_Sent_Count = 0; |
NarendraSingh | 11:77e595130230 | 270 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 11:77e595130230 | 271 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 272 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker |
NarendraSingh | 11:77e595130230 | 273 | |
NarendraSingh | 11:77e595130230 | 274 | } |
NarendraSingh | 11:77e595130230 | 275 | } |
NarendraSingh | 11:77e595130230 | 276 | } |
NarendraSingh | 11:77e595130230 | 277 | else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) //check if packet to be sent is Checkin packet |
NarendraSingh | 11:77e595130230 | 278 | { |
NarendraSingh | 11:77e595130230 | 279 | if(Send_Lora_Packet_Flag) //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 11:77e595130230 | 280 | { |
NarendraSingh | 11:77e595130230 | 281 | Send_CheckIN_Packet(); //call function to send periodic checkIn packet |
NarendraSingh | 11:77e595130230 | 282 | DEBUG_UART.printf("Sent Checkin Packet"); |
NarendraSingh | 11:77e595130230 | 283 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 284 | while(AT_Response_Receive_Status) |
NarendraSingh | 11:77e595130230 | 285 | Get_Lora_Response(); |
NarendraSingh | 11:77e595130230 | 286 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 11:77e595130230 | 287 | DEBUG_UART.printf("Checkin Packet Response Received"); |
NarendraSingh | 11:77e595130230 | 288 | if(CheckIN_Packet_Sent_Count >= 5) //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 11:77e595130230 | 289 | { |
NarendraSingh | 11:77e595130230 | 290 | DEBUG_UART.printf("Packet Type Sending Changed to HeartBeat"); |
NarendraSingh | 11:77e595130230 | 291 | CheckIN_Packet_Sent_Count = 0; |
NarendraSingh | 11:77e595130230 | 292 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 11:77e595130230 | 293 | Initialize_Packets_Sent_Count(); |
NarendraSingh | 11:77e595130230 | 294 | } |
NarendraSingh | 11:77e595130230 | 295 | } |
NarendraSingh | 11:77e595130230 | 296 | } |
NarendraSingh | 11:77e595130230 | 297 | if(OBD_PlugInOut_IOC_Status) //Check if Accelerometer Interrupt is generated |
NarendraSingh | 11:77e595130230 | 298 | { |
NarendraSingh | 13:8955f2e95021 | 299 | //Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 11:77e595130230 | 300 | //Get_Acceleration_Type(); |
NarendraSingh | 11:77e595130230 | 301 | OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 12:6107b32b0729 | 302 | Checkin_Detect_Status ^= 0x01; |
NarendraSingh | 13:8955f2e95021 | 303 | Packet_Type_To_Send = CHECKIN_TYPE_PACKET; |
NarendraSingh | 13:8955f2e95021 | 304 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 12:6107b32b0729 | 305 | if(Checkin_Detect_Status) //OBD Plugin detected |
NarendraSingh | 12:6107b32b0729 | 306 | { |
NarendraSingh | 12:6107b32b0729 | 307 | wait(1); //wait for 1sec to avoid detecting plugin debounce |
NarendraSingh | 12:6107b32b0729 | 308 | CheckIn_Interrupt.fall(&Handle_CheckIn_Interrupt); //Change interrupt on change to falling type to detect plugout |
NarendraSingh | 12:6107b32b0729 | 309 | //write code to enable motion interrupt |
NarendraSingh | 12:6107b32b0729 | 310 | } |
NarendraSingh | 12:6107b32b0729 | 311 | else //OBD Plugout detecetd |
NarendraSingh | 12:6107b32b0729 | 312 | { |
NarendraSingh | 12:6107b32b0729 | 313 | CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt); //Change interrupt on change to rising type to detect plugin |
NarendraSingh | 12:6107b32b0729 | 314 | wait(1); //wait for 1sec to avoid detecting plugin debounce |
NarendraSingh | 12:6107b32b0729 | 315 | //write code to disable motion interrupt |
NarendraSingh | 12:6107b32b0729 | 316 | } |
NarendraSingh | 11:77e595130230 | 317 | } |
NarendraSingh | 11:77e595130230 | 318 | if(Motion_Detect_Status) //Check if Accelerometer Interrupt is generated |
NarendraSingh | 11:77e595130230 | 319 | { |
NarendraSingh | 11:77e595130230 | 320 | Get_Acceleration_Type(); |
NarendraSingh | 11:77e595130230 | 321 | Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 322 | } |
NarendraSingh | 11:77e595130230 | 323 | } |
NarendraSingh | 11:77e595130230 | 324 | } |
NarendraSingh | 11:77e595130230 | 325 | |
NarendraSingh | 11:77e595130230 | 326 | void Get_Acceleration_Type(void) |
NarendraSingh | 11:77e595130230 | 327 | { |
NarendraSingh | 11:77e595130230 | 328 | Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type |
NarendraSingh | 11:77e595130230 | 329 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 330 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed |
NarendraSingh | 11:77e595130230 | 331 | Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd |
NarendraSingh | 11:77e595130230 | 332 | Motion_Lora_Packet.Acceleration_Type = Motion_Type_Detected; //Read Type of motion deteceted |
NarendraSingh | 11:77e595130230 | 333 | //write code to read acceleration value for every 10sec after any of the acceleration is found |
NarendraSingh | 11:77e595130230 | 334 | } |
NarendraSingh | 13:8955f2e95021 | 335 | /* |
NarendraSingh | 11:77e595130230 | 336 | //This function is used to enable checkin packet sending once OBD device is plugged in |
NarendraSingh | 11:77e595130230 | 337 | void Enable_CheckIN_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 338 | { |
NarendraSingh | 12:6107b32b0729 | 339 | // Enable_CheckIN_Packet_Sending = TRUE; //set status to true |
NarendraSingh | 13:8955f2e95021 | 340 | if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE |
NarendraSingh | 11:77e595130230 | 341 | { |
NarendraSingh | 11:77e595130230 | 342 | if(Calculate_Lora_Frame_FCS((Command_Received + 1),(Command_Length + 1)) == (Command_Received[Command_Length + 2])) //Check for packet inegrity by checking FCS |
NarendraSingh | 11:77e595130230 | 343 | { |
NarendraSingh | 11:77e595130230 | 344 | if((Command_Received[0] == SET_BEACON_CMD0) && (Command_Received[1] == SET_BEACON_CMD1)) //Check if command is received for setting beacon parameters |
NarendraSingh | 11:77e595130230 | 345 | { |
NarendraSingh | 11:77e595130230 | 346 | Process_Beacon_Command_Received((Command_Received + 2)); |
NarendraSingh | 11:77e595130230 | 347 | } |
NarendraSingh | 11:77e595130230 | 348 | } |
NarendraSingh | 13:8955f2e95021 | 349 | } |
NarendraSingh | 13:8955f2e95021 | 350 | } */ |