kumar singh
/
Dealer_20Mar
BLE Transmitter not working
Fork of Dealer_23Feb by
main.cpp@26:506380fccce2, 2017-03-20 (annotated)
- Committer:
- NarendraSingh
- Date:
- Mon Mar 20 02:44:45 2017 +0000
- Revision:
- 26:506380fccce2
- Parent:
- 24:1063cfc311e5
Before troubleshooting BLE Transmitter
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 | 16:7703b9d92326 | 13 | uint8 OBD_Plug_In_Status = FALSE; |
NarendraSingh | 16:7703b9d92326 | 14 | |
NarendraSingh | 16:7703b9d92326 | 15 | //peripheral connection |
NarendraSingh | 26:506380fccce2 | 16 | //DigitalOut led1(PB_11); |
NarendraSingh | 26:506380fccce2 | 17 | //DigitalOut led2(LED2); |
NarendraSingh | 24:1063cfc311e5 | 18 | DigitalIn Switch2(PB_13); |
NarendraSingh | 11:77e595130230 | 19 | |
NarendraSingh | 26:506380fccce2 | 20 | DigitalOut led1(LED1); |
NarendraSingh | 26:506380fccce2 | 21 | DigitalOut led2(PA_11); |
NarendraSingh | 26:506380fccce2 | 22 | DigitalOut led3(PA_12); |
NarendraSingh | 26:506380fccce2 | 23 | |
NarendraSingh | 11:77e595130230 | 24 | //Configure Serial port |
NarendraSingh | 16:7703b9d92326 | 25 | 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 | 23:688ee106c385 | 26 | //RawSerial pc1(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 23:688ee106c385 | 27 | //RawSerial DEBUG_UART(PA_14, PA_15);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 16:7703b9d92326 | 28 | RawSerial Beacon_UART(PC_4, PC_5);//USART3_TX->PC4,USART3_RX->PC_5 : Used for sending command to beacon module |
NarendraSingh | 16:7703b9d92326 | 29 | RawSerial BLE_RECEIVER_UART(PA_9, PA_10);//USART1_TX->PA_0,USART1_RX->PA_1 : Used for Lora module command sending and reception from gateway |
NarendraSingh | 23:688ee106c385 | 30 | Serial pc1(USBTX, USBRX); |
NarendraSingh | 23:688ee106c385 | 31 | RawSerial DEBUG_UART(USBTX, USBRX);//USART1_TX->PA_9,USART1_RX->PA_10 : Used for debugging purpose only |
NarendraSingh | 16:7703b9d92326 | 32 | |
NarendraSingh | 16:7703b9d92326 | 33 | //InterruptIn OBD_PLUGIN_INTERRUPT_PIN(PC_13); |
NarendraSingh | 21:a5fb0ae94dc6 | 34 | InterruptIn CheckIn_Interrupt(PB_7);//(PC_13); |
NarendraSingh | 24:1063cfc311e5 | 35 | InterruptIn Motion_Start_To_Stop(PB_5);//(PC_13); |
NarendraSingh | 24:1063cfc311e5 | 36 | InterruptIn Motion_Stop_To_Start(PB_14);//(PC_13); |
NarendraSingh | 24:1063cfc311e5 | 37 | InterruptIn Motion_Sudden_Jerk(PC_13);//(PC_13); |
NarendraSingh | 24:1063cfc311e5 | 38 | |
NarendraSingh | 16:7703b9d92326 | 39 | |
NarendraSingh | 16:7703b9d92326 | 40 | uint8 Ticker_Count = 0; //Variable to count for timer overflows |
NarendraSingh | 16:7703b9d92326 | 41 | |
NarendraSingh | 16:7703b9d92326 | 42 | uint8 OBD_Plugin_Detected = FALSE; |
NarendraSingh | 16:7703b9d92326 | 43 | |
NarendraSingh | 16:7703b9d92326 | 44 | //Create Object for structure of Lora Packet to be sent |
NarendraSingh | 11:77e595130230 | 45 | |
NarendraSingh | 16:7703b9d92326 | 46 | static uint16 Calculate_Wheels_RPM(uint8* Buffer); |
NarendraSingh | 16:7703b9d92326 | 47 | void flip_Packet_Sending_Flag(); |
NarendraSingh | 16:7703b9d92326 | 48 | void Lora_Periodic_Packet_Sending_thread(void const *args); |
NarendraSingh | 11:77e595130230 | 49 | |
NarendraSingh | 16:7703b9d92326 | 50 | void Lora_Rcvd_Cmd_Processing_thread(void);// const *args); |
NarendraSingh | 16:7703b9d92326 | 51 | void Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 16:7703b9d92326 | 52 | |
NarendraSingh | 23:688ee106c385 | 53 | const char GET_RSSI[]= {0x41,0x54,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x0A}; |
NarendraSingh | 23:688ee106c385 | 54 | const char SET_BEACON_VENDOR_ID[]= {0x41,0x54,0xF1,0x01,0x02,0x00,0x00,0x00,0x00,0xF2,0x0D,0x0A}; |
NarendraSingh | 23:688ee106c385 | 55 | const char SET_BEACON_MESSAGE_TYPE[]= {0x41,0x54,0xF2,0x01,0x00,0x00,0x00,0x00,0x00,0xF3,0x0D,0x0A}; |
NarendraSingh | 11:77e595130230 | 56 | /*************************Accelerometer related definitions***********************************/ |
NarendraSingh | 11:77e595130230 | 57 | //Accelerometer related definitions |
NarendraSingh | 13:8955f2e95021 | 58 | |
NarendraSingh | 11:77e595130230 | 59 | #define DOUBLE_TAP_INTERRUPT 0x20 |
NarendraSingh | 11:77e595130230 | 60 | #define ACTIVITY_INTERRUPT 0x10 |
NarendraSingh | 11:77e595130230 | 61 | #define INACTIVITY_INTERRUPT 0x08 |
NarendraSingh | 11:77e595130230 | 62 | |
NarendraSingh | 16:7703b9d92326 | 63 | #define TAP_THRESHOLD 75 |
NarendraSingh | 11:77e595130230 | 64 | #define ACTIVITY_THRESHOLD 64 // THRES_ACT register value 62.5mg/LSB , therfore value 32 indicates 2g activity |
NarendraSingh | 11:77e595130230 | 65 | #define INACTIVITY_THRESHOLD 50 |
NarendraSingh | 11:77e595130230 | 66 | |
NarendraSingh | 11:77e595130230 | 67 | #define DUR_TIME 0x15 // DUR Register value providing maximum time to be held to generate an interrupt |
NarendraSingh | 11:77e595130230 | 68 | #define LATENT_TIME 0x15 // The interrupt latency |
NarendraSingh | 11:77e595130230 | 69 | #define WINDOW_TIME 0x45 // The time of the interrupt window in which the next tap will be detected |
NarendraSingh | 11:77e595130230 | 70 | #define INACTIVITY_VALIDATION_TIME 5 // The time until which the acceleration must be held below the inactivity threshold to generate an inactvity interrupt |
NarendraSingh | 16:7703b9d92326 | 71 | // Here the value 5 indicates literally 5 secs |
NarendraSingh | 11:77e595130230 | 72 | #define X_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 73 | #define Y_AXIS_OFFSET 0x7F |
NarendraSingh | 11:77e595130230 | 74 | #define Z_AXIS_OFFSET 0x05 |
NarendraSingh | 11:77e595130230 | 75 | |
NarendraSingh | 17:758fb8454ab0 | 76 | Serial pc(USBTX, USBRX); |
NarendraSingh | 16:7703b9d92326 | 77 | I2C i2c(PB_9, PB_8); |
NarendraSingh | 11:77e595130230 | 78 | |
NarendraSingh | 16:7703b9d92326 | 79 | InterruptIn activity(PB_0); |
NarendraSingh | 11:77e595130230 | 80 | InterruptIn inactivity(PA_4); // As for now only this is used |
NarendraSingh | 11:77e595130230 | 81 | DigitalOut led(LED1); |
NarendraSingh | 11:77e595130230 | 82 | |
NarendraSingh | 11:77e595130230 | 83 | const int slave_address_acc = 0xA6; |
NarendraSingh | 11:77e595130230 | 84 | char axis_data[6] = {0,0,0,0,0,0}; |
NarendraSingh | 11:77e595130230 | 85 | |
NarendraSingh | 11:77e595130230 | 86 | char interrupt_source[2]; |
NarendraSingh | 11:77e595130230 | 87 | char axis_data_start_address[2] = {0x32, 0}; |
NarendraSingh | 11:77e595130230 | 88 | char intr_source_address[2] = {0x30, 0}; |
NarendraSingh | 11:77e595130230 | 89 | char all_interrupt_clear_command[2] = {0x2E, 0x00}; |
NarendraSingh | 11:77e595130230 | 90 | char all_interrupt_enable_command[2] = {0x2E, 0x38}; |
NarendraSingh | 11:77e595130230 | 91 | char activity_interrupt_disable_command[2] = {0x2E, 0x08}; |
NarendraSingh | 11:77e595130230 | 92 | char inactivity_interrupt_disable_command[2] = {0x2E, 0x30}; |
NarendraSingh | 11:77e595130230 | 93 | char accelerometer_status_registered = 0; |
NarendraSingh | 11:77e595130230 | 94 | unsigned int interrupt_source_duplicate; |
NarendraSingh | 11:77e595130230 | 95 | |
NarendraSingh | 11:77e595130230 | 96 | char threshold_offset_command[5]; |
NarendraSingh | 11:77e595130230 | 97 | char act_inact_time_config_command[8]; |
NarendraSingh | 11:77e595130230 | 98 | char interrupt_enable_command[3]; |
NarendraSingh | 11:77e595130230 | 99 | char tap_axis_enable_command[2]; |
NarendraSingh | 11:77e595130230 | 100 | char baud_rate_command[2]; |
NarendraSingh | 11:77e595130230 | 101 | char data_format_command[2]; |
NarendraSingh | 11:77e595130230 | 102 | char measure_bit_on_command[2]; |
NarendraSingh | 11:77e595130230 | 103 | |
NarendraSingh | 11:77e595130230 | 104 | |
NarendraSingh | 11:77e595130230 | 105 | unsigned char vehicle_speed = 25; // Kmph |
NarendraSingh | 11:77e595130230 | 106 | unsigned char current_speed, previous_speed, speed_threshold = 10; // Kmph |
NarendraSingh | 11:77e595130230 | 107 | |
NarendraSingh | 11:77e595130230 | 108 | unsigned char x_axis, y_axis, z_axis; |
NarendraSingh | 11:77e595130230 | 109 | |
NarendraSingh | 11:77e595130230 | 110 | unsigned char Motion_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 111 | uint8 OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 112 | unsigned char Motion_Type_Detected = MOTION_TYPE_UNKNOWN; //By default set motion type as unknown |
NarendraSingh | 11:77e595130230 | 113 | void Accelerometer_Process_thread();//void const *args) ; |
NarendraSingh | 11:77e595130230 | 114 | |
NarendraSingh | 16:7703b9d92326 | 115 | #define BLE_RECEIVER_UART_RX_Size 100 |
NarendraSingh | 16:7703b9d92326 | 116 | uint8 BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 16:7703b9d92326 | 117 | char BLE_Receiver_UART_RX_Buffer[BLE_RECEIVER_UART_RX_Size]; |
NarendraSingh | 13:8955f2e95021 | 118 | |
NarendraSingh | 11:77e595130230 | 119 | //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 | 120 | void interrupt_activity_inactivity() |
NarendraSingh | 16:7703b9d92326 | 121 | { |
NarendraSingh | 16:7703b9d92326 | 122 | i2c.write(slave_address_acc, all_interrupt_clear_command, 2); |
NarendraSingh | 16:7703b9d92326 | 123 | Motion_Detect_Status = TRUE; |
NarendraSingh | 11:77e595130230 | 124 | } |
NarendraSingh | 13:8955f2e95021 | 125 | |
NarendraSingh | 13:8955f2e95021 | 126 | |
NarendraSingh | 16:7703b9d92326 | 127 | /************************************************************************/ |
NarendraSingh | 11:77e595130230 | 128 | uint8 Command_Sent[100]; |
NarendraSingh | 11:77e595130230 | 129 | uint8 Command_Length_Sent; |
NarendraSingh | 12:6107b32b0729 | 130 | uint8 Checkin_Detect_Status = FALSE; |
NarendraSingh | 11:77e595130230 | 131 | void Extract_Received_Lora_Response(void); |
NarendraSingh | 23:688ee106c385 | 132 | void Send_Command_To_BLE_Receiver(const char* Command); |
NarendraSingh | 16:7703b9d92326 | 133 | |
NarendraSingh | 16:7703b9d92326 | 134 | char previous_state = 0; |
NarendraSingh | 16:7703b9d92326 | 135 | char current_state = 0; |
NarendraSingh | 16:7703b9d92326 | 136 | |
NarendraSingh | 16:7703b9d92326 | 137 | uint8 OBD_PlugIN_State=0; |
NarendraSingh | 16:7703b9d92326 | 138 | uint8 OBD_PlugIN_State1=0; |
NarendraSingh | 16:7703b9d92326 | 139 | uint8 OBD_PlugIN_State2=0; |
NarendraSingh | 16:7703b9d92326 | 140 | uint8 OBD_PlugIN_Temp_State=1; |
NarendraSingh | 16:7703b9d92326 | 141 | |
NarendraSingh | 24:1063cfc311e5 | 142 | uint8 Start_To_Stop_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 143 | uint8 Stop_To_Start_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 144 | uint8 Jerk_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 145 | |
NarendraSingh | 24:1063cfc311e5 | 146 | void Start_To_Stop_Interrupt() |
NarendraSingh | 24:1063cfc311e5 | 147 | { |
NarendraSingh | 24:1063cfc311e5 | 148 | Start_To_Stop_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 149 | Motion_Detect_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 150 | } |
NarendraSingh | 24:1063cfc311e5 | 151 | |
NarendraSingh | 24:1063cfc311e5 | 152 | void Stop_To_Start_Interrupt() |
NarendraSingh | 24:1063cfc311e5 | 153 | { |
NarendraSingh | 24:1063cfc311e5 | 154 | Stop_To_Start_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 155 | Motion_Detect_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 156 | } |
NarendraSingh | 24:1063cfc311e5 | 157 | |
NarendraSingh | 24:1063cfc311e5 | 158 | void Sudden_Jerk_Interrupt() |
NarendraSingh | 24:1063cfc311e5 | 159 | { |
NarendraSingh | 24:1063cfc311e5 | 160 | Jerk_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 161 | Motion_Detect_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 162 | } |
NarendraSingh | 16:7703b9d92326 | 163 | void OBD_Plug_IN_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 164 | { |
NarendraSingh | 17:758fb8454ab0 | 165 | if(OBD_PlugIN_State1!=OBD_PlugIN_Temp_State) { |
NarendraSingh | 16:7703b9d92326 | 166 | OBD_PlugIN_State1=1; |
NarendraSingh | 16:7703b9d92326 | 167 | OBD_PlugIN_State=!OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 168 | OBD_PlugIN_Temp_State=OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 169 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 16:7703b9d92326 | 170 | } |
NarendraSingh | 16:7703b9d92326 | 171 | } |
NarendraSingh | 17:758fb8454ab0 | 172 | |
NarendraSingh | 16:7703b9d92326 | 173 | void OBD_Plug_OUT_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 174 | { |
NarendraSingh | 22:c2f034a13108 | 175 | |
NarendraSingh | 17:758fb8454ab0 | 176 | if(OBD_PlugIN_State2!=OBD_PlugIN_Temp_State) { |
NarendraSingh | 16:7703b9d92326 | 177 | OBD_PlugIN_State2=0; |
NarendraSingh | 16:7703b9d92326 | 178 | OBD_PlugIN_State=!OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 179 | OBD_PlugIN_Temp_State=OBD_PlugIN_State; |
NarendraSingh | 16:7703b9d92326 | 180 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 17:758fb8454ab0 | 181 | } |
NarendraSingh | 16:7703b9d92326 | 182 | } |
NarendraSingh | 16:7703b9d92326 | 183 | |
NarendraSingh | 16:7703b9d92326 | 184 | //This function is Interrupt routine for detecting OBD Plugin and Out |
NarendraSingh | 11:77e595130230 | 185 | void Handle_CheckIn_Interrupt() |
NarendraSingh | 16:7703b9d92326 | 186 | { |
NarendraSingh | 11:77e595130230 | 187 | OBD_PlugInOut_IOC_Status = TRUE; |
NarendraSingh | 24:1063cfc311e5 | 188 | pc.printf("\nMovement_Detected\n"); |
NarendraSingh | 11:77e595130230 | 189 | } |
NarendraSingh | 11:77e595130230 | 190 | |
NarendraSingh | 16:7703b9d92326 | 191 | //Declare Ticker for sending lora packet |
NarendraSingh | 16:7703b9d92326 | 192 | Ticker Lora_Packet_Sending_Ticker; |
NarendraSingh | 16:7703b9d92326 | 193 | void flip_Packet_Sending_Flag(void) |
NarendraSingh | 16:7703b9d92326 | 194 | { |
NarendraSingh | 24:1063cfc311e5 | 195 | led1=!led1; |
NarendraSingh | 16:7703b9d92326 | 196 | //flip function |
NarendraSingh | 16:7703b9d92326 | 197 | if(Ticker_Count < 5) { |
NarendraSingh | 11:77e595130230 | 198 | Ticker_Count++; |
NarendraSingh | 16:7703b9d92326 | 199 | } else { |
NarendraSingh | 11:77e595130230 | 200 | Ticker_Count = 0; |
NarendraSingh | 11:77e595130230 | 201 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 11:77e595130230 | 202 | } |
NarendraSingh | 16:7703b9d92326 | 203 | } |
NarendraSingh | 11:77e595130230 | 204 | |
NarendraSingh | 11:77e595130230 | 205 | // called every time a byte is received from lora module. |
NarendraSingh | 11:77e595130230 | 206 | void Lora_onDataRx() |
NarendraSingh | 11:77e595130230 | 207 | { |
NarendraSingh | 16:7703b9d92326 | 208 | while (LORA_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 209 | // while there is data waiting |
NarendraSingh | 11:77e595130230 | 210 | LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos++] = LORA_UART.getc(); // put it in the buffer |
NarendraSingh | 20:f812f85cf97e | 211 | //pc1.putc(LORA_UART_RX_Buffer[Lora_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 212 | if(Lora_RxBuffer_End_Pos >= LORA_UART_RX_Size) { |
NarendraSingh | 11:77e595130230 | 213 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 214 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 215 | Lora_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 216 | } |
emilmont | 1:491820ee784d | 217 | } |
NarendraSingh | 16:7703b9d92326 | 218 | } |
NarendraSingh | 11:77e595130230 | 219 | |
NarendraSingh | 11:77e595130230 | 220 | // called every time a byte is received from Beacon Module. |
NarendraSingh | 11:77e595130230 | 221 | void Beacon_onDataRx() |
NarendraSingh | 11:77e595130230 | 222 | { |
NarendraSingh | 16:7703b9d92326 | 223 | while (Beacon_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 224 | // while there is data waiting |
NarendraSingh | 11:77e595130230 | 225 | Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos++] = Beacon_UART.getc(); // put it in the buffer |
NarendraSingh | 23:688ee106c385 | 226 | pc1.printf("%2x",Beacon_RX_Buffer[Beacon_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 227 | if(Beacon_RxBuffer_End_Pos >= 100) { |
NarendraSingh | 11:77e595130230 | 228 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 11:77e595130230 | 229 | // For now just throw everything away. |
NarendraSingh | 11:77e595130230 | 230 | Beacon_RxBuffer_End_Pos = 0; |
NarendraSingh | 11:77e595130230 | 231 | } |
emilmont | 1:491820ee784d | 232 | } |
emilmont | 1:491820ee784d | 233 | } |
NarendraSingh | 11:77e595130230 | 234 | |
NarendraSingh | 16:7703b9d92326 | 235 | void BLE_Receiver_onDataRx(void) |
NarendraSingh | 13:8955f2e95021 | 236 | { |
NarendraSingh | 16:7703b9d92326 | 237 | while (BLE_RECEIVER_UART.readable()) { |
NarendraSingh | 16:7703b9d92326 | 238 | // while there is data waiting |
NarendraSingh | 24:1063cfc311e5 | 239 | //BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos++] = BLE_RECEIVER_UART.getc(); // put it in the buffer |
NarendraSingh | 22:c2f034a13108 | 240 | //pc1.putc(BLE_Receiver_UART_RX_Buffer[BLE_RxBuffer_End_Pos-1]); |
NarendraSingh | 16:7703b9d92326 | 241 | if(BLE_RxBuffer_End_Pos >= BLE_RECEIVER_UART_RX_Size) { |
NarendraSingh | 13:8955f2e95021 | 242 | // BUFFER OVERFLOW. What goes here depends on how you want to cope with that situation. |
NarendraSingh | 13:8955f2e95021 | 243 | // For now just throw everything away. |
NarendraSingh | 13:8955f2e95021 | 244 | BLE_RxBuffer_End_Pos = 0; |
NarendraSingh | 13:8955f2e95021 | 245 | } |
NarendraSingh | 12:6107b32b0729 | 246 | } |
NarendraSingh | 16:7703b9d92326 | 247 | } |
NarendraSingh | 16:7703b9d92326 | 248 | |
NarendraSingh | 26:506380fccce2 | 249 | #define LORA_SEND_THREAD "lora_send_thread" |
NarendraSingh | 26:506380fccce2 | 250 | #define HEART_BEAT_PACKET_SENDING 0x01 |
NarendraSingh | 26:506380fccce2 | 251 | #define STATUS_PACKET_SENDING 0x02 |
NarendraSingh | 26:506380fccce2 | 252 | |
NarendraSingh | 26:506380fccce2 | 253 | InterruptIn checkinIntr(PC_13); |
NarendraSingh | 26:506380fccce2 | 254 | InterruptIn motionIntr(PB_14); |
NarendraSingh | 26:506380fccce2 | 255 | InterruptIn testIntr(PB_5); |
NarendraSingh | 26:506380fccce2 | 256 | |
NarendraSingh | 26:506380fccce2 | 257 | |
NarendraSingh | 26:506380fccce2 | 258 | |
NarendraSingh | 26:506380fccce2 | 259 | /* reserve the debbuger uart to shell interface */ |
NarendraSingh | 26:506380fccce2 | 260 | Serial pc_serial(USBTX,USBRX); |
NarendraSingh | 26:506380fccce2 | 261 | |
NarendraSingh | 26:506380fccce2 | 262 | /* declares threads for this demo: */ |
NarendraSingh | 26:506380fccce2 | 263 | const size_t a_stk_size = 512; |
NarendraSingh | 26:506380fccce2 | 264 | uint8_t a_stk[a_stk_size]; |
NarendraSingh | 26:506380fccce2 | 265 | Thread loraSendThread(osPriorityNormal, a_stk_size, &a_stk[0]); |
NarendraSingh | 26:506380fccce2 | 266 | |
NarendraSingh | 26:506380fccce2 | 267 | const size_t c_stk_size = 512; |
NarendraSingh | 26:506380fccce2 | 268 | uint8_t c_stk[c_stk_size]; |
NarendraSingh | 26:506380fccce2 | 269 | |
NarendraSingh | 26:506380fccce2 | 270 | const size_t e_stk_size = 512; |
NarendraSingh | 26:506380fccce2 | 271 | uint8_t e_stk[e_stk_size]; |
NarendraSingh | 26:506380fccce2 | 272 | Thread checkinEventThread(osPriorityNormal, e_stk_size, &e_stk[0]); |
NarendraSingh | 26:506380fccce2 | 273 | Thread motionEventThread(osPriorityNormal, c_stk_size, &c_stk[0]); |
NarendraSingh | 26:506380fccce2 | 274 | |
NarendraSingh | 26:506380fccce2 | 275 | // create an event queue |
NarendraSingh | 26:506380fccce2 | 276 | EventQueue chechinEventQueue; |
NarendraSingh | 26:506380fccce2 | 277 | EventQueue motionEventQueue; |
NarendraSingh | 26:506380fccce2 | 278 | |
NarendraSingh | 26:506380fccce2 | 279 | Queue<int, 16> loraQueue; |
NarendraSingh | 26:506380fccce2 | 280 | int message2; |
NarendraSingh | 26:506380fccce2 | 281 | int message3; |
NarendraSingh | 26:506380fccce2 | 282 | |
NarendraSingh | 26:506380fccce2 | 283 | uint32_t count = 0; |
NarendraSingh | 26:506380fccce2 | 284 | |
NarendraSingh | 26:506380fccce2 | 285 | void checkinFunction() { |
NarendraSingh | 26:506380fccce2 | 286 | // this now runs in the context of checkinEventThread, instead of in the ISR |
NarendraSingh | 26:506380fccce2 | 287 | count++; |
NarendraSingh | 26:506380fccce2 | 288 | printf("Toggling LED 2!\r\n"); |
NarendraSingh | 26:506380fccce2 | 289 | for(int i = 0 ; i < 0xFFFFFF; i++); |
NarendraSingh | 26:506380fccce2 | 290 | led2 = !led2; |
NarendraSingh | 26:506380fccce2 | 291 | message2 = HEART_BEAT_PACKET_SENDING; |
NarendraSingh | 26:506380fccce2 | 292 | loraQueue.put(&message2); |
NarendraSingh | 26:506380fccce2 | 293 | printf("Toggled LED 2 count = %d \r\n", count); |
NarendraSingh | 26:506380fccce2 | 294 | } |
NarendraSingh | 26:506380fccce2 | 295 | |
NarendraSingh | 26:506380fccce2 | 296 | void motionFunction() { |
NarendraSingh | 26:506380fccce2 | 297 | // this now runs in the context of motionEventThread, instead of in the ISR |
NarendraSingh | 26:506380fccce2 | 298 | count++; |
NarendraSingh | 26:506380fccce2 | 299 | printf("Toggling LED 3!\r\n"); |
NarendraSingh | 26:506380fccce2 | 300 | for(int i = 0 ; i < 0xFFFFFF; i++); |
NarendraSingh | 26:506380fccce2 | 301 | led3 = !led3; |
NarendraSingh | 26:506380fccce2 | 302 | message3 = STATUS_PACKET_SENDING; |
NarendraSingh | 26:506380fccce2 | 303 | loraQueue.put(&message3); |
NarendraSingh | 26:506380fccce2 | 304 | printf("Toggled LED 3 count = %d \r\n", count); |
NarendraSingh | 26:506380fccce2 | 305 | } |
NarendraSingh | 26:506380fccce2 | 306 | |
NarendraSingh | 26:506380fccce2 | 307 | /** |
NarendraSingh | 26:506380fccce2 | 308 | * @brief thread a function |
NarendraSingh | 26:506380fccce2 | 309 | */ |
NarendraSingh | 26:506380fccce2 | 310 | static void loraSendThreadFunc(void const *buf) |
NarendraSingh | 26:506380fccce2 | 311 | { |
NarendraSingh | 26:506380fccce2 | 312 | uint32_t execs = 0; |
NarendraSingh | 26:506380fccce2 | 313 | int *message1; |
NarendraSingh | 26:506380fccce2 | 314 | osEvent evt; |
NarendraSingh | 26:506380fccce2 | 315 | pc_serial.printf("## started %s execution! ##\n\r", (char *)buf); |
NarendraSingh | 26:506380fccce2 | 316 | |
NarendraSingh | 26:506380fccce2 | 317 | for(;;) { |
NarendraSingh | 26:506380fccce2 | 318 | execs++; |
NarendraSingh | 26:506380fccce2 | 319 | /* adds dummy processing */ |
NarendraSingh | 26:506380fccce2 | 320 | for(int i = 0 ; i < 0xFFFFFF; i++); |
NarendraSingh | 26:506380fccce2 | 321 | evt = loraQueue.get(); |
NarendraSingh | 26:506380fccce2 | 322 | if (evt.status == osEventMessage) { |
NarendraSingh | 26:506380fccce2 | 323 | message1 = (int*)evt.value.p; |
NarendraSingh | 26:506380fccce2 | 324 | } |
NarendraSingh | 26:506380fccce2 | 325 | if( *message1 == HEART_BEAT_PACKET_SENDING) |
NarendraSingh | 26:506380fccce2 | 326 | Send_HeartBeat_Packet(); //call function to send heartbeat packet |
NarendraSingh | 26:506380fccce2 | 327 | else if( *message1 == STATUS_PACKET_SENDING) |
NarendraSingh | 26:506380fccce2 | 328 | Send_Vehicle_Status_Packet(); //call function to send heartbeat packet |
NarendraSingh | 26:506380fccce2 | 329 | |
NarendraSingh | 26:506380fccce2 | 330 | |
NarendraSingh | 26:506380fccce2 | 331 | pc_serial.printf("## %s executed %d times! p = %d ##\n\r", (char *)buf, execs, *message1); |
NarendraSingh | 26:506380fccce2 | 332 | led1 = !led1; |
NarendraSingh | 26:506380fccce2 | 333 | Thread::yield(); |
NarendraSingh | 26:506380fccce2 | 334 | } |
NarendraSingh | 26:506380fccce2 | 335 | } |
NarendraSingh | 26:506380fccce2 | 336 | |
NarendraSingh | 26:506380fccce2 | 337 | /** |
NarendraSingh | 26:506380fccce2 | 338 | * @brief main application loop |
NarendraSingh | 26:506380fccce2 | 339 | * |
NarendraSingh | 26:506380fccce2 | 340 | int main(void) |
NarendraSingh | 26:506380fccce2 | 341 | { |
NarendraSingh | 26:506380fccce2 | 342 | pc_serial.baud(115200); |
NarendraSingh | 26:506380fccce2 | 343 | loraSendThread.start(callback(loraSendThreadFunc, (void *)LORA_SEND_THREAD)); |
NarendraSingh | 26:506380fccce2 | 344 | //b_thread.start(callback(thread2, (void *)THREAD_B)); |
NarendraSingh | 26:506380fccce2 | 345 | /*c_thread.start(callback(thread3, (void *)THREAD_C));* |
NarendraSingh | 26:506380fccce2 | 346 | |
NarendraSingh | 26:506380fccce2 | 347 | checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 348 | motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 349 | |
NarendraSingh | 26:506380fccce2 | 350 | // wrap calls in queue.event to automatically defer to the queue's thread |
NarendraSingh | 26:506380fccce2 | 351 | checkinIntr.fall(chechinEventQueue.event(&checkinFunction)); |
NarendraSingh | 26:506380fccce2 | 352 | motionIntr.fall(motionEventQueue.event(&motionFunction)); |
NarendraSingh | 26:506380fccce2 | 353 | |
NarendraSingh | 26:506380fccce2 | 354 | |
NarendraSingh | 26:506380fccce2 | 355 | return 0; |
NarendraSingh | 26:506380fccce2 | 356 | } |
NarendraSingh | 26:506380fccce2 | 357 | */ |
NarendraSingh | 26:506380fccce2 | 358 | int main(void) |
NarendraSingh | 26:506380fccce2 | 359 | { |
NarendraSingh | 26:506380fccce2 | 360 | pc_serial.baud(115200); |
NarendraSingh | 26:506380fccce2 | 361 | loraSendThread.start(callback(loraSendThreadFunc, (void *)LORA_SEND_THREAD)); |
NarendraSingh | 26:506380fccce2 | 362 | //b_thread.start(callback(thread2, (void *)THREAD_B)); |
NarendraSingh | 26:506380fccce2 | 363 | /*c_thread.start(callback(thread3, (void *)THREAD_C));*/ |
NarendraSingh | 26:506380fccce2 | 364 | |
NarendraSingh | 26:506380fccce2 | 365 | checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 366 | motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 367 | |
NarendraSingh | 26:506380fccce2 | 368 | // wrap calls in queue.event to automatically defer to the queue's thread |
NarendraSingh | 26:506380fccce2 | 369 | checkinIntr.fall(chechinEventQueue.event(&checkinFunction)); |
NarendraSingh | 26:506380fccce2 | 370 | motionIntr.fall(motionEventQueue.event(&motionFunction)); |
NarendraSingh | 26:506380fccce2 | 371 | return 0; |
NarendraSingh | 26:506380fccce2 | 372 | } |
NarendraSingh | 26:506380fccce2 | 373 | /* |
NarendraSingh | 26:506380fccce2 | 374 | |
NarendraSingh | 16:7703b9d92326 | 375 | int main() |
NarendraSingh | 11:77e595130230 | 376 | { |
NarendraSingh | 17:758fb8454ab0 | 377 | pc1.baud(115200); |
NarendraSingh | 16:7703b9d92326 | 378 | BLE_RECEIVER_UART.baud(115200); |
NarendraSingh | 26:506380fccce2 | 379 | //pc1.printf("%s","Debugging started\n"); |
NarendraSingh | 26:506380fccce2 | 380 | pc_serial.printf("%s","Debugging started\n"); |
NarendraSingh | 26:506380fccce2 | 381 | loraSendThread.start(callback(loraSendThreadFunc, (void *)LORA_SEND_THREAD)); |
NarendraSingh | 26:506380fccce2 | 382 | //b_thread.start(callback(thread2, (void *)THREAD_B)); |
NarendraSingh | 26:506380fccce2 | 383 | /*c_thread.start(callback(thread3, (void *)THREAD_C));* |
NarendraSingh | 26:506380fccce2 | 384 | |
NarendraSingh | 26:506380fccce2 | 385 | checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 386 | motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever)); |
NarendraSingh | 26:506380fccce2 | 387 | |
NarendraSingh | 26:506380fccce2 | 388 | // wrap calls in queue.event to automatically defer to the queue's thread |
NarendraSingh | 26:506380fccce2 | 389 | checkinIntr.fall(chechinEventQueue.event(&checkinFunction)); |
NarendraSingh | 26:506380fccce2 | 390 | motionIntr.fall(motionEventQueue.event(&motionFunction)); |
NarendraSingh | 26:506380fccce2 | 391 | /* |
NarendraSingh | 26:506380fccce2 | 392 | |
NarendraSingh | 24:1063cfc311e5 | 393 | //BLE_RECEIVER_UART.attach(&BLE_Receiver_onDataRx, Serial::RxIrq); |
NarendraSingh | 11:77e595130230 | 394 | LORA_UART.attach(&Lora_onDataRx, Serial::RxIrq); |
NarendraSingh | 22:c2f034a13108 | 395 | CheckIn_Interrupt.fall(&OBD_Plug_IN_Interrupt); |
NarendraSingh | 22:c2f034a13108 | 396 | CheckIn_Interrupt.rise(&OBD_Plug_OUT_Interrupt); |
NarendraSingh | 24:1063cfc311e5 | 397 | if(Switch2) |
NarendraSingh | 24:1063cfc311e5 | 398 | { |
NarendraSingh | 24:1063cfc311e5 | 399 | } |
NarendraSingh | 24:1063cfc311e5 | 400 | Motion_Start_To_Stop.fall(&Start_To_Stop_Interrupt); |
NarendraSingh | 24:1063cfc311e5 | 401 | Motion_Stop_To_Start.fall(&Stop_To_Start_Interrupt); |
NarendraSingh | 24:1063cfc311e5 | 402 | Motion_Sudden_Jerk.fall(&Sudden_Jerk_Interrupt); |
NarendraSingh | 11:77e595130230 | 403 | //Create a thread to read vehicle data |
NarendraSingh | 11:77e595130230 | 404 | //Thread OBD_thread(OBD_Rcvd_Cmd_Processing_thread); |
NarendraSingh | 11:77e595130230 | 405 | |
NarendraSingh | 11:77e595130230 | 406 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 3.0); // call flip_Packet_Sending_Flag function every 5 seconds |
NarendraSingh | 11:77e595130230 | 407 | |
NarendraSingh | 11:77e595130230 | 408 | // OBD_PLUGIN_INTERRUPT_PIN.rise(&Enable_CheckIN_Packet_Sending); // call toggle function on the rising edge |
NarendraSingh | 11:77e595130230 | 409 | //led2_thread is executing concurrently with main at this point |
NarendraSingh | 22:c2f034a13108 | 410 | //inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge |
NarendraSingh | 19:886d50ecc718 | 411 | Initialize_Beacon_Module(); |
NarendraSingh | 24:1063cfc311e5 | 412 | // Send_Command_To_BLE_Receiver(SET_BEACON_VENDOR_ID); |
NarendraSingh | 24:1063cfc311e5 | 413 | // Send_Command_To_BLE_Receiver(SET_BEACON_MESSAGE_TYPE); |
NarendraSingh | 24:1063cfc311e5 | 414 | pc1.printf("\n%s","Transmitter MAC ID received\n"); |
NarendraSingh | 11:77e595130230 | 415 | Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here |
NarendraSingh | 26:506380fccce2 | 416 | * |
NarendraSingh | 16:7703b9d92326 | 417 | } |
NarendraSingh | 26:506380fccce2 | 418 | */ |
NarendraSingh | 23:688ee106c385 | 419 | void Send_Command_To_BLE_Receiver(const char* Command) |
NarendraSingh | 23:688ee106c385 | 420 | { |
NarendraSingh | 23:688ee106c385 | 421 | uint8 i; |
NarendraSingh | 23:688ee106c385 | 422 | for(i=0;i<12;i++) |
NarendraSingh | 23:688ee106c385 | 423 | BLE_RECEIVER_UART.putc(Command[i]); |
NarendraSingh | 23:688ee106c385 | 424 | } |
NarendraSingh | 16:7703b9d92326 | 425 | //Function to be called when Interrupt is genearted for motion sensing, checkin |
NarendraSingh | 16:7703b9d92326 | 426 | void Initialize_Packets_Sent_Count(void) |
NarendraSingh | 16:7703b9d92326 | 427 | { |
NarendraSingh | 11:77e595130230 | 428 | Motion_Packet_Sent_Count = 0x00; |
NarendraSingh | 11:77e595130230 | 429 | CheckIN_Packet_Sent_Count = 0x00; |
NarendraSingh | 16:7703b9d92326 | 430 | } |
NarendraSingh | 16:7703b9d92326 | 431 | |
NarendraSingh | 16:7703b9d92326 | 432 | uint8 Status_Packet_Wait_Count = 0; |
NarendraSingh | 16:7703b9d92326 | 433 | void Lora_Periodic_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 434 | { |
NarendraSingh | 24:1063cfc311e5 | 435 | pc1.printf("\nPeriodic packet sending intiialized\n"); |
NarendraSingh | 11:77e595130230 | 436 | Set_Up_Lora_Network_Configuration(); |
NarendraSingh | 16:7703b9d92326 | 437 | Initialize_lora_Packets(); |
NarendraSingh | 19:886d50ecc718 | 438 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 16:7703b9d92326 | 439 | while (true) { |
NarendraSingh | 16:7703b9d92326 | 440 | if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET) { //check if packet to be sent is Heartbeat packet |
NarendraSingh | 16:7703b9d92326 | 441 | if(Send_Lora_Packet_Flag) { //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 24:1063cfc311e5 | 442 | pc1.printf("\nStatus_Packet_Wait_Count %d\n",Status_Packet_Wait_Count); |
NarendraSingh | 12:6107b32b0729 | 443 | Status_Packet_Wait_Count++; |
NarendraSingh | 21:a5fb0ae94dc6 | 444 | if(Status_Packet_Wait_Count < 4) { |
NarendraSingh | 24:1063cfc311e5 | 445 | // Send_Command_To_BLE_Receiver(GET_RSSI); |
NarendraSingh | 24:1063cfc311e5 | 446 | //Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 24:1063cfc311e5 | 447 | pc1.printf("\nSending heartbeat packet\n"); //call function to send periodic motion packet |
NarendraSingh | 12:6107b32b0729 | 448 | Send_HeartBeat_Packet(); //call function to send heartbeat packet |
NarendraSingh | 24:1063cfc311e5 | 449 | pc.printf("\nSent HeartBeat Packet\n"); |
NarendraSingh | 24:1063cfc311e5 | 450 | /*AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 12:6107b32b0729 | 451 | while(AT_Response_Receive_Status) |
NarendraSingh | 12:6107b32b0729 | 452 | Get_Lora_Response(); |
NarendraSingh | 24:1063cfc311e5 | 453 | */Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 454 | pc1.printf("\nHeartbeat Packet Response Received\n"); |
NarendraSingh | 22:c2f034a13108 | 455 | } else { |
NarendraSingh | 20:f812f85cf97e | 456 | //Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 12:6107b32b0729 | 457 | Status_Packet_Wait_Count = 0; |
NarendraSingh | 24:1063cfc311e5 | 458 | pc1.printf("\nSending Vehicle status packets\n"); //call function to send periodic motion packet |
NarendraSingh | 13:8955f2e95021 | 459 | Send_Vehicle_Status_Packet(); //call function to send heartbeat packet |
NarendraSingh | 24:1063cfc311e5 | 460 | pc1.printf("\nSent Vehicle Status Packet\n"); |
NarendraSingh | 19:886d50ecc718 | 461 | //AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 21:a5fb0ae94dc6 | 462 | // while(AT_Response_Receive_Status) |
NarendraSingh | 19:886d50ecc718 | 463 | // Get_Lora_Response(); |
NarendraSingh | 12:6107b32b0729 | 464 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 465 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; |
NarendraSingh | 24:1063cfc311e5 | 466 | //pc1.printf("\nStatus Packet Response Received\n"); |
NarendraSingh | 12:6107b32b0729 | 467 | } |
NarendraSingh | 11:77e595130230 | 468 | } |
NarendraSingh | 16:7703b9d92326 | 469 | } else if(Packet_Type_To_Send == MOTION_TYPE_PACKET) { //check if packet to be sent is motion packet |
NarendraSingh | 16:7703b9d92326 | 470 | if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 21:a5fb0ae94dc6 | 471 | //Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 24:1063cfc311e5 | 472 | pc1.printf("\nSending Motion Packet\n"); //call function to send periodic motion packet |
NarendraSingh | 11:77e595130230 | 473 | Send_Motion_Packet(); |
NarendraSingh | 24:1063cfc311e5 | 474 | pc1.printf("\nSent Motion Packet\n"); //call function to send periodic motion packet |
NarendraSingh | 11:77e595130230 | 475 | AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 476 | while(AT_Response_Receive_Status) |
NarendraSingh | 11:77e595130230 | 477 | Get_Lora_Response(); |
NarendraSingh | 24:1063cfc311e5 | 478 | pc1.printf("\nMotion Packet Response Received\n"); |
NarendraSingh | 11:77e595130230 | 479 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 16:7703b9d92326 | 480 | if(Motion_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 24:1063cfc311e5 | 481 | pc1.printf("\nPacket Type Sending Changed to HeartBeat\n"); |
NarendraSingh | 11:77e595130230 | 482 | Motion_Packet_Sent_Count = 0; |
NarendraSingh | 11:77e595130230 | 483 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 16:7703b9d92326 | 484 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 485 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 10.0); //create new ticker |
NarendraSingh | 11:77e595130230 | 486 | } |
NarendraSingh | 16:7703b9d92326 | 487 | } |
NarendraSingh | 16:7703b9d92326 | 488 | } else if(Packet_Type_To_Send == CHECKIN_TYPE_PACKET) { //check if packet to be sent is Checkin packet |
NarendraSingh | 16:7703b9d92326 | 489 | if(Send_Lora_Packet_Flag) { //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period |
NarendraSingh | 19:886d50ecc718 | 490 | Send_Lora_Packet_Flag = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 491 | //pc1.printf("\nSent Beacon ID request\n"); |
NarendraSingh | 20:f812f85cf97e | 492 | // Send_RSSI_Request_Command(GET_RSSI); |
NarendraSingh | 19:886d50ecc718 | 493 | if(OBD_PlugIN_State) |
NarendraSingh | 19:886d50ecc718 | 494 | { |
NarendraSingh | 24:1063cfc311e5 | 495 | pc1.printf("\nSening Checkin Packet\n"); |
NarendraSingh | 19:886d50ecc718 | 496 | Send_CheckIN_Packet(); //call function to send periodic checkIn packet |
NarendraSingh | 24:1063cfc311e5 | 497 | pc1.printf("\nSent Checkin Packet\n"); |
NarendraSingh | 24:1063cfc311e5 | 498 | //Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID); |
NarendraSingh | 24:1063cfc311e5 | 499 | //wait(.2); |
NarendraSingh | 24:1063cfc311e5 | 500 | //Process_Beacon_Command_Received(SOFT_REBOOT1); |
NarendraSingh | 19:886d50ecc718 | 501 | } |
NarendraSingh | 19:886d50ecc718 | 502 | else |
NarendraSingh | 19:886d50ecc718 | 503 | { |
NarendraSingh | 24:1063cfc311e5 | 504 | pc1.printf("\nSending Checkout Packet\n"); |
NarendraSingh | 19:886d50ecc718 | 505 | Send_CheckOUT_Packet(); |
NarendraSingh | 24:1063cfc311e5 | 506 | pc1.printf("\nSent CheckOut Packet\n"); |
NarendraSingh | 24:1063cfc311e5 | 507 | //Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID2); |
NarendraSingh | 24:1063cfc311e5 | 508 | //wait(.2); |
NarendraSingh | 24:1063cfc311e5 | 509 | //Process_Beacon_Command_Received(SOFT_REBOOT1); |
NarendraSingh | 19:886d50ecc718 | 510 | } |
NarendraSingh | 22:c2f034a13108 | 511 | /* AT_Response_Receive_Status = FAILURE; |
NarendraSingh | 11:77e595130230 | 512 | while(AT_Response_Receive_Status) |
NarendraSingh | 22:c2f034a13108 | 513 | Get_Lora_Response();*/ |
NarendraSingh | 16:7703b9d92326 | 514 | if(CheckIN_Packet_Sent_Count >= 5) { //Stop Sending Motion Packets if after sending for 2 minute |
NarendraSingh | 24:1063cfc311e5 | 515 | pc1.printf("\nPacket Type Sending Changed to HeartBeat\n"); |
NarendraSingh | 20:f812f85cf97e | 516 | CheckIN_Packet_Sent_Count = 0; |
NarendraSingh | 20:f812f85cf97e | 517 | Packet_Type_To_Send = HEARTBEAT_TYPE_PACKET; //Set Packet type to send as heartbeat packet |
NarendraSingh | 20:f812f85cf97e | 518 | Initialize_Packets_Sent_Count(); |
NarendraSingh | 11:77e595130230 | 519 | } |
NarendraSingh | 11:77e595130230 | 520 | } |
NarendraSingh | 16:7703b9d92326 | 521 | } |
NarendraSingh | 16:7703b9d92326 | 522 | if(OBD_PlugInOut_IOC_Status) { //Check if Accelerometer Interrupt is generated |
NarendraSingh | 13:8955f2e95021 | 523 | //Enable_CheckIN_Packet_Sending(); |
NarendraSingh | 11:77e595130230 | 524 | //Get_Acceleration_Type(); |
NarendraSingh | 21:a5fb0ae94dc6 | 525 | // Process_Beacon_Command_Received(SET_VIRTUAL_PACKET_UUID); |
NarendraSingh | 21:a5fb0ae94dc6 | 526 | // Process_Beacon_Command_Received(SOFT_REBOOT1); |
NarendraSingh | 11:77e595130230 | 527 | OBD_PlugInOut_IOC_Status = FALSE; |
NarendraSingh | 13:8955f2e95021 | 528 | Packet_Type_To_Send = CHECKIN_TYPE_PACKET; |
NarendraSingh | 13:8955f2e95021 | 529 | Send_Lora_Packet_Flag = TRUE; |
NarendraSingh | 19:886d50ecc718 | 530 | CheckIN_Packet_Sent_Count = 0; |
NarendraSingh | 24:1063cfc311e5 | 531 | CheckIN_Lora_Packet.Sequence_No = 0x01; //Reset counter |
NarendraSingh | 24:1063cfc311e5 | 532 | CheckOUT_Packet.Sequence_No = 0x01; //Reset counter |
NarendraSingh | 16:7703b9d92326 | 533 | wait(1); //wait for 1sec to avoid detecting plugin debounce |
NarendraSingh | 11:77e595130230 | 534 | } |
NarendraSingh | 16:7703b9d92326 | 535 | if(Motion_Detect_Status) { //Check if Accelerometer Interrupt is generated |
NarendraSingh | 24:1063cfc311e5 | 536 | Motion_Lora_Packet.Sequence_No = 0x01; //Reset counter |
NarendraSingh | 24:1063cfc311e5 | 537 | Motion_Packet_Sent_Count = 0x00; //reset counetr |
NarendraSingh | 11:77e595130230 | 538 | Get_Acceleration_Type(); |
NarendraSingh | 11:77e595130230 | 539 | Motion_Detect_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 540 | Start_To_Stop_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 541 | Jerk_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 542 | Stop_To_Start_Status = FALSE; |
NarendraSingh | 24:1063cfc311e5 | 543 | wait(1); //wait for 1sec to avoid detecting switch debounce |
NarendraSingh | 11:77e595130230 | 544 | } |
NarendraSingh | 16:7703b9d92326 | 545 | } |
NarendraSingh | 11:77e595130230 | 546 | } |
NarendraSingh | 11:77e595130230 | 547 | |
NarendraSingh | 11:77e595130230 | 548 | void Get_Acceleration_Type(void) |
NarendraSingh | 11:77e595130230 | 549 | { |
NarendraSingh | 11:77e595130230 | 550 | Packet_Type_To_Send = MOTION_TYPE_PACKET; //whenever acceleration is detected change the packet type to be sent to motion type |
NarendraSingh | 16:7703b9d92326 | 551 | Lora_Packet_Sending_Ticker.detach(); //destroy ticker |
NarendraSingh | 11:77e595130230 | 552 | Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker, time inetrval value to be changed |
NarendraSingh | 11:77e595130230 | 553 | Send_Lora_Packet_Flag = 1; //set flag to send motion packet as soon as motion is detecetd |
NarendraSingh | 24:1063cfc311e5 | 554 | if(Start_To_Stop_Status) |
NarendraSingh | 24:1063cfc311e5 | 555 | Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_START_TO_STOP; //Read Type of motion deteceted |
NarendraSingh | 24:1063cfc311e5 | 556 | if(Stop_To_Start_Status) |
NarendraSingh | 24:1063cfc311e5 | 557 | Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_STOP_TO_START; //Read Type of motion deteceted |
NarendraSingh | 24:1063cfc311e5 | 558 | if(Jerk_Status) |
NarendraSingh | 24:1063cfc311e5 | 559 | Motion_Lora_Packet.Acceleration_Type = MOTION_TYPE_TAP; //Read Type of motion deteceted |
NarendraSingh | 11:77e595130230 | 560 | //write code to read acceleration value for every 10sec after any of the acceleration is found |
NarendraSingh | 11:77e595130230 | 561 | } |
NarendraSingh | 19:886d50ecc718 | 562 | |
NarendraSingh | 13:8955f2e95021 | 563 | /* |
NarendraSingh | 11:77e595130230 | 564 | //This function is used to enable checkin packet sending once OBD device is plugged in |
NarendraSingh | 16:7703b9d92326 | 565 | void Enable_CheckIN_Packet_Sending() |
NarendraSingh | 11:77e595130230 | 566 | { |
NarendraSingh | 12:6107b32b0729 | 567 | // Enable_CheckIN_Packet_Sending = TRUE; //set status to true |
NarendraSingh | 13:8955f2e95021 | 568 | if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE |
NarendraSingh | 11:77e595130230 | 569 | { |
NarendraSingh | 11:77e595130230 | 570 | 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 | 571 | { |
NarendraSingh | 11:77e595130230 | 572 | 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 | 573 | { |
NarendraSingh | 11:77e595130230 | 574 | Process_Beacon_Command_Received((Command_Received + 2)); |
NarendraSingh | 11:77e595130230 | 575 | } |
NarendraSingh | 11:77e595130230 | 576 | } |
NarendraSingh | 13:8955f2e95021 | 577 | } |
NarendraSingh | 15:a448e955b8f3 | 578 | } */ |
NarendraSingh | 15:a448e955b8f3 | 579 | |
NarendraSingh | 16:7703b9d92326 | 580 | void Send_RSSI_Request_Command(const char* Command_To_Send) |
NarendraSingh | 15:a448e955b8f3 | 581 | { |
NarendraSingh | 23:688ee106c385 | 582 | //BLE_RECEIVER_UART.printf(Command_To_Send); |
NarendraSingh | 16:7703b9d92326 | 583 | Get_Fixed_Beacon_RSSI(); |
NarendraSingh | 15:a448e955b8f3 | 584 | } |
NarendraSingh | 15:a448e955b8f3 | 585 | |
NarendraSingh | 16:7703b9d92326 | 586 | void Get_Fixed_Beacon_RSSI(void) |
NarendraSingh | 15:a448e955b8f3 | 587 | { |
NarendraSingh | 16:7703b9d92326 | 588 | uint8 i; |
NarendraSingh | 16:7703b9d92326 | 589 | uint8 Temp_Pos=0; |
NarendraSingh | 19:886d50ecc718 | 590 | /*Fixed_Beacon_Packet.Parking1_Beacon_ID[0] = 0xA0; |
NarendraSingh | 16:7703b9d92326 | 591 | Fixed_Beacon_Packet.Parking1_Beacon_ID[1] = 0xAC; |
NarendraSingh | 16:7703b9d92326 | 592 | Fixed_Beacon_Packet.Parking1_Beacon_ID[2] = 0x51; |
NarendraSingh | 16:7703b9d92326 | 593 | Fixed_Beacon_Packet.Parking1_Beacon_ID[3] = 0x3F; |
NarendraSingh | 16:7703b9d92326 | 594 | Fixed_Beacon_Packet.Parking1_Beacon_ID[4] = 0x91; |
NarendraSingh | 16:7703b9d92326 | 595 | Fixed_Beacon_Packet.Parking1_Beacon_ID[5] = 0xE0; |
NarendraSingh | 16:7703b9d92326 | 596 | Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = 0xC3; |
NarendraSingh | 16:7703b9d92326 | 597 | Fixed_Beacon_Packet.Parking2_Beacon_ID[0] = 0xDC; |
NarendraSingh | 16:7703b9d92326 | 598 | Fixed_Beacon_Packet.Parking2_Beacon_ID[1] = 0xA3; |
NarendraSingh | 16:7703b9d92326 | 599 | Fixed_Beacon_Packet.Parking2_Beacon_ID[2] = 0x6D; |
NarendraSingh | 16:7703b9d92326 | 600 | Fixed_Beacon_Packet.Parking2_Beacon_ID[3] = 0x30; |
NarendraSingh | 16:7703b9d92326 | 601 | Fixed_Beacon_Packet.Parking2_Beacon_ID[4] = 0x38; |
NarendraSingh | 16:7703b9d92326 | 602 | Fixed_Beacon_Packet.Parking2_Beacon_ID[5] = 0xDF; |
NarendraSingh | 16:7703b9d92326 | 603 | Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = 0xB4; |
NarendraSingh | 16:7703b9d92326 | 604 | Fixed_Beacon_Packet.Parking3_Beacon_ID[0] = 0x6C; |
NarendraSingh | 16:7703b9d92326 | 605 | Fixed_Beacon_Packet.Parking3_Beacon_ID[1] = 0x29; |
NarendraSingh | 16:7703b9d92326 | 606 | Fixed_Beacon_Packet.Parking3_Beacon_ID[2] = 0xB6; |
NarendraSingh | 16:7703b9d92326 | 607 | Fixed_Beacon_Packet.Parking3_Beacon_ID[3] = 0x27; |
NarendraSingh | 16:7703b9d92326 | 608 | Fixed_Beacon_Packet.Parking3_Beacon_ID[4] = 0x3A; |
NarendraSingh | 16:7703b9d92326 | 609 | Fixed_Beacon_Packet.Parking3_Beacon_ID[5] = 0xC9; |
NarendraSingh | 16:7703b9d92326 | 610 | Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = 0xB0; |
NarendraSingh | 19:886d50ecc718 | 611 | */ |
NarendraSingh | 24:1063cfc311e5 | 612 | pc1.printf("\nReading MACID from Receiver\n"); |
NarendraSingh | 17:758fb8454ab0 | 613 | while(true) |
NarendraSingh | 15:a448e955b8f3 | 614 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 615 | wait_ms(2); |
NarendraSingh | 21:a5fb0ae94dc6 | 616 | if(Temp_Pos <= BLE_RxBuffer_End_Pos) |
NarendraSingh | 15:a448e955b8f3 | 617 | { |
NarendraSingh | 17:758fb8454ab0 | 618 | if(BLE_Receiver_UART_RX_Buffer[Temp_Pos] == 0x0D) |
NarendraSingh | 16:7703b9d92326 | 619 | { |
NarendraSingh | 17:758fb8454ab0 | 620 | if(BLE_Receiver_UART_RX_Buffer[Temp_Pos+1] == 0x0A) |
NarendraSingh | 16:7703b9d92326 | 621 | { |
NarendraSingh | 16:7703b9d92326 | 622 | Temp_Pos = 2; |
NarendraSingh | 24:1063cfc311e5 | 623 | //pc1.printf("\nFixed Beacon1 MAC ID\n"); |
NarendraSingh | 16:7703b9d92326 | 624 | for(i=0; i<6; i++) |
NarendraSingh | 21:a5fb0ae94dc6 | 625 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 626 | Fixed_Beacon_Packet.Parking1_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 21:a5fb0ae94dc6 | 627 | pc1.putc(Fixed_Beacon_Packet.Parking1_Beacon_ID[i]); |
NarendraSingh | 21:a5fb0ae94dc6 | 628 | } |
NarendraSingh | 19:886d50ecc718 | 629 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 630 | Fixed_Beacon_Packet.Parking1_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 24:1063cfc311e5 | 631 | //pc1.printf("\nFixed Beacon2 MAC ID\n"); |
NarendraSingh | 16:7703b9d92326 | 632 | for(i=0; i<6; i++) |
NarendraSingh | 21:a5fb0ae94dc6 | 633 | { |
NarendraSingh | 16:7703b9d92326 | 634 | Fixed_Beacon_Packet.Parking2_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 21:a5fb0ae94dc6 | 635 | pc1.putc(Fixed_Beacon_Packet.Parking2_Beacon_ID[i]); |
NarendraSingh | 21:a5fb0ae94dc6 | 636 | } |
NarendraSingh | 19:886d50ecc718 | 637 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 638 | Fixed_Beacon_Packet.Parking2_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 24:1063cfc311e5 | 639 | //pc1.printf("\nFixed Beacon3 MAC ID\n"); |
NarendraSingh | 16:7703b9d92326 | 640 | for(i=0; i<6; i++) |
NarendraSingh | 21:a5fb0ae94dc6 | 641 | { |
NarendraSingh | 21:a5fb0ae94dc6 | 642 | Fixed_Beacon_Packet.Parking3_Beacon_ID[i] = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 21:a5fb0ae94dc6 | 643 | pc1.putc(Fixed_Beacon_Packet.Parking3_Beacon_ID[i]); |
NarendraSingh | 21:a5fb0ae94dc6 | 644 | } |
NarendraSingh | 19:886d50ecc718 | 645 | Temp_Pos+=2; |
NarendraSingh | 16:7703b9d92326 | 646 | Fixed_Beacon_Packet.Parking3_Beacon_Signal_Strength = BLE_Receiver_UART_RX_Buffer[Temp_Pos++]; |
NarendraSingh | 21:a5fb0ae94dc6 | 647 | BLE_RxBuffer_End_Pos = 0x00; //Start Next adat at 0th location |
NarendraSingh | 16:7703b9d92326 | 648 | break; |
NarendraSingh | 16:7703b9d92326 | 649 | } |
NarendraSingh | 16:7703b9d92326 | 650 | Temp_Pos++; |
NarendraSingh | 15:a448e955b8f3 | 651 | } |
NarendraSingh | 16:7703b9d92326 | 652 | else |
NarendraSingh | 16:7703b9d92326 | 653 | Temp_Pos++; |
NarendraSingh | 15:a448e955b8f3 | 654 | } |
NarendraSingh | 15:a448e955b8f3 | 655 | else |
NarendraSingh | 16:7703b9d92326 | 656 | Temp_Pos = 0x00; |
NarendraSingh | 19:886d50ecc718 | 657 | } |
NarendraSingh | 24:1063cfc311e5 | 658 | pc1.printf("\nReading MACID finished\n"); |
NarendraSingh | 15:a448e955b8f3 | 659 | } |