nRF51822 serves as the bridge between BLE central and MCU, which makes cental able to fetch photos from serial camera.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_blinky by
src/main.cpp@14:f62d55b7dead, 2016-04-07 (annotated)
- Committer:
- stormysun513
- Date:
- Thu Apr 07 16:54:50 2016 +0000
- Revision:
- 14:f62d55b7dead
- Parent:
- 13:65c9b7d667f6
- Child:
- 15:c099dd509203
Modify LED Mode and implement low power constraint
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stormysun513 | 12:7060e287bdae | 1 | #ifndef DEVICE_SERIAL_FC |
stormysun513 | 12:7060e287bdae | 2 | #define DEVICE_SERIAL_FC 1 |
stormysun513 | 12:7060e287bdae | 3 | #endif |
stormysun513 | 14:f62d55b7dead | 4 | |
stormysun513 | 13:65c9b7d667f6 | 5 | #define APPLICATION_TIMEOUT |
stormysun513 | 14:f62d55b7dead | 6 | //#define LOW_POWER_CONSTRAINT |
stormysun513 | 12:7060e287bdae | 7 | |
dan | 0:7dec7e9ac085 | 8 | #include "mbed.h" |
stormysun513 | 12:7060e287bdae | 9 | #include "ble/BLE.h" |
stormysun513 | 12:7060e287bdae | 10 | #include "ble/Gap.h" |
stormysun513 | 12:7060e287bdae | 11 | #include "ringBuffer.h" |
stormysun513 | 11:b3929de96933 | 12 | #include "application.h" |
stormysun513 | 11:b3929de96933 | 13 | |
stormysun513 | 14:f62d55b7dead | 14 | #define DEVICE_NAME "myble3" |
stormysun513 | 14:f62d55b7dead | 15 | |
stormysun513 | 12:7060e287bdae | 16 | #define SDA_PIN P0_8 |
stormysun513 | 12:7060e287bdae | 17 | #define SCL_PIN P0_9 |
stormysun513 | 12:7060e287bdae | 18 | #define UARTTX_PIN P0_18 |
stormysun513 | 12:7060e287bdae | 19 | #define UARTRX_PIN P0_17 |
stormysun513 | 12:7060e287bdae | 20 | #define POWER_LED_PIN P0_11 |
stormysun513 | 12:7060e287bdae | 21 | #define INSERTION_LED_PIN P0_10 |
stormysun513 | 12:7060e287bdae | 22 | #define ANI_BATTERY_VOLTAGE_PIN P0_2 |
stormysun513 | 12:7060e287bdae | 23 | #define ANI_USB_CHARGE_PIN P0_3 |
stormysun513 | 12:7060e287bdae | 24 | #define BTN_POWER_ON_PIN P0_4 |
stormysun513 | 12:7060e287bdae | 25 | #define ANI_SALIVA_VOLTAGE_PIN P0_5 |
stormysun513 | 14:f62d55b7dead | 26 | #define STM32_ENABLE_PIN P0_12 |
stormysun513 | 12:7060e287bdae | 27 | #define CHECK_BUTTON_TIMES 2 |
stormysun513 | 12:7060e287bdae | 28 | #define CHECK_BUTTON_VOLTAGE_LEVEL 0.05f |
stormysun513 | 12:7060e287bdae | 29 | |
stormysun513 | 14:f62d55b7dead | 30 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 31 | #define LOW_POWER_THERS 100 |
stormysun513 | 14:f62d55b7dead | 32 | #define LOW_POWER_THERS_MARGIN 2 |
stormysun513 | 14:f62d55b7dead | 33 | #define MAXIMUM_LOW_POWER_COUNTER 30 |
stormysun513 | 14:f62d55b7dead | 34 | #endif |
stormysun513 | 14:f62d55b7dead | 35 | |
stormysun513 | 11:b3929de96933 | 36 | #define UART_BAUDRATE 14400 |
stormysun513 | 11:b3929de96933 | 37 | #define MINIMUM_TICKER_PERIOD 2500 |
stormysun513 | 12:7060e287bdae | 38 | |
stormysun513 | 12:7060e287bdae | 39 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
stormysun513 | 12:7060e287bdae | 40 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
stormysun513 | 12:7060e287bdae | 41 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
stormysun513 | 12:7060e287bdae | 42 | |
stormysun513 | 12:7060e287bdae | 43 | #define BLE_GAP_CP_MIN_CONN_INTVL_MIN 0x0006 //Lowest mimimum connection interval permitted, in units of 1.25 ms, i.e. 7.5 ms. |
stormysun513 | 12:7060e287bdae | 44 | #define BLE_GAP_CP_MAX_CONN_INTVL_MAX 0x0C80 //Highest maximum connection interval permitted, in units of 1.25 ms, i.e. 4 s. |
stormysun513 | 12:7060e287bdae | 45 | #define BLE_GAP_CP_SLAVE_LATENCY_MAX 0x01F3 //Highest slave latency permitted, in connection events. |
stormysun513 | 12:7060e287bdae | 46 | #define BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN 0x000A //Lowest supervision timeout permitted, in units of 10 ms, i.e. 100 ms. |
stormysun513 | 12:7060e287bdae | 47 | #define BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX 0x0C80 //Highest supervision timeout permitted, in units of 10 ms, i.e. 32 s. |
stormysun513 | 12:7060e287bdae | 48 | |
stormysun513 | 11:b3929de96933 | 49 | #define BLE_BUF_LEN 20 |
stormysun513 | 12:7060e287bdae | 50 | #define BLE_RETRANSMISSION_BUF 18 |
stormysun513 | 12:7060e287bdae | 51 | #define DEVICE_INFO_PAYLOAD 5 |
stormysun513 | 12:7060e287bdae | 52 | #define SINGLE_UINT16_PAYLOAD 3 |
stormysun513 | 12:7060e287bdae | 53 | #define UART_DATA_PACKET_PAYLOAD 1 |
stormysun513 | 12:7060e287bdae | 54 | #define UART_PACKET_SIZE 111 |
stormysun513 | 12:7060e287bdae | 55 | |
stormysun513 | 12:7060e287bdae | 56 | typedef enum { |
stormysun513 | 12:7060e287bdae | 57 | SYS_POWERSAVING = 0, |
stormysun513 | 12:7060e287bdae | 58 | SYS_RELAXING_BUTTON, |
stormysun513 | 12:7060e287bdae | 59 | SYS_LOCKING_BUTTON |
stormysun513 | 12:7060e287bdae | 60 | }SystemStateTypeDef; |
stormysun513 | 12:7060e287bdae | 61 | |
stormysun513 | 12:7060e287bdae | 62 | typedef enum { |
stormysun513 | 12:7060e287bdae | 63 | BTN_IDLE = 0, |
stormysun513 | 12:7060e287bdae | 64 | BTN_CHANGE_ANALOG, |
stormysun513 | 12:7060e287bdae | 65 | BTN_CHECK_LONG_PRESS |
stormysun513 | 12:7060e287bdae | 66 | }ButtonStateTypeDef; |
stormysun513 | 12:7060e287bdae | 67 | |
stormysun513 | 12:7060e287bdae | 68 | typedef enum { |
stormysun513 | 12:7060e287bdae | 69 | STM32_IDLE = 0, //Cannot receive any byte from UARTRX |
stormysun513 | 12:7060e287bdae | 70 | STM32_CHECK_HEADER, //Check incoming header and forward to the client |
stormysun513 | 12:7060e287bdae | 71 | STM32_IMAGE_FORWARDING //Forward image data to the client |
stormysun513 | 12:7060e287bdae | 72 | }STM32StateTypeDef; |
stormysun513 | 12:7060e287bdae | 73 | |
stormysun513 | 12:7060e287bdae | 74 | typedef enum { |
stormysun513 | 12:7060e287bdae | 75 | SYS_HEADER_IMAGE_INFO = 0, |
stormysun513 | 12:7060e287bdae | 76 | SYS_HEADER_SALIVA_VOLTAGE, // Not used (for debug purpose) |
stormysun513 | 12:7060e287bdae | 77 | SYS_HEADER_DEVICE_INFO, // Not used (for debug purpose) |
stormysun513 | 12:7060e287bdae | 78 | SYS_HEADER_IMAGE_PACKET, |
stormysun513 | 12:7060e287bdae | 79 | SYS_HEADER_NO_SPECIFIC |
stormysun513 | 12:7060e287bdae | 80 | }HeaderTypeDef; |
stormysun513 | 12:7060e287bdae | 81 | |
stormysun513 | 12:7060e287bdae | 82 | typedef enum { |
stormysun513 | 12:7060e287bdae | 83 | UARTRX_CHECK_FIRST_PREAMBLE = 0, |
stormysun513 | 12:7060e287bdae | 84 | UARTRX_CHECK_SECOND_PREAMBLE, |
stormysun513 | 12:7060e287bdae | 85 | UARTRX_TRANSFERING_DATA |
stormysun513 | 12:7060e287bdae | 86 | }UARTRxStateTypeDef; |
stormysun513 | 12:7060e287bdae | 87 | |
stormysun513 | 12:7060e287bdae | 88 | typedef enum { |
stormysun513 | 12:7060e287bdae | 89 | BLE_SWITCH_NOTIFICATION_MODE = 0, // Enable/Disable device information |
stormysun513 | 12:7060e287bdae | 90 | BLE_CHANGE_DEVICE_ID, // Write ID to saliva cassette |
stormysun513 | 12:7060e287bdae | 91 | BLE_REQUEST_SALIVA_VOLTAGE, // Turn on saliva sampling periodically |
stormysun513 | 12:7060e287bdae | 92 | BLE_REQUEST_IMAGE_INFO, // Request image header |
stormysun513 | 12:7060e287bdae | 93 | BLE_REQUEST_IMAGE_BY_INDEX, // Request image data by uart packet IDs |
stormysun513 | 12:7060e287bdae | 94 | BLE_END_IMAGE_TRANSFER, // Directly terminate image transferring |
stormysun513 | 12:7060e287bdae | 95 | BLE_DEREQUEST_SALIVA_VOLTAGE, // Turn off saliva sampling periodically |
stormysun513 | 12:7060e287bdae | 96 | BLE_SHUTDOWN, // Shutdown device |
stormysun513 | 12:7060e287bdae | 97 | BLE_UNLOCK_BUTTON, // Unlock Button |
stormysun513 | 14:f62d55b7dead | 98 | BLE_UART_DEBUG, // Reserved for debug Value: 0x09 |
stormysun513 | 12:7060e287bdae | 99 | }BLECommandTypeDef; |
stormysun513 | 12:7060e287bdae | 100 | |
stormysun513 | 12:7060e287bdae | 101 | /* BLE Callback functions */ |
stormysun513 | 12:7060e287bdae | 102 | void bleInitCompleteCB(struct BLE::InitializationCompleteCallbackContext *context); |
stormysun513 | 12:7060e287bdae | 103 | void bleOnConnectionCB(const Gap::ConnectionCallbackParams_t *p_conn_param); |
stormysun513 | 12:7060e287bdae | 104 | void bleDisconnectionCB(const Gap::DisconnectionCallbackParams_t *params); |
stormysun513 | 12:7060e287bdae | 105 | void bleCharacteristicWrittenCB(const GattWriteCallbackParams *Handler); |
stormysun513 | 12:7060e287bdae | 106 | void bleReduceConnInterval(void); |
dan | 0:7dec7e9ac085 | 107 | |
stormysun513 | 11:b3929de96933 | 108 | void timerPeriodicalCB(void); |
stormysun513 | 14:f62d55b7dead | 109 | void timerFlashPowerLed(void); |
stormysun513 | 14:f62d55b7dead | 110 | void timerFlashInsertionLed(void); |
stormysun513 | 12:7060e287bdae | 111 | void timerCheckBuffer(void); |
stormysun513 | 12:7060e287bdae | 112 | void timerRoutineTasks(void); |
stormysun513 | 12:7060e287bdae | 113 | |
stormysun513 | 12:7060e287bdae | 114 | void uartTxTransmit(uint8_t* buf, uint16_t length); |
stormysun513 | 12:7060e287bdae | 115 | void uartRxCB(void); |
stormysun513 | 11:b3929de96933 | 116 | |
stormysun513 | 12:7060e287bdae | 117 | void buttonInterruptServiceRoutine(void); |
stormysun513 | 12:7060e287bdae | 118 | void buttonCheckLongPress(uint8_t counter); |
stormysun513 | 12:7060e287bdae | 119 | void buttonLongPressVerified(void); |
stormysun513 | 12:7060e287bdae | 120 | void changeButtonPinToAnalogIn(void); |
stormysun513 | 12:7060e287bdae | 121 | void changeButtonPinToInterrupt(void); |
stormysun513 | 12:7060e287bdae | 122 | void shutdownDevice(void); |
stormysun513 | 12:7060e287bdae | 123 | |
stormysun513 | 12:7060e287bdae | 124 | void resetSTM32StateType(void); |
stormysun513 | 12:7060e287bdae | 125 | void requestSalivaVoltage(void); |
stormysun513 | 12:7060e287bdae | 126 | void requestForImagePacket(uint16_t packetId); |
stormysun513 | 12:7060e287bdae | 127 | |
stormysun513 | 12:7060e287bdae | 128 | BLE ble; |
stormysun513 | 12:7060e287bdae | 129 | Gap::Handle_t mHandle = 0; |
stormysun513 | 11:b3929de96933 | 130 | Ticker ticker; |
stormysun513 | 12:7060e287bdae | 131 | Serial pc(UARTTX_PIN, UARTRX_PIN); |
stormysun513 | 12:7060e287bdae | 132 | I2C *pI2C = NULL; |
stormysun513 | 12:7060e287bdae | 133 | DigitalOut ledPower(POWER_LED_PIN); //P0_11 |
stormysun513 | 12:7060e287bdae | 134 | DigitalOut *pLedInsertion = NULL; //P0_10 Must be changed |
stormysun513 | 14:f62d55b7dead | 135 | DigitalOut uartEnable(STM32_ENABLE_PIN); //P0_12 |
stormysun513 | 12:7060e287bdae | 136 | InterruptIn *pButton = NULL; //P0_4 |
stormysun513 | 12:7060e287bdae | 137 | AnalogIn *pButtonAnalogIn = NULL; //P0_4 |
stormysun513 | 12:7060e287bdae | 138 | AnalogIn aniSalivaVolt(ANI_SALIVA_VOLTAGE_PIN); //P0_5 |
stormysun513 | 12:7060e287bdae | 139 | AnalogIn aniUSBChargeVolt(ANI_USB_CHARGE_PIN); |
stormysun513 | 12:7060e287bdae | 140 | AnalogIn aniBatterVolt(ANI_BATTERY_VOLTAGE_PIN); //P0_2 |
stormysun513 | 12:7060e287bdae | 141 | |
stormysun513 | 12:7060e287bdae | 142 | /* State variables for application status, UARTRX data parsing status, |
stormysun513 | 12:7060e287bdae | 143 | waiting header type and main loop trigger */ |
stormysun513 | 12:7060e287bdae | 144 | static SystemStateTypeDef systemState = SYS_RELAXING_BUTTON; |
stormysun513 | 12:7060e287bdae | 145 | static ButtonStateTypeDef buttonState = BTN_IDLE; |
stormysun513 | 12:7060e287bdae | 146 | static STM32StateTypeDef stm32State = STM32_IDLE; |
stormysun513 | 12:7060e287bdae | 147 | static UARTRxStateTypeDef uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 148 | static HeaderTypeDef waitHeader = SYS_HEADER_DEVICE_INFO; |
stormysun513 | 12:7060e287bdae | 149 | |
stormysun513 | 12:7060e287bdae | 150 | /* Variables to handle data transferring. */ |
stormysun513 | 12:7060e287bdae | 151 | static uint8_t packetNum = 0; |
stormysun513 | 12:7060e287bdae | 152 | static uint8_t lastPacketSize = 0; |
stormysun513 | 12:7060e287bdae | 153 | static uint8_t currentPacketIndex = 0; |
stormysun513 | 12:7060e287bdae | 154 | static uint8_t targetPayload = 0; |
stormysun513 | 12:7060e287bdae | 155 | static uint8_t alreadyTransferred = 0; |
stormysun513 | 12:7060e287bdae | 156 | static uint8_t waitCamera = 0; |
stormysun513 | 12:7060e287bdae | 157 | static const uint8_t MAX_WAIT_TARGET_PAYLOAD = 2; |
stormysun513 | 12:7060e287bdae | 158 | static bool isRetransmit = false; |
stormysun513 | 12:7060e287bdae | 159 | static uint8_t retransmitIndex = 0; |
stormysun513 | 12:7060e287bdae | 160 | static uint8_t retransmitSize = 0; |
stormysun513 | 12:7060e287bdae | 161 | static bool setImgPktPayloadByID = false; |
stormysun513 | 12:7060e287bdae | 162 | static bool directForward = false; |
stormysun513 | 11:b3929de96933 | 163 | |
stormysun513 | 12:7060e287bdae | 164 | static bool isCheckBuf = false; |
stormysun513 | 12:7060e287bdae | 165 | static bool isRoutine = false; |
stormysun513 | 12:7060e287bdae | 166 | static bool isEnterMainLoop = false; |
stormysun513 | 12:7060e287bdae | 167 | static bool infoNotification = false; |
stormysun513 | 12:7060e287bdae | 168 | static bool salivaPeriodically = false; |
stormysun513 | 12:7060e287bdae | 169 | |
stormysun513 | 12:7060e287bdae | 170 | static uint8_t checkButtonCounter = 0; |
stormysun513 | 12:7060e287bdae | 171 | |
stormysun513 | 14:f62d55b7dead | 172 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 173 | static bool isLowPower = false; |
stormysun513 | 14:f62d55b7dead | 174 | static uint8_t lowPowerCounter = 0; |
stormysun513 | 14:f62d55b7dead | 175 | #endif |
stormysun513 | 14:f62d55b7dead | 176 | |
stormysun513 | 12:7060e287bdae | 177 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 178 | static uint16_t disConnIdleCnt = 0; |
stormysun513 | 12:7060e287bdae | 179 | static uint16_t connIdleCnt = 0; |
stormysun513 | 14:f62d55b7dead | 180 | static const uint16_t MAX_DISCONN_IDLE = 300; |
stormysun513 | 14:f62d55b7dead | 181 | static const uint16_t MAX_CONN_IDLE = 300; |
stormysun513 | 12:7060e287bdae | 182 | #endif |
stormysun513 | 11:b3929de96933 | 183 | |
stormysun513 | 12:7060e287bdae | 184 | static RingBuffer ringBuffer; |
stormysun513 | 12:7060e287bdae | 185 | static const uint8_t preamble[] = {0xFF, 0x7F}; |
stormysun513 | 12:7060e287bdae | 186 | uint8_t uartTxPayload[BLE_BUF_LEN] = {0,}; |
stormysun513 | 12:7060e287bdae | 187 | uint8_t uartRxPayload[BLE_BUF_LEN] = {0,}; |
stormysun513 | 12:7060e287bdae | 188 | uint8_t retransmitIndices[BLE_RETRANSMISSION_BUF] = {0,}; |
stormysun513 | 12:7060e287bdae | 189 | |
stormysun513 | 12:7060e287bdae | 190 | // The Nordic UART Service |
stormysun513 | 12:7060e287bdae | 191 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
stormysun513 | 12:7060e287bdae | 192 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
stormysun513 | 12:7060e287bdae | 193 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
stormysun513 | 12:7060e287bdae | 194 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
stormysun513 | 12:7060e287bdae | 195 | |
stormysun513 | 12:7060e287bdae | 196 | /* GATTs */ |
stormysun513 | 12:7060e287bdae | 197 | GattCharacteristic txCharacteristic (uart_tx_uuid, uartTxPayload, 1, BLE_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
stormysun513 | 12:7060e287bdae | 198 | GattCharacteristic rxCharacteristic (uart_rx_uuid, uartRxPayload, 1, BLE_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
stormysun513 | 12:7060e287bdae | 199 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
stormysun513 | 12:7060e287bdae | 200 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
dan | 0:7dec7e9ac085 | 201 | |
RedBearLab | 10:9e24f9f4ac47 | 202 | int main() |
RedBearLab | 10:9e24f9f4ac47 | 203 | { |
stormysun513 | 12:7060e287bdae | 204 | pc.baud(UART_BAUDRATE); |
stormysun513 | 12:7060e287bdae | 205 | pc.attach(uartRxCB , pc.RxIrq); |
stormysun513 | 12:7060e287bdae | 206 | pc.set_flow_control(pc.Disabled); |
stormysun513 | 12:7060e287bdae | 207 | RingBuffer_init(&ringBuffer); |
stormysun513 | 14:f62d55b7dead | 208 | uartEnable = 1; |
stormysun513 | 12:7060e287bdae | 209 | |
stormysun513 | 12:7060e287bdae | 210 | pI2C = new I2C(SDA_PIN, SCL_PIN); |
stormysun513 | 12:7060e287bdae | 211 | pLedInsertion = new DigitalOut(INSERTION_LED_PIN); |
stormysun513 | 14:f62d55b7dead | 212 | pLedInsertion->write(1); |
stormysun513 | 14:f62d55b7dead | 213 | ledPower = 0; |
stormysun513 | 12:7060e287bdae | 214 | |
stormysun513 | 11:b3929de96933 | 215 | ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD); |
stormysun513 | 12:7060e287bdae | 216 | pButton = new InterruptIn(BTN_POWER_ON_PIN); |
stormysun513 | 12:7060e287bdae | 217 | pButton->fall(buttonInterruptServiceRoutine); |
stormysun513 | 12:7060e287bdae | 218 | |
stormysun513 | 12:7060e287bdae | 219 | ble.init(bleInitCompleteCB); |
stormysun513 | 12:7060e287bdae | 220 | ble.onConnection(bleOnConnectionCB); |
stormysun513 | 12:7060e287bdae | 221 | ble.onDisconnection(bleDisconnectionCB); |
stormysun513 | 12:7060e287bdae | 222 | ble.onDataWritten(bleCharacteristicWrittenCB); |
stormysun513 | 14:f62d55b7dead | 223 | |
stormysun513 | 14:f62d55b7dead | 224 | // Adjust interrupt priority |
stormysun513 | 12:7060e287bdae | 225 | // NVIC_SetPriority(RADIO_IRQn, 0); |
stormysun513 | 12:7060e287bdae | 226 | // NVIC_SetPriority(UART0_IRQn, 1); |
stormysun513 | 12:7060e287bdae | 227 | // NVIC_SetPriority(RTC1_IRQn, 2); |
stormysun513 | 12:7060e287bdae | 228 | |
stormysun513 | 12:7060e287bdae | 229 | // setup advertising // Restricted to 31 bytes |
stormysun513 | 12:7060e287bdae | 230 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
stormysun513 | 12:7060e287bdae | 231 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
stormysun513 | 12:7060e287bdae | 232 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"BLETest", sizeof("BLETest") - 1); |
stormysun513 | 12:7060e287bdae | 233 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
stormysun513 | 12:7060e287bdae | 234 | ble.gap().setDeviceName("BLETest"); |
stormysun513 | 12:7060e287bdae | 235 | |
stormysun513 | 14:f62d55b7dead | 236 | ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
stormysun513 | 12:7060e287bdae | 237 | ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. |
stormysun513 | 12:7060e287bdae | 238 | ble.addService(uartService); |
stormysun513 | 12:7060e287bdae | 239 | |
stormysun513 | 12:7060e287bdae | 240 | ble.startAdvertising(); |
stormysun513 | 11:b3929de96933 | 241 | |
stormysun513 | 11:b3929de96933 | 242 | while(true){ |
stormysun513 | 12:7060e287bdae | 243 | |
stormysun513 | 14:f62d55b7dead | 244 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 245 | if(lowPowerCounter > MAXIMUM_LOW_POWER_COUNTER){ |
stormysun513 | 14:f62d55b7dead | 246 | shutdownDevice(); |
stormysun513 | 14:f62d55b7dead | 247 | } |
stormysun513 | 14:f62d55b7dead | 248 | #endif |
stormysun513 | 14:f62d55b7dead | 249 | |
stormysun513 | 12:7060e287bdae | 250 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 251 | if(disConnIdleCnt >= MAX_DISCONN_IDLE && systemState == SYS_RELAXING_BUTTON){ |
stormysun513 | 12:7060e287bdae | 252 | shutdownDevice(); |
stormysun513 | 12:7060e287bdae | 253 | } |
stormysun513 | 12:7060e287bdae | 254 | else if(connIdleCnt >= MAX_CONN_IDLE){ |
stormysun513 | 13:65c9b7d667f6 | 255 | if(mHandle != 0) |
stormysun513 | 13:65c9b7d667f6 | 256 | ble.disconnect(mHandle, Gap::CONNECTION_TIMEOUT); |
stormysun513 | 12:7060e287bdae | 257 | systemState = SYS_RELAXING_BUTTON; |
stormysun513 | 12:7060e287bdae | 258 | } |
stormysun513 | 12:7060e287bdae | 259 | #endif |
stormysun513 | 12:7060e287bdae | 260 | |
stormysun513 | 12:7060e287bdae | 261 | switch (systemState) { |
stormysun513 | 12:7060e287bdae | 262 | case SYS_POWERSAVING: |
stormysun513 | 12:7060e287bdae | 263 | case SYS_RELAXING_BUTTON: |
stormysun513 | 12:7060e287bdae | 264 | switch (buttonState) { |
stormysun513 | 12:7060e287bdae | 265 | case BTN_CHANGE_ANALOG: |
stormysun513 | 12:7060e287bdae | 266 | changeButtonPinToAnalogIn(); |
stormysun513 | 12:7060e287bdae | 267 | break; |
stormysun513 | 12:7060e287bdae | 268 | case BTN_CHECK_LONG_PRESS: |
stormysun513 | 12:7060e287bdae | 269 | buttonCheckLongPress(checkButtonCounter); |
stormysun513 | 12:7060e287bdae | 270 | break; |
stormysun513 | 12:7060e287bdae | 271 | case BTN_IDLE: |
stormysun513 | 12:7060e287bdae | 272 | default: |
stormysun513 | 12:7060e287bdae | 273 | break; |
stormysun513 | 12:7060e287bdae | 274 | } |
stormysun513 | 12:7060e287bdae | 275 | break; |
stormysun513 | 12:7060e287bdae | 276 | case SYS_LOCKING_BUTTON: |
stormysun513 | 12:7060e287bdae | 277 | buttonState = BTN_IDLE; |
stormysun513 | 12:7060e287bdae | 278 | break; |
stormysun513 | 12:7060e287bdae | 279 | } |
stormysun513 | 12:7060e287bdae | 280 | |
stormysun513 | 12:7060e287bdae | 281 | if(isCheckBuf){ |
stormysun513 | 12:7060e287bdae | 282 | isCheckBuf = false; |
stormysun513 | 12:7060e287bdae | 283 | timerCheckBuffer(); |
stormysun513 | 12:7060e287bdae | 284 | } |
stormysun513 | 12:7060e287bdae | 285 | |
stormysun513 | 12:7060e287bdae | 286 | if(isRoutine){ |
stormysun513 | 12:7060e287bdae | 287 | isRoutine = false; |
stormysun513 | 12:7060e287bdae | 288 | timerRoutineTasks(); |
stormysun513 | 12:7060e287bdae | 289 | } |
stormysun513 | 12:7060e287bdae | 290 | |
stormysun513 | 12:7060e287bdae | 291 | if( isEnterMainLoop ){ |
stormysun513 | 12:7060e287bdae | 292 | switch (stm32State){ |
stormysun513 | 12:7060e287bdae | 293 | case STM32_IMAGE_FORWARDING:{ |
stormysun513 | 12:7060e287bdae | 294 | if (setImgPktPayloadByID == false){ |
stormysun513 | 12:7060e287bdae | 295 | uint8_t check = 0; |
stormysun513 | 12:7060e287bdae | 296 | RingBuffer_read(&ringBuffer, &check, 1); |
stormysun513 | 12:7060e287bdae | 297 | HeaderTypeDef header = static_cast<HeaderTypeDef>(check); |
stormysun513 | 12:7060e287bdae | 298 | if (header == SYS_HEADER_IMAGE_PACKET){ |
stormysun513 | 12:7060e287bdae | 299 | if (currentPacketIndex != (packetNum-1)) |
stormysun513 | 12:7060e287bdae | 300 | targetPayload = UART_PACKET_SIZE + 3; |
stormysun513 | 12:7060e287bdae | 301 | else |
stormysun513 | 12:7060e287bdae | 302 | targetPayload = lastPacketSize + 3; |
stormysun513 | 12:7060e287bdae | 303 | |
stormysun513 | 12:7060e287bdae | 304 | alreadyTransferred = 0; |
stormysun513 | 12:7060e287bdae | 305 | setImgPktPayloadByID = true; |
stormysun513 | 12:7060e287bdae | 306 | waitCamera = 0; |
stormysun513 | 12:7060e287bdae | 307 | } |
stormysun513 | 12:7060e287bdae | 308 | } |
stormysun513 | 12:7060e287bdae | 309 | else { |
stormysun513 | 12:7060e287bdae | 310 | uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer); |
stormysun513 | 12:7060e287bdae | 311 | while((numBytes >= (BLE_BUF_LEN-1)) || (directForward == true)){ |
stormysun513 | 12:7060e287bdae | 312 | waitCamera = 0; |
stormysun513 | 12:7060e287bdae | 313 | uint16_t diff = numBytes; |
stormysun513 | 12:7060e287bdae | 314 | if(diff > (BLE_BUF_LEN-1)) |
stormysun513 | 12:7060e287bdae | 315 | diff = (BLE_BUF_LEN-1); |
stormysun513 | 12:7060e287bdae | 316 | uartRxPayload[0] = static_cast<uint8_t>(SYS_HEADER_IMAGE_PACKET); |
stormysun513 | 12:7060e287bdae | 317 | |
stormysun513 | 12:7060e287bdae | 318 | RingBuffer_read(&ringBuffer, uartRxPayload+1, diff); |
stormysun513 | 12:7060e287bdae | 319 | |
stormysun513 | 12:7060e287bdae | 320 | if(mHandle != 0){ |
stormysun513 | 12:7060e287bdae | 321 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), uartRxPayload, diff+1); |
stormysun513 | 12:7060e287bdae | 322 | alreadyTransferred += diff; |
stormysun513 | 12:7060e287bdae | 323 | numBytes -= diff; |
stormysun513 | 12:7060e287bdae | 324 | } |
stormysun513 | 12:7060e287bdae | 325 | |
stormysun513 | 12:7060e287bdae | 326 | if(directForward == true || alreadyTransferred >= targetPayload){ |
stormysun513 | 12:7060e287bdae | 327 | __disable_irq(); // Disable Interrupts |
stormysun513 | 12:7060e287bdae | 328 | setImgPktPayloadByID = false; |
stormysun513 | 12:7060e287bdae | 329 | __enable_irq(); |
stormysun513 | 12:7060e287bdae | 330 | directForward = false; |
stormysun513 | 12:7060e287bdae | 331 | RingBuffer_init(&ringBuffer); |
stormysun513 | 12:7060e287bdae | 332 | |
stormysun513 | 12:7060e287bdae | 333 | if(isRetransmit == false){ |
stormysun513 | 12:7060e287bdae | 334 | currentPacketIndex++; |
stormysun513 | 12:7060e287bdae | 335 | } |
stormysun513 | 12:7060e287bdae | 336 | else{ |
stormysun513 | 12:7060e287bdae | 337 | retransmitIndex++; |
stormysun513 | 12:7060e287bdae | 338 | if(retransmitIndex >= retransmitSize){ |
stormysun513 | 12:7060e287bdae | 339 | retransmitSize = 0; |
stormysun513 | 12:7060e287bdae | 340 | resetSTM32StateType(); |
stormysun513 | 12:7060e287bdae | 341 | break; |
stormysun513 | 12:7060e287bdae | 342 | } |
stormysun513 | 12:7060e287bdae | 343 | else{ |
stormysun513 | 12:7060e287bdae | 344 | currentPacketIndex = retransmitIndices[retransmitIndex]; |
stormysun513 | 12:7060e287bdae | 345 | } |
stormysun513 | 12:7060e287bdae | 346 | } |
stormysun513 | 12:7060e287bdae | 347 | |
stormysun513 | 12:7060e287bdae | 348 | if(currentPacketIndex == packetNum && isRetransmit == false){ |
stormysun513 | 12:7060e287bdae | 349 | /* TODO retransmission */ |
stormysun513 | 12:7060e287bdae | 350 | isRetransmit = true; |
stormysun513 | 12:7060e287bdae | 351 | if(retransmitSize > 0){ |
stormysun513 | 12:7060e287bdae | 352 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 353 | retransmitIndex = 0; |
stormysun513 | 12:7060e287bdae | 354 | currentPacketIndex = retransmitIndices[retransmitIndex]; |
stormysun513 | 12:7060e287bdae | 355 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 356 | } |
stormysun513 | 12:7060e287bdae | 357 | else{ |
stormysun513 | 12:7060e287bdae | 358 | resetSTM32StateType(); |
stormysun513 | 12:7060e287bdae | 359 | } |
stormysun513 | 12:7060e287bdae | 360 | } |
stormysun513 | 12:7060e287bdae | 361 | else{ |
stormysun513 | 12:7060e287bdae | 362 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 363 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 364 | } |
stormysun513 | 12:7060e287bdae | 365 | break; |
stormysun513 | 12:7060e287bdae | 366 | } |
stormysun513 | 12:7060e287bdae | 367 | } |
stormysun513 | 12:7060e287bdae | 368 | } |
stormysun513 | 12:7060e287bdae | 369 | break; |
stormysun513 | 12:7060e287bdae | 370 | } |
stormysun513 | 12:7060e287bdae | 371 | case STM32_CHECK_HEADER:{ |
stormysun513 | 12:7060e287bdae | 372 | uint8_t buf[BLE_BUF_LEN] = {0,}; |
stormysun513 | 12:7060e287bdae | 373 | HeaderTypeDef header; |
stormysun513 | 12:7060e287bdae | 374 | switch (waitHeader) { |
stormysun513 | 12:7060e287bdae | 375 | /* Image info */ |
stormysun513 | 12:7060e287bdae | 376 | case SYS_HEADER_IMAGE_INFO: |
stormysun513 | 12:7060e287bdae | 377 | RingBuffer_read(&ringBuffer, buf, 1); |
stormysun513 | 12:7060e287bdae | 378 | header = static_cast<HeaderTypeDef>(buf[0]); |
stormysun513 | 12:7060e287bdae | 379 | if (header == SYS_HEADER_IMAGE_INFO){ |
stormysun513 | 12:7060e287bdae | 380 | RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1); |
stormysun513 | 12:7060e287bdae | 381 | if(mHandle != 0) |
stormysun513 | 12:7060e287bdae | 382 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD); |
stormysun513 | 12:7060e287bdae | 383 | |
stormysun513 | 12:7060e287bdae | 384 | uint16_t totalDataLength = buf[1]*256 + buf[2]; |
stormysun513 | 12:7060e287bdae | 385 | packetNum = totalDataLength/UART_PACKET_SIZE; |
stormysun513 | 12:7060e287bdae | 386 | lastPacketSize = totalDataLength%UART_PACKET_SIZE; |
stormysun513 | 12:7060e287bdae | 387 | if(totalDataLength%UART_PACKET_SIZE != 0) |
stormysun513 | 12:7060e287bdae | 388 | packetNum++; |
stormysun513 | 12:7060e287bdae | 389 | else |
stormysun513 | 12:7060e287bdae | 390 | lastPacketSize = UART_PACKET_SIZE; |
stormysun513 | 12:7060e287bdae | 391 | |
stormysun513 | 12:7060e287bdae | 392 | currentPacketIndex = 0; |
stormysun513 | 12:7060e287bdae | 393 | resetSTM32StateType(); |
stormysun513 | 12:7060e287bdae | 394 | } |
stormysun513 | 12:7060e287bdae | 395 | break; |
stormysun513 | 12:7060e287bdae | 396 | case SYS_HEADER_SALIVA_VOLTAGE: |
stormysun513 | 12:7060e287bdae | 397 | RingBuffer_read(&ringBuffer, buf, 1); |
stormysun513 | 12:7060e287bdae | 398 | header = static_cast<HeaderTypeDef>(buf[0]); |
stormysun513 | 12:7060e287bdae | 399 | if (header == SYS_HEADER_SALIVA_VOLTAGE){ |
stormysun513 | 12:7060e287bdae | 400 | RingBuffer_read(&ringBuffer, buf+1, SINGLE_UINT16_PAYLOAD-1); |
stormysun513 | 12:7060e287bdae | 401 | if(mHandle != 0) |
stormysun513 | 12:7060e287bdae | 402 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD); |
stormysun513 | 12:7060e287bdae | 403 | |
stormysun513 | 12:7060e287bdae | 404 | stm32State = STM32_CHECK_HEADER; |
stormysun513 | 12:7060e287bdae | 405 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 406 | } |
stormysun513 | 12:7060e287bdae | 407 | break; |
stormysun513 | 12:7060e287bdae | 408 | case SYS_HEADER_NO_SPECIFIC: |
stormysun513 | 12:7060e287bdae | 409 | default: |
stormysun513 | 12:7060e287bdae | 410 | break; |
stormysun513 | 12:7060e287bdae | 411 | } |
stormysun513 | 12:7060e287bdae | 412 | break; |
stormysun513 | 12:7060e287bdae | 413 | } |
stormysun513 | 12:7060e287bdae | 414 | case STM32_IDLE: |
stormysun513 | 12:7060e287bdae | 415 | default: |
stormysun513 | 12:7060e287bdae | 416 | break; |
stormysun513 | 12:7060e287bdae | 417 | } |
stormysun513 | 12:7060e287bdae | 418 | isEnterMainLoop = false; |
stormysun513 | 12:7060e287bdae | 419 | } |
stormysun513 | 12:7060e287bdae | 420 | ble.waitForEvent(); |
stevep | 4:81cea7a352b0 | 421 | } |
dan | 0:7dec7e9ac085 | 422 | } |
stormysun513 | 11:b3929de96933 | 423 | |
stormysun513 | 12:7060e287bdae | 424 | void bleInitCompleteCB(struct BLE::InitializationCompleteCallbackContext *context){ |
stormysun513 | 12:7060e287bdae | 425 | if (context->error != BLE_ERROR_NONE) |
stormysun513 | 12:7060e287bdae | 426 | return; |
stormysun513 | 12:7060e287bdae | 427 | |
stormysun513 | 12:7060e287bdae | 428 | if (context->ble.getInstanceID() == BLE::DEFAULT_INSTANCE){ |
stormysun513 | 12:7060e287bdae | 429 | /* use the BLE instance */ |
stormysun513 | 12:7060e287bdae | 430 | return; |
stormysun513 | 12:7060e287bdae | 431 | } |
stormysun513 | 12:7060e287bdae | 432 | } |
stormysun513 | 12:7060e287bdae | 433 | |
stormysun513 | 12:7060e287bdae | 434 | void bleOnConnectionCB(const Gap::ConnectionCallbackParams_t *p_conn_param){ |
stormysun513 | 12:7060e287bdae | 435 | mHandle = p_conn_param->handle; |
stormysun513 | 12:7060e287bdae | 436 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 437 | connIdleCnt = 0; |
stormysun513 | 12:7060e287bdae | 438 | #endif |
stormysun513 | 12:7060e287bdae | 439 | bleReduceConnInterval(); |
stormysun513 | 12:7060e287bdae | 440 | } |
stormysun513 | 12:7060e287bdae | 441 | |
stormysun513 | 12:7060e287bdae | 442 | void bleDisconnectionCB(const Gap::DisconnectionCallbackParams_t *params){ |
stormysun513 | 12:7060e287bdae | 443 | mHandle = 0; |
stormysun513 | 12:7060e287bdae | 444 | if(systemState != SYS_POWERSAVING){ |
stormysun513 | 12:7060e287bdae | 445 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 446 | disConnIdleCnt = 0; |
stormysun513 | 12:7060e287bdae | 447 | #endif |
stormysun513 | 12:7060e287bdae | 448 | ble.startAdvertising(); |
stormysun513 | 12:7060e287bdae | 449 | } |
stormysun513 | 12:7060e287bdae | 450 | } |
stormysun513 | 12:7060e287bdae | 451 | |
stormysun513 | 12:7060e287bdae | 452 | void bleCharacteristicWrittenCB(const GattWriteCallbackParams *Handler){ |
stormysun513 | 12:7060e287bdae | 453 | uint16_t bleBytesRead; |
stormysun513 | 12:7060e287bdae | 454 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
stormysun513 | 12:7060e287bdae | 455 | { |
stormysun513 | 12:7060e287bdae | 456 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 457 | connIdleCnt = 0; |
stormysun513 | 12:7060e287bdae | 458 | #endif |
stormysun513 | 12:7060e287bdae | 459 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), uartTxPayload, &bleBytesRead); |
stormysun513 | 12:7060e287bdae | 460 | if(bleBytesRead == 0) |
stormysun513 | 12:7060e287bdae | 461 | return; |
stormysun513 | 12:7060e287bdae | 462 | |
stormysun513 | 12:7060e287bdae | 463 | BLECommandTypeDef command = static_cast<BLECommandTypeDef>(uartTxPayload[0]); |
stormysun513 | 12:7060e287bdae | 464 | bool isUARTCommand = false; |
stormysun513 | 12:7060e287bdae | 465 | switch(command){ |
stormysun513 | 12:7060e287bdae | 466 | case BLE_SWITCH_NOTIFICATION_MODE: |
stormysun513 | 12:7060e287bdae | 467 | infoNotification = !infoNotification; |
stormysun513 | 12:7060e287bdae | 468 | break; |
stormysun513 | 12:7060e287bdae | 469 | case BLE_REQUEST_SALIVA_VOLTAGE: |
stormysun513 | 12:7060e287bdae | 470 | waitHeader = SYS_HEADER_SALIVA_VOLTAGE; |
stormysun513 | 12:7060e287bdae | 471 | salivaPeriodically = true; |
stormysun513 | 12:7060e287bdae | 472 | systemState = SYS_LOCKING_BUTTON; // Lock Begin |
stormysun513 | 12:7060e287bdae | 473 | break; |
stormysun513 | 12:7060e287bdae | 474 | case BLE_DEREQUEST_SALIVA_VOLTAGE: |
stormysun513 | 12:7060e287bdae | 475 | salivaPeriodically = false; |
stormysun513 | 12:7060e287bdae | 476 | break; |
stormysun513 | 12:7060e287bdae | 477 | case BLE_REQUEST_IMAGE_INFO: |
stormysun513 | 12:7060e287bdae | 478 | waitHeader = SYS_HEADER_IMAGE_INFO; |
stormysun513 | 12:7060e287bdae | 479 | isUARTCommand = true; |
stormysun513 | 12:7060e287bdae | 480 | salivaPeriodically = false; |
stormysun513 | 12:7060e287bdae | 481 | break; |
stormysun513 | 12:7060e287bdae | 482 | case BLE_END_IMAGE_TRANSFER: |
stormysun513 | 12:7060e287bdae | 483 | isRetransmit = false; |
stormysun513 | 12:7060e287bdae | 484 | packetNum = 0; |
stormysun513 | 12:7060e287bdae | 485 | resetSTM32StateType(); |
stormysun513 | 12:7060e287bdae | 486 | break; |
stormysun513 | 12:7060e287bdae | 487 | case BLE_SHUTDOWN: |
stormysun513 | 12:7060e287bdae | 488 | changeButtonPinToInterrupt(); |
stormysun513 | 12:7060e287bdae | 489 | shutdownDevice(); |
stormysun513 | 12:7060e287bdae | 490 | break; |
stormysun513 | 12:7060e287bdae | 491 | case BLE_UNLOCK_BUTTON: |
stormysun513 | 12:7060e287bdae | 492 | systemState = SYS_RELAXING_BUTTON; |
stormysun513 | 12:7060e287bdae | 493 | break; |
stormysun513 | 12:7060e287bdae | 494 | case BLE_UART_DEBUG:{ |
stormysun513 | 12:7060e287bdae | 495 | uint8_t debugHeader = 0xF1; |
stormysun513 | 12:7060e287bdae | 496 | uartTxTransmit(&debugHeader, 1); |
stormysun513 | 12:7060e287bdae | 497 | break; |
stormysun513 | 12:7060e287bdae | 498 | } |
stormysun513 | 12:7060e287bdae | 499 | case BLE_REQUEST_IMAGE_BY_INDEX: |
stormysun513 | 12:7060e287bdae | 500 | if(bleBytesRead >= 2){ |
stormysun513 | 12:7060e287bdae | 501 | salivaPeriodically = false; |
stormysun513 | 12:7060e287bdae | 502 | uint8_t check = uartTxPayload[1]; |
stormysun513 | 12:7060e287bdae | 503 | if(check == 0){ |
stormysun513 | 12:7060e287bdae | 504 | if(packetNum > 0){ |
stormysun513 | 12:7060e287bdae | 505 | setImgPktPayloadByID = false; |
stormysun513 | 12:7060e287bdae | 506 | directForward = false; |
stormysun513 | 12:7060e287bdae | 507 | RingBuffer_init(&ringBuffer); |
stormysun513 | 12:7060e287bdae | 508 | currentPacketIndex = 0; |
stormysun513 | 12:7060e287bdae | 509 | isRetransmit = false; |
stormysun513 | 12:7060e287bdae | 510 | retransmitSize = 0; |
stormysun513 | 12:7060e287bdae | 511 | stm32State = STM32_IMAGE_FORWARDING; |
stormysun513 | 12:7060e287bdae | 512 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 513 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 514 | } |
stormysun513 | 12:7060e287bdae | 515 | } |
stormysun513 | 12:7060e287bdae | 516 | else if(check <= 18){ |
stormysun513 | 12:7060e287bdae | 517 | if(bleBytesRead < check+2) |
stormysun513 | 12:7060e287bdae | 518 | break; |
stormysun513 | 12:7060e287bdae | 519 | retransmitSize = check; |
stormysun513 | 12:7060e287bdae | 520 | memcpy(retransmitIndices, uartTxPayload+2, retransmitSize); |
stormysun513 | 12:7060e287bdae | 521 | if(isRetransmit == true){ |
stormysun513 | 12:7060e287bdae | 522 | stm32State = STM32_IMAGE_FORWARDING; |
stormysun513 | 12:7060e287bdae | 523 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 524 | retransmitIndex = 0; |
stormysun513 | 12:7060e287bdae | 525 | currentPacketIndex = retransmitIndices[retransmitIndex]; |
stormysun513 | 12:7060e287bdae | 526 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 527 | } |
stormysun513 | 12:7060e287bdae | 528 | else{ |
stormysun513 | 12:7060e287bdae | 529 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 530 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 531 | } |
stormysun513 | 12:7060e287bdae | 532 | } |
stormysun513 | 12:7060e287bdae | 533 | else{ |
stormysun513 | 12:7060e287bdae | 534 | /* Invalid check number */ |
stormysun513 | 12:7060e287bdae | 535 | } |
stormysun513 | 12:7060e287bdae | 536 | } |
stormysun513 | 12:7060e287bdae | 537 | break; |
stormysun513 | 12:7060e287bdae | 538 | case BLE_CHANGE_DEVICE_ID: |
stormysun513 | 12:7060e287bdae | 539 | if(bleBytesRead >= 3) |
stormysun513 | 12:7060e287bdae | 540 | writeAT24EEPROMBuffer(*pI2C, 0, (char*)uartTxPayload+1, 2); |
stormysun513 | 12:7060e287bdae | 541 | break; |
stormysun513 | 12:7060e287bdae | 542 | default: |
stormysun513 | 12:7060e287bdae | 543 | break; |
stormysun513 | 12:7060e287bdae | 544 | } |
stormysun513 | 12:7060e287bdae | 545 | if(isUARTCommand){ |
stormysun513 | 12:7060e287bdae | 546 | stm32State = STM32_CHECK_HEADER; |
stormysun513 | 12:7060e287bdae | 547 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 548 | uartTxTransmit(uartTxPayload, 1); |
stormysun513 | 12:7060e287bdae | 549 | } |
stormysun513 | 12:7060e287bdae | 550 | } |
stormysun513 | 12:7060e287bdae | 551 | } |
stormysun513 | 12:7060e287bdae | 552 | |
stormysun513 | 12:7060e287bdae | 553 | void bleReduceConnInterval(void){ |
stormysun513 | 12:7060e287bdae | 554 | Gap::ConnectionParams_t bleGapConnParamInitStruct; |
stormysun513 | 12:7060e287bdae | 555 | bleGapConnParamInitStruct.minConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/ |
stormysun513 | 12:7060e287bdae | 556 | bleGapConnParamInitStruct.maxConnectionInterval = BLE_GAP_CP_MIN_CONN_INTVL_MIN; /*Minimum Connection Interval in 1.25 ms*/ |
stormysun513 | 12:7060e287bdae | 557 | bleGapConnParamInitStruct.slaveLatency = 0; /*Slave Latency in number of connection events*/ |
stormysun513 | 12:7060e287bdae | 558 | bleGapConnParamInitStruct.connectionSupervisionTimeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; /*Connection Supervision Timeout in 10 ms*/ |
stormysun513 | 12:7060e287bdae | 559 | |
stormysun513 | 12:7060e287bdae | 560 | ble.gap().updateConnectionParams(mHandle, &bleGapConnParamInitStruct); |
stormysun513 | 12:7060e287bdae | 561 | } |
stormysun513 | 12:7060e287bdae | 562 | |
stormysun513 | 11:b3929de96933 | 563 | void timerPeriodicalCB(void){ |
stormysun513 | 11:b3929de96933 | 564 | static uint32_t time = 0; |
stormysun513 | 11:b3929de96933 | 565 | time++; |
stormysun513 | 11:b3929de96933 | 566 | |
stormysun513 | 11:b3929de96933 | 567 | if ( time % 100 == 2 ) { // 250ms |
stormysun513 | 12:7060e287bdae | 568 | if(buttonState == BTN_CHECK_LONG_PRESS) |
stormysun513 | 12:7060e287bdae | 569 | checkButtonCounter++; |
stormysun513 | 11:b3929de96933 | 570 | } |
stormysun513 | 11:b3929de96933 | 571 | |
stormysun513 | 12:7060e287bdae | 572 | if(systemState != SYS_POWERSAVING){ |
stormysun513 | 12:7060e287bdae | 573 | if ( time % 3 == 1 ) { // 7.5ms |
stormysun513 | 12:7060e287bdae | 574 | isCheckBuf = true; |
stormysun513 | 12:7060e287bdae | 575 | } |
stormysun513 | 12:7060e287bdae | 576 | |
stormysun513 | 14:f62d55b7dead | 577 | // if ( time % 50 == 2 ) { // 250ms |
stormysun513 | 14:f62d55b7dead | 578 | // if(isInserted) |
stormysun513 | 14:f62d55b7dead | 579 | // timerFlashPowerLed(); |
stormysun513 | 14:f62d55b7dead | 580 | // } |
stormysun513 | 12:7060e287bdae | 581 | |
stormysun513 | 12:7060e287bdae | 582 | if ( time % 200 == 3 ) { // 500ms |
stormysun513 | 14:f62d55b7dead | 583 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 584 | if(isLowPower){ |
stormysun513 | 14:f62d55b7dead | 585 | timerFlashPowerLed(); |
stormysun513 | 14:f62d55b7dead | 586 | lowPowerCounter++; |
stormysun513 | 14:f62d55b7dead | 587 | } |
stormysun513 | 14:f62d55b7dead | 588 | #endif |
stormysun513 | 14:f62d55b7dead | 589 | |
stormysun513 | 14:f62d55b7dead | 590 | if(systemState == SYS_LOCKING_BUTTON) |
stormysun513 | 14:f62d55b7dead | 591 | timerFlashInsertionLed(); |
stormysun513 | 12:7060e287bdae | 592 | } |
stormysun513 | 12:7060e287bdae | 593 | |
stormysun513 | 12:7060e287bdae | 594 | if ( time % 400 == 4 ) { // 1s |
stormysun513 | 12:7060e287bdae | 595 | isRoutine = true; |
stormysun513 | 12:7060e287bdae | 596 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 13:65c9b7d667f6 | 597 | if(mHandle == 0) |
stormysun513 | 12:7060e287bdae | 598 | disConnIdleCnt++; |
stormysun513 | 12:7060e287bdae | 599 | else |
stormysun513 | 12:7060e287bdae | 600 | connIdleCnt++; |
stormysun513 | 12:7060e287bdae | 601 | #endif |
stormysun513 | 12:7060e287bdae | 602 | } |
stormysun513 | 12:7060e287bdae | 603 | |
stormysun513 | 12:7060e287bdae | 604 | if ( time % 800 == 5 ) { |
stormysun513 | 12:7060e287bdae | 605 | if(salivaPeriodically == true) |
stormysun513 | 12:7060e287bdae | 606 | requestSalivaVoltage(); |
stormysun513 | 12:7060e287bdae | 607 | } |
stormysun513 | 11:b3929de96933 | 608 | } |
stormysun513 | 11:b3929de96933 | 609 | } |
stormysun513 | 11:b3929de96933 | 610 | |
stormysun513 | 14:f62d55b7dead | 611 | void timerFlashPowerLed(void){ |
stormysun513 | 12:7060e287bdae | 612 | ledPower = !ledPower; |
stormysun513 | 11:b3929de96933 | 613 | } |
stormysun513 | 11:b3929de96933 | 614 | |
stormysun513 | 14:f62d55b7dead | 615 | void timerFlashInsertionLed(void){ |
stormysun513 | 14:f62d55b7dead | 616 | int tmp = pLedInsertion->read(); |
stormysun513 | 14:f62d55b7dead | 617 | pLedInsertion->write(!tmp); |
stormysun513 | 14:f62d55b7dead | 618 | } |
stormysun513 | 14:f62d55b7dead | 619 | |
stormysun513 | 12:7060e287bdae | 620 | void timerRoutineTasks(void){ |
stormysun513 | 11:b3929de96933 | 621 | uint8_t buf[BLE_BUF_LEN] = {0,}; |
stormysun513 | 12:7060e287bdae | 622 | buf[0] = SYS_HEADER_DEVICE_INFO; |
stormysun513 | 12:7060e287bdae | 623 | bool result = readAT24EEPROMBuffer(*pI2C, 0, (char*)buf+1, 2); |
stormysun513 | 11:b3929de96933 | 624 | |
stormysun513 | 11:b3929de96933 | 625 | if(result == false){ |
stormysun513 | 14:f62d55b7dead | 626 | pLedInsertion->write(1); |
stormysun513 | 11:b3929de96933 | 627 | buf[1] = 0xFF; |
stormysun513 | 11:b3929de96933 | 628 | buf[2] = 0xFF; |
stormysun513 | 11:b3929de96933 | 629 | } |
stormysun513 | 11:b3929de96933 | 630 | else{ |
stormysun513 | 14:f62d55b7dead | 631 | pLedInsertion->write(0); |
stormysun513 | 14:f62d55b7dead | 632 | if(systemState == SYS_POWERSAVING){ |
stormysun513 | 14:f62d55b7dead | 633 | pLedInsertion->write(1); |
stormysun513 | 14:f62d55b7dead | 634 | } |
stormysun513 | 12:7060e287bdae | 635 | } |
stormysun513 | 12:7060e287bdae | 636 | |
stormysun513 | 12:7060e287bdae | 637 | float fvalue = (float)aniBatterVolt.read()*3.3*100; |
stormysun513 | 12:7060e287bdae | 638 | uint16_t ivalue = (uint16_t)fvalue; |
stormysun513 | 12:7060e287bdae | 639 | buf[3] = (ivalue >> 8) & 0xFF; |
stormysun513 | 12:7060e287bdae | 640 | buf[4] = ivalue & 0xFF; |
stormysun513 | 12:7060e287bdae | 641 | |
stormysun513 | 14:f62d55b7dead | 642 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 643 | if(!isLowPower && ivalue < (LOW_POWER_THERS-LOW_POWER_THERS_MARGIN)){ |
stormysun513 | 14:f62d55b7dead | 644 | isLowPower = true; |
stormysun513 | 14:f62d55b7dead | 645 | lowPowerCounter = 0; |
stormysun513 | 14:f62d55b7dead | 646 | } |
stormysun513 | 14:f62d55b7dead | 647 | else if(isLowPower && ivalue > (LOW_POWER_THERS+LOW_POWER_THERS_MARGIN)){ |
stormysun513 | 14:f62d55b7dead | 648 | isLowPower = false; |
stormysun513 | 14:f62d55b7dead | 649 | } |
stormysun513 | 14:f62d55b7dead | 650 | #endif |
stormysun513 | 14:f62d55b7dead | 651 | |
stormysun513 | 12:7060e287bdae | 652 | if(mHandle != 0 && infoNotification == true){ |
stormysun513 | 12:7060e287bdae | 653 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, DEVICE_INFO_PAYLOAD); |
stormysun513 | 12:7060e287bdae | 654 | } |
stormysun513 | 12:7060e287bdae | 655 | } |
stormysun513 | 12:7060e287bdae | 656 | |
stormysun513 | 12:7060e287bdae | 657 | void uartTxTransmit(uint8_t* buf, uint16_t length){ |
stormysun513 | 12:7060e287bdae | 658 | uint16_t index; |
stormysun513 | 12:7060e287bdae | 659 | pc.putc(preamble[0]); |
stormysun513 | 12:7060e287bdae | 660 | pc.putc(preamble[1]); |
stormysun513 | 12:7060e287bdae | 661 | for(index = 0; index < length; index++) |
stormysun513 | 12:7060e287bdae | 662 | { |
stormysun513 | 12:7060e287bdae | 663 | pc.putc(buf[index]); |
stormysun513 | 12:7060e287bdae | 664 | } |
stormysun513 | 12:7060e287bdae | 665 | } |
stormysun513 | 12:7060e287bdae | 666 | |
stormysun513 | 12:7060e287bdae | 667 | // UARTRX Callback function |
stormysun513 | 12:7060e287bdae | 668 | void uartRxCB(void){ |
stormysun513 | 12:7060e287bdae | 669 | while(pc.readable()) |
stormysun513 | 12:7060e287bdae | 670 | { |
stormysun513 | 12:7060e287bdae | 671 | uint8_t byteIn = pc.getc(); |
stormysun513 | 12:7060e287bdae | 672 | if(stm32State == STM32_IDLE) |
stormysun513 | 12:7060e287bdae | 673 | continue; |
stormysun513 | 12:7060e287bdae | 674 | |
stormysun513 | 12:7060e287bdae | 675 | if(uartRxState == UARTRX_TRANSFERING_DATA){ |
stormysun513 | 12:7060e287bdae | 676 | RingBuffer_writebyte(&ringBuffer, byteIn); |
stormysun513 | 12:7060e287bdae | 677 | } |
stormysun513 | 12:7060e287bdae | 678 | else if(uartRxState == UARTRX_CHECK_FIRST_PREAMBLE && byteIn == preamble[0]){ |
stormysun513 | 12:7060e287bdae | 679 | uartRxState = UARTRX_CHECK_SECOND_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 680 | } |
stormysun513 | 12:7060e287bdae | 681 | else if(uartRxState == UARTRX_CHECK_SECOND_PREAMBLE && byteIn == preamble[1]){ |
stormysun513 | 12:7060e287bdae | 682 | uartRxState = UARTRX_TRANSFERING_DATA; |
stormysun513 | 12:7060e287bdae | 683 | } |
stormysun513 | 12:7060e287bdae | 684 | } |
stormysun513 | 12:7060e287bdae | 685 | } |
stormysun513 | 12:7060e287bdae | 686 | |
stormysun513 | 12:7060e287bdae | 687 | void buttonInterruptServiceRoutine(void){ |
stormysun513 | 12:7060e287bdae | 688 | if(buttonState == BTN_IDLE) |
stormysun513 | 12:7060e287bdae | 689 | buttonState = BTN_CHANGE_ANALOG; |
stormysun513 | 12:7060e287bdae | 690 | |
stormysun513 | 12:7060e287bdae | 691 | if(systemState == SYS_POWERSAVING) |
stormysun513 | 12:7060e287bdae | 692 | ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD); |
stormysun513 | 12:7060e287bdae | 693 | } |
stormysun513 | 12:7060e287bdae | 694 | |
stormysun513 | 12:7060e287bdae | 695 | void buttonCheckLongPress(uint8_t counter){ |
stormysun513 | 12:7060e287bdae | 696 | if(counter < CHECK_BUTTON_TIMES){ |
stormysun513 | 12:7060e287bdae | 697 | if(pButtonAnalogIn != NULL && (float)pButtonAnalogIn->read() < CHECK_BUTTON_VOLTAGE_LEVEL){ |
stormysun513 | 12:7060e287bdae | 698 | return; |
stormysun513 | 12:7060e287bdae | 699 | } |
stormysun513 | 12:7060e287bdae | 700 | else if(systemState == SYS_POWERSAVING){ |
stormysun513 | 12:7060e287bdae | 701 | ticker.detach(); |
stormysun513 | 12:7060e287bdae | 702 | } |
stormysun513 | 12:7060e287bdae | 703 | } |
stormysun513 | 12:7060e287bdae | 704 | else{ |
stormysun513 | 12:7060e287bdae | 705 | buttonLongPressVerified(); |
stormysun513 | 12:7060e287bdae | 706 | } |
stormysun513 | 12:7060e287bdae | 707 | changeButtonPinToInterrupt(); |
stormysun513 | 12:7060e287bdae | 708 | } |
stormysun513 | 12:7060e287bdae | 709 | |
stormysun513 | 12:7060e287bdae | 710 | void buttonLongPressVerified(void){ |
stormysun513 | 12:7060e287bdae | 711 | checkButtonCounter = 0; |
stormysun513 | 12:7060e287bdae | 712 | if(systemState == SYS_POWERSAVING){ |
stormysun513 | 12:7060e287bdae | 713 | systemState = SYS_RELAXING_BUTTON; |
stormysun513 | 12:7060e287bdae | 714 | ble.startAdvertising(); |
stormysun513 | 12:7060e287bdae | 715 | #ifdef APPLICATION_TIMEOUT |
stormysun513 | 12:7060e287bdae | 716 | disConnIdleCnt = 0; |
stormysun513 | 12:7060e287bdae | 717 | #endif |
stormysun513 | 14:f62d55b7dead | 718 | #ifdef LOW_POWER_CONSTRAINT |
stormysun513 | 14:f62d55b7dead | 719 | lowPowerCounter = 0; |
stormysun513 | 14:f62d55b7dead | 720 | #endif |
stormysun513 | 12:7060e287bdae | 721 | ticker.attach_us(timerPeriodicalCB, MINIMUM_TICKER_PERIOD); |
stormysun513 | 14:f62d55b7dead | 722 | ledPower = 0; |
stormysun513 | 14:f62d55b7dead | 723 | uartEnable = 1; |
stormysun513 | 12:7060e287bdae | 724 | resetSTM32StateType(); |
stormysun513 | 12:7060e287bdae | 725 | } |
stormysun513 | 12:7060e287bdae | 726 | else if(systemState == SYS_RELAXING_BUTTON){ |
stormysun513 | 12:7060e287bdae | 727 | shutdownDevice(); |
stormysun513 | 12:7060e287bdae | 728 | } |
stormysun513 | 12:7060e287bdae | 729 | } |
stormysun513 | 12:7060e287bdae | 730 | |
stormysun513 | 12:7060e287bdae | 731 | void changeButtonPinToAnalogIn(void){ |
stormysun513 | 12:7060e287bdae | 732 | pButton->fall(NULL); |
stormysun513 | 12:7060e287bdae | 733 | delete pButton; |
stormysun513 | 12:7060e287bdae | 734 | checkButtonCounter = 0; |
stormysun513 | 12:7060e287bdae | 735 | pButtonAnalogIn = new AnalogIn(BTN_POWER_ON_PIN); |
stormysun513 | 12:7060e287bdae | 736 | buttonState = BTN_CHECK_LONG_PRESS; |
stormysun513 | 12:7060e287bdae | 737 | } |
stormysun513 | 12:7060e287bdae | 738 | |
stormysun513 | 12:7060e287bdae | 739 | void changeButtonPinToInterrupt(void){ |
stormysun513 | 12:7060e287bdae | 740 | delete pButtonAnalogIn; |
stormysun513 | 12:7060e287bdae | 741 | pButton = new InterruptIn(BTN_POWER_ON_PIN); |
stormysun513 | 12:7060e287bdae | 742 | pButton->fall(buttonInterruptServiceRoutine); |
stormysun513 | 12:7060e287bdae | 743 | float dummy = (float)pButtonAnalogIn->read(); |
stormysun513 | 12:7060e287bdae | 744 | buttonState = BTN_IDLE; |
stormysun513 | 11:b3929de96933 | 745 | } |
stormysun513 | 12:7060e287bdae | 746 | |
stormysun513 | 12:7060e287bdae | 747 | void shutdownDevice(void){ |
stormysun513 | 12:7060e287bdae | 748 | systemState = SYS_POWERSAVING; |
stormysun513 | 13:65c9b7d667f6 | 749 | if(mHandle != 0) |
stormysun513 | 13:65c9b7d667f6 | 750 | ble.disconnect(mHandle, Gap::REMOTE_USER_TERMINATED_CONNECTION); |
stormysun513 | 14:f62d55b7dead | 751 | pLedInsertion->write(1); |
stormysun513 | 13:65c9b7d667f6 | 752 | ticker.detach(); |
stormysun513 | 12:7060e287bdae | 753 | ble.gap().stopAdvertising(); |
stormysun513 | 12:7060e287bdae | 754 | ledPower = 1; |
stormysun513 | 14:f62d55b7dead | 755 | uartEnable = 0; |
stormysun513 | 12:7060e287bdae | 756 | deepsleep(); |
stormysun513 | 12:7060e287bdae | 757 | } |
stormysun513 | 12:7060e287bdae | 758 | |
stormysun513 | 12:7060e287bdae | 759 | void resetSTM32StateType(void){ |
stormysun513 | 12:7060e287bdae | 760 | stm32State = STM32_IDLE; |
stormysun513 | 12:7060e287bdae | 761 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 762 | waitHeader = SYS_HEADER_NO_SPECIFIC; |
stormysun513 | 12:7060e287bdae | 763 | } |
stormysun513 | 12:7060e287bdae | 764 | |
stormysun513 | 12:7060e287bdae | 765 | void requestSalivaVoltage(void){ |
stormysun513 | 12:7060e287bdae | 766 | uint8_t buf[BLE_BUF_LEN] = {0,}; |
stormysun513 | 12:7060e287bdae | 767 | float fvalue = aniSalivaVolt.read()*3.3*100; |
stormysun513 | 12:7060e287bdae | 768 | uint16_t ivalue = (uint16_t)fvalue; |
stormysun513 | 12:7060e287bdae | 769 | |
stormysun513 | 12:7060e287bdae | 770 | buf[0] = SYS_HEADER_SALIVA_VOLTAGE; |
stormysun513 | 12:7060e287bdae | 771 | buf[1] = (ivalue >> 8) & 0xFF; |
stormysun513 | 12:7060e287bdae | 772 | buf[2] = ivalue & 0xFF; |
stormysun513 | 12:7060e287bdae | 773 | |
stormysun513 | 12:7060e287bdae | 774 | if(mHandle != 0){ |
stormysun513 | 12:7060e287bdae | 775 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, SINGLE_UINT16_PAYLOAD); |
stormysun513 | 12:7060e287bdae | 776 | } |
stormysun513 | 12:7060e287bdae | 777 | } |
stormysun513 | 12:7060e287bdae | 778 | |
stormysun513 | 12:7060e287bdae | 779 | void timerCheckBuffer(void){ |
stormysun513 | 12:7060e287bdae | 780 | uint16_t numBytes = RingBuffer_availableDataAmount(&ringBuffer); |
stormysun513 | 12:7060e287bdae | 781 | switch (stm32State) { |
stormysun513 | 12:7060e287bdae | 782 | case STM32_CHECK_HEADER:{ |
stormysun513 | 12:7060e287bdae | 783 | if (numBytes >= SINGLE_UINT16_PAYLOAD){ |
stormysun513 | 12:7060e287bdae | 784 | isEnterMainLoop = true; |
stormysun513 | 12:7060e287bdae | 785 | } |
stormysun513 | 12:7060e287bdae | 786 | break; |
stormysun513 | 12:7060e287bdae | 787 | } |
stormysun513 | 12:7060e287bdae | 788 | case STM32_IMAGE_FORWARDING:{ |
stormysun513 | 12:7060e287bdae | 789 | if(setImgPktPayloadByID == false){ |
stormysun513 | 12:7060e287bdae | 790 | if(numBytes >= UART_DATA_PACKET_PAYLOAD){ |
stormysun513 | 12:7060e287bdae | 791 | isEnterMainLoop = true; |
stormysun513 | 12:7060e287bdae | 792 | } |
stormysun513 | 12:7060e287bdae | 793 | else{ |
stormysun513 | 12:7060e287bdae | 794 | if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){ |
stormysun513 | 12:7060e287bdae | 795 | setImgPktPayloadByID = false; |
stormysun513 | 12:7060e287bdae | 796 | directForward = false; |
stormysun513 | 12:7060e287bdae | 797 | RingBuffer_init(&ringBuffer); |
stormysun513 | 12:7060e287bdae | 798 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 799 | waitCamera = 0; |
stormysun513 | 12:7060e287bdae | 800 | } |
stormysun513 | 12:7060e287bdae | 801 | } |
stormysun513 | 12:7060e287bdae | 802 | } |
stormysun513 | 12:7060e287bdae | 803 | else { |
stormysun513 | 12:7060e287bdae | 804 | if((targetPayload-alreadyTransferred) <= (BLE_BUF_LEN-1)){ |
stormysun513 | 12:7060e287bdae | 805 | if(numBytes+alreadyTransferred == targetPayload) |
stormysun513 | 12:7060e287bdae | 806 | directForward = true; |
stormysun513 | 12:7060e287bdae | 807 | } |
stormysun513 | 12:7060e287bdae | 808 | |
stormysun513 | 12:7060e287bdae | 809 | if (numBytes > 0){ |
stormysun513 | 12:7060e287bdae | 810 | isEnterMainLoop = true; |
stormysun513 | 12:7060e287bdae | 811 | } |
stormysun513 | 12:7060e287bdae | 812 | |
stormysun513 | 12:7060e287bdae | 813 | else if (waitCamera > MAX_WAIT_TARGET_PAYLOAD){ |
stormysun513 | 12:7060e287bdae | 814 | uartRxState = UARTRX_CHECK_FIRST_PREAMBLE; |
stormysun513 | 12:7060e287bdae | 815 | requestForImagePacket(currentPacketIndex); |
stormysun513 | 12:7060e287bdae | 816 | waitCamera = 0; |
stormysun513 | 12:7060e287bdae | 817 | } |
stormysun513 | 12:7060e287bdae | 818 | |
stormysun513 | 12:7060e287bdae | 819 | } |
stormysun513 | 12:7060e287bdae | 820 | break; |
stormysun513 | 12:7060e287bdae | 821 | } |
stormysun513 | 12:7060e287bdae | 822 | default: |
stormysun513 | 12:7060e287bdae | 823 | break; |
stormysun513 | 12:7060e287bdae | 824 | } |
stormysun513 | 12:7060e287bdae | 825 | } |
stormysun513 | 12:7060e287bdae | 826 | |
stormysun513 | 12:7060e287bdae | 827 | void requestForImagePacket(uint16_t packetId){ |
stormysun513 | 12:7060e287bdae | 828 | uint8_t tempBuf[3]; |
stormysun513 | 12:7060e287bdae | 829 | tempBuf[0] = static_cast<uint8_t>(BLE_REQUEST_IMAGE_BY_INDEX); |
stormysun513 | 12:7060e287bdae | 830 | tempBuf[1] = (packetId >> 8) & 0xFF; |
stormysun513 | 12:7060e287bdae | 831 | tempBuf[2] = packetId & 0xFF; |
stormysun513 | 12:7060e287bdae | 832 | uartTxTransmit(tempBuf, 3); |
stormysun513 | 12:7060e287bdae | 833 | } |
stormysun513 | 12:7060e287bdae | 834 | |
stormysun513 | 12:7060e287bdae | 835 | void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { |
stormysun513 | 12:7060e287bdae | 836 | FlowControl flow_type = (FlowControl)type; |
stormysun513 | 12:7060e287bdae | 837 | switch(type) { |
stormysun513 | 12:7060e287bdae | 838 | case RTS: |
stormysun513 | 12:7060e287bdae | 839 | serial_set_flow_control(&_serial, flow_type, flow1, NC); |
stormysun513 | 12:7060e287bdae | 840 | break; |
stormysun513 | 12:7060e287bdae | 841 | |
stormysun513 | 12:7060e287bdae | 842 | case CTS: |
stormysun513 | 12:7060e287bdae | 843 | serial_set_flow_control(&_serial, flow_type, NC, flow1); |
stormysun513 | 12:7060e287bdae | 844 | break; |
stormysun513 | 12:7060e287bdae | 845 | |
stormysun513 | 12:7060e287bdae | 846 | case RTSCTS: |
stormysun513 | 12:7060e287bdae | 847 | case Disabled: |
stormysun513 | 12:7060e287bdae | 848 | serial_set_flow_control(&_serial, flow_type, flow1, flow2); |
stormysun513 | 12:7060e287bdae | 849 | break; |
stormysun513 | 12:7060e287bdae | 850 | |
stormysun513 | 12:7060e287bdae | 851 | default: |
stormysun513 | 12:7060e287bdae | 852 | break; |
stormysun513 | 12:7060e287bdae | 853 | } |
stormysun513 | 12:7060e287bdae | 854 | } |