posilani dat
Dependencies: FatFileSystemCpp mbed PowerControl USBHostLite
main.cpp@26:5674b8978551, 2017-05-23 (annotated)
- Committer:
- PavelKumpan
- Date:
- Tue May 23 18:42:14 2017 +0000
- Revision:
- 26:5674b8978551
- Parent:
- 25:2f964ebcdfd8
Recreated communication protocol.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
legwinskij | 15:baa2672a9b38 | 1 | /* BRIEF: |
jkaderka | 9:8839ecc02e0e | 2 | * Connects to wifi and listen on ip given by dhcp on port 2000 |
jkaderka | 9:8839ecc02e0e | 3 | * |
legwinskij | 18:7acae34b518d | 4 | * Commands are send in for #X where X is from {S,E,T,B,R}, all commands are |
jkaderka | 9:8839ecc02e0e | 5 | * confirmed by mbed by sending #A |
jkaderka | 9:8839ecc02e0e | 6 | * |
jkaderka | 9:8839ecc02e0e | 7 | * Use #S to start saving data, #E to stop saving. This can be done repeatedly, |
jkaderka | 9:8839ecc02e0e | 8 | * mbed saves the data into separated files and generates new swimmer id |
jkaderka | 9:8839ecc02e0e | 9 | * At the end, use #T to start data transfer, the format of transmitted data is: |
jkaderka | 9:8839ecc02e0e | 10 | * |
jkaderka | 9:8839ecc02e0e | 11 | * Header for each swimmer: |
jkaderka | 9:8839ecc02e0e | 12 | * (int) -1 (int) -1 (int) swimmer_id |
jkaderka | 9:8839ecc02e0e | 13 | * Data itself |
jkaderka | 9:8839ecc02e0e | 14 | * (char) 0xAA (int) count 3*(short) acc 3*(float) gyro |
jkaderka | 9:8839ecc02e0e | 15 | * End of swimmer block |
jkaderka | 9:8839ecc02e0e | 16 | * (int) -1 (int) -1 |
jkaderka | 9:8839ecc02e0e | 17 | * |
jkaderka | 9:8839ecc02e0e | 18 | * where count is number starting on 0, incremented on every packet |
jkaderka | 9:8839ecc02e0e | 19 | * 0xAA is a sync byte used to avoid confusion when some bytes are lost during |
jkaderka | 9:8839ecc02e0e | 20 | * transmission (UART on faster reates seems to be unstable) |
legwinskij | 15:baa2672a9b38 | 21 | * |
legwinskij | 15:baa2672a9b38 | 22 | * CHANGELOG 22/10/2015 |
legwinskij | 15:baa2672a9b38 | 23 | * - Refactored code |
legwinskij | 15:baa2672a9b38 | 24 | * - Now using USB flash instead of SD card |
legwinskij | 15:baa2672a9b38 | 25 | * - SIZE of packets to be saved at once changed to 32 (from 1000, saving took too much time, effectively loosing measurements) |
legwinskij | 15:baa2672a9b38 | 26 | * - Using fwrite to write 32 packets at once (packet size = 16 bytes -> 32*16 = 512 bytes -> USB flash sector size is 512 bytes, thus maximizing fwrite effectivity |
legwinskij | 15:baa2672a9b38 | 27 | * - Saving of 32 packets takes about 3,5 ms |
legwinskij | 15:baa2672a9b38 | 28 | * KNOWN ISSUES: |
legwinskij | 15:baa2672a9b38 | 29 | * - Opening USB disk takes some time, couple first measurements not valid (perhaps open next file right when saving stops ?) |
legwinskij | 18:7acae34b518d | 30 | * |
legwinskij | 18:7acae34b518d | 31 | * CHANGELOG 13/11/2015 |
legwinskij | 18:7acae34b518d | 32 | * - Focused on data acquisition |
legwinskij | 18:7acae34b518d | 33 | * - #T commands now requires one byte after ACK to pick which swimmer data to send |
legwinskij | 18:7acae34b518d | 34 | * - When board resets, it deletes all files on root, to make sure old data won't get send |
legwinskij | 18:7acae34b518d | 35 | * - Added new command #B which represents begining of unified scope of data and should be sent right after connecting (clears all files and resets swimmerid) |
legwinskij | 18:7acae34b518d | 36 | * |
legwinskij | 18:7acae34b518d | 37 | * CHANGELOG 7/1/2016 |
legwinskij | 18:7acae34b518d | 38 | * - Integrated MPU6000 IMU library |
legwinskij | 18:7acae34b518d | 39 | * - Rewritten data acquisition to support new IMU |
legwinskij | 18:7acae34b518d | 40 | * - Edited usbhost library to support sandisk flash disk |
legwinskij | 18:7acae34b518d | 41 | * |
legwinskij | 18:7acae34b518d | 42 | * CHANGELOG 12/1/2016 |
legwinskij | 18:7acae34b518d | 43 | * - Added delay after chipselect deselection between device bursts (solved gyro data inconsistency) |
legwinskij | 18:7acae34b518d | 44 | * |
legwinskij | 19:c2476c0ea3e0 | 45 | * CHANGELOG 6/5/2016 |
legwinskij | 19:c2476c0ea3e0 | 46 | * - Moved to first real HW (rev 1.0) |
legwinskij | 19:c2476c0ea3e0 | 47 | * - Added multipurpose button functionality |
legwinskij | 19:c2476c0ea3e0 | 48 | * |
legwinskij | 20:66ecb2f0e307 | 49 | * CHANGELOG 19/10/2016 |
legwinskij | 20:66ecb2f0e307 | 50 | * - Added status message |
legwinskij | 20:66ecb2f0e307 | 51 | * - Changed button behaviour |
legwinskij | 20:66ecb2f0e307 | 52 | * - Added background battery monitoring |
legwinskij | 20:66ecb2f0e307 | 53 | * - Added background filesystem monitoring |
legwinskij | 20:66ecb2f0e307 | 54 | * |
legwinskij | 20:66ecb2f0e307 | 55 | * CHANGELOG 22/10/2016 |
legwinskij | 20:66ecb2f0e307 | 56 | * - Slightly adjusted wifly communication (added timeouts) |
legwinskij | 20:66ecb2f0e307 | 57 | * - Disabled TCP autoretransmission on wifly |
legwinskij | 20:66ecb2f0e307 | 58 | * - Added send log feature |
legwinskij | 20:66ecb2f0e307 | 59 | * |
legwinskij | 20:66ecb2f0e307 | 60 | * CHANGELOG 05/03/2017 |
legwinskij | 20:66ecb2f0e307 | 61 | * - Added define switch for new revision of hardware |
legwinskij | 20:66ecb2f0e307 | 62 | * - HW v1r2 only supports single MPU |
legwinskij | 20:66ecb2f0e307 | 63 | * |
PavelKumpan | 22:69621e10dd26 | 64 | * CHANGELOG 10/03/2017 |
PavelKumpan | 21:d1569911500a | 65 | * - Added swimmer index to the status message. |
PavelKumpan | 21:d1569911500a | 66 | * - Added record file for records, function appendRecord, sendRecords |
PavelKumpan | 21:d1569911500a | 67 | * - Added clear root command |
PavelKumpan | 22:69621e10dd26 | 68 | * |
PavelKumpan | 22:69621e10dd26 | 69 | * CHANGELOG 13/03/2017 |
PavelKumpan | 22:69621e10dd26 | 70 | * - Removed clearRoot() call from init. |
PavelKumpan | 23:bb76f8ad9557 | 71 | * - Change representation of the records file to binary. |
PavelKumpan | 25:2f964ebcdfd8 | 72 | * - Add Prepare command - first send prepare, wait for a file open and then send Start. |
PavelKumpan | 26:5674b8978551 | 73 | * |
PavelKumpan | 26:5674b8978551 | 74 | * CHANGELOG 20/05/2017 |
PavelKumpan | 26:5674b8978551 | 75 | * - Changed communication protocol - each message have header with its size. |
jkaderka | 9:8839ecc02e0e | 76 | */ |
legwinskij | 15:baa2672a9b38 | 77 | |
legwinskij | 15:baa2672a9b38 | 78 | // Includes ==================================================================== |
legwinskij | 15:baa2672a9b38 | 79 | |
jkaderka | 0:16fd37cf4a7c | 80 | #include "mbed.h" |
legwinskij | 18:7acae34b518d | 81 | #include "EthernetPowerControl.h" |
legwinskij | 18:7acae34b518d | 82 | #include "MSCFileSystem.h" |
legwinskij | 14:6bd7d1fff6af | 83 | #include "PowerControl.h" |
legwinskij | 18:7acae34b518d | 84 | #include "DirHandle.h" |
legwinskij | 18:7acae34b518d | 85 | #include "mpu6000.h" |
jkaderka | 0:16fd37cf4a7c | 86 | #include "stdint.h" |
legwinskij | 18:7acae34b518d | 87 | #include "stdio.h" |
jkaderka | 2:f623d1815dc4 | 88 | #include "utils.h" |
jkaderka | 4:030c7726c7dc | 89 | #include "wifi.h" |
legwinskij | 18:7acae34b518d | 90 | #include "math.h" |
legwinskij | 18:7acae34b518d | 91 | |
legwinskij | 20:66ecb2f0e307 | 92 | // Revision ==================================================================== |
legwinskij | 20:66ecb2f0e307 | 93 | |
legwinskij | 20:66ecb2f0e307 | 94 | // Revision define, it's important to select proper version! (pinmuxes differ) |
PavelKumpan | 25:2f964ebcdfd8 | 95 | #define REVISION 1 |
legwinskij | 20:66ecb2f0e307 | 96 | |
legwinskij | 15:baa2672a9b38 | 97 | // Defines ===================================================================== |
legwinskij | 15:baa2672a9b38 | 98 | |
legwinskij | 19:c2476c0ea3e0 | 99 | /* Data acquisition related stuff */ |
legwinskij | 18:7acae34b518d | 100 | //#define SAMPLE_RATE 700000 // Sample rate for ADC conversion |
legwinskij | 18:7acae34b518d | 101 | #define SAMPLE_RATE 10000 |
legwinskij | 19:c2476c0ea3e0 | 102 | #define SAMPLE_PERIOD 0.01 // Setting the period of measuring, 0.01 means 100 times per second |
legwinskij | 15:baa2672a9b38 | 103 | #define SIZE 32 // Setting number of instance in array - for saving to SD card |
legwinskij | 15:baa2672a9b38 | 104 | #define BUFFER_SIZE (sizeof(int) + sizeof(short)*6) //size of one data block |
jkaderka | 2:f623d1815dc4 | 105 | |
legwinskij | 19:c2476c0ea3e0 | 106 | /* Battery related defines */ |
legwinskij | 19:c2476c0ea3e0 | 107 | #define UVLO_THRESHOLD 140U // Battery low threshold |
legwinskij | 19:c2476c0ea3e0 | 108 | #define BATTERY_PERIOD 30U // How often to measure voltage in seconds |
legwinskij | 19:c2476c0ea3e0 | 109 | |
legwinskij | 19:c2476c0ea3e0 | 110 | /* Button press defines */ |
legwinskij | 19:c2476c0ea3e0 | 111 | #define PRESS_PERIOD_SHUTDOWN 2000U |
legwinskij | 20:66ecb2f0e307 | 112 | #define PRESS_PERIOD_LOW 250U |
legwinskij | 20:66ecb2f0e307 | 113 | #define PRESS_PERIOD_HIGH 450U |
legwinskij | 19:c2476c0ea3e0 | 114 | |
legwinskij | 19:c2476c0ea3e0 | 115 | /* Mass storage realted stuff */ |
legwinskij | 15:baa2672a9b38 | 116 | #define FSNAME "usb" |
legwinskij | 20:66ecb2f0e307 | 117 | #define LOG_FILE "/usb/log.txt" |
PavelKumpan | 21:d1569911500a | 118 | #define RECORDS_FILE "/usb/records.txt" |
legwinskij | 19:c2476c0ea3e0 | 119 | #define SPACE_LOW_THRESHOLD 100 // USB Flash disk free threshold |
legwinskij | 19:c2476c0ea3e0 | 120 | #define SPACE_PERIOD 300U // How often to measure free space in seconds |
jkaderka | 1:3ec5f7df2410 | 121 | |
legwinskij | 19:c2476c0ea3e0 | 122 | /* Commands swimfly understands */ |
PavelKumpan | 25:2f964ebcdfd8 | 123 | #define PREPARE_SAVING 'P' |
legwinskij | 15:baa2672a9b38 | 124 | #define SAVING_START 'S' |
legwinskij | 15:baa2672a9b38 | 125 | #define SAVING_STOP 'E' |
legwinskij | 18:7acae34b518d | 126 | #define TRANSFER_REQUEST 'T' |
legwinskij | 18:7acae34b518d | 127 | #define SCOPE_BEGIN 'B' |
legwinskij | 15:baa2672a9b38 | 128 | #define CHECK_READY 'R' |
legwinskij | 20:66ecb2f0e307 | 129 | #define SEND_LOG 'L' |
PavelKumpan | 21:d1569911500a | 130 | #define SEND_RECORDS 'D' |
legwinskij | 20:66ecb2f0e307 | 131 | #define HALT 'H' |
legwinskij | 15:baa2672a9b38 | 132 | |
legwinskij | 19:c2476c0ea3e0 | 133 | /* Comm stuff */ |
legwinskij | 19:c2476c0ea3e0 | 134 | #define SOH 0x01 |
legwinskij | 19:c2476c0ea3e0 | 135 | |
legwinskij | 19:c2476c0ea3e0 | 136 | /* Test mode define (undefine to disable) */ |
legwinskij | 19:c2476c0ea3e0 | 137 | //#define LOG_TEST_MODE |
legwinskij | 19:c2476c0ea3e0 | 138 | |
legwinskij | 15:baa2672a9b38 | 139 | /* Function like macros for leds (bleh) */ |
jkaderka | 1:3ec5f7df2410 | 140 | #define leds_off() { led_measuring = led_sending = led_working = led_saving = 0; } |
jkaderka | 1:3ec5f7df2410 | 141 | #define leds_error() { led_measuring = led_sending = led_working = led_saving = 1; } |
jkaderka | 0:16fd37cf4a7c | 142 | |
legwinskij | 19:c2476c0ea3e0 | 143 | // Function to power down magic USB interface chip with new firmware |
legwinskij | 19:c2476c0ea3e0 | 144 | #define USR_POWERDOWN (0x104) |
legwinskij | 19:c2476c0ea3e0 | 145 | int semihost_powerdown() { |
legwinskij | 19:c2476c0ea3e0 | 146 | uint32_t arg; |
legwinskij | 19:c2476c0ea3e0 | 147 | return __semihost(USR_POWERDOWN, &arg); |
legwinskij | 19:c2476c0ea3e0 | 148 | } |
legwinskij | 19:c2476c0ea3e0 | 149 | |
legwinskij | 20:66ecb2f0e307 | 150 | // Function prototypes ========================================================= |
legwinskij | 20:66ecb2f0e307 | 151 | |
legwinskij | 20:66ecb2f0e307 | 152 | /* File handling */ |
legwinskij | 20:66ecb2f0e307 | 153 | void clearRoot(void); |
legwinskij | 20:66ecb2f0e307 | 154 | void appendLog(char textToAppend[]); |
PavelKumpan | 23:bb76f8ad9557 | 155 | void appendRecord(void); |
legwinskij | 20:66ecb2f0e307 | 156 | bool doesFileExist(const char *filename); |
legwinskij | 20:66ecb2f0e307 | 157 | void spaceMeas(void); |
PavelKumpan | 23:bb76f8ad9557 | 158 | void readSwimmerID(void); |
legwinskij | 20:66ecb2f0e307 | 159 | |
legwinskij | 20:66ecb2f0e307 | 160 | /* Button handlers */ |
legwinskij | 20:66ecb2f0e307 | 161 | void btnPressCallback(void); |
legwinskij | 20:66ecb2f0e307 | 162 | void btnReleaseCallback(void); |
legwinskij | 20:66ecb2f0e307 | 163 | |
legwinskij | 20:66ecb2f0e307 | 164 | /* Power management */ |
legwinskij | 20:66ecb2f0e307 | 165 | void pllInit(void); |
legwinskij | 20:66ecb2f0e307 | 166 | void battMeas(void); |
legwinskij | 20:66ecb2f0e307 | 167 | void shutdownHandler(void); |
legwinskij | 20:66ecb2f0e307 | 168 | |
legwinskij | 20:66ecb2f0e307 | 169 | /* Init */ |
legwinskij | 20:66ecb2f0e307 | 170 | void boardInit(void); |
legwinskij | 20:66ecb2f0e307 | 171 | |
legwinskij | 20:66ecb2f0e307 | 172 | /* Measurement */ |
legwinskij | 20:66ecb2f0e307 | 173 | void measure(void); |
legwinskij | 20:66ecb2f0e307 | 174 | void setMeasuringFlag(void); |
legwinskij | 20:66ecb2f0e307 | 175 | |
legwinskij | 20:66ecb2f0e307 | 176 | /* Command functions */ |
PavelKumpan | 25:2f964ebcdfd8 | 177 | void prepareSaving(void); |
legwinskij | 20:66ecb2f0e307 | 178 | void startSaving(void); |
legwinskij | 20:66ecb2f0e307 | 179 | void stopSaving(void); |
legwinskij | 20:66ecb2f0e307 | 180 | void transfer(void); |
PavelKumpan | 26:5674b8978551 | 181 | void sendFile(char* name); |
legwinskij | 20:66ecb2f0e307 | 182 | void halt(void); |
legwinskij | 20:66ecb2f0e307 | 183 | void beginScope(void); |
legwinskij | 20:66ecb2f0e307 | 184 | void sendStatus(void); |
legwinskij | 20:66ecb2f0e307 | 185 | |
legwinskij | 20:66ecb2f0e307 | 186 | /* Generic functions */ |
legwinskij | 20:66ecb2f0e307 | 187 | void blink(void); |
legwinskij | 20:66ecb2f0e307 | 188 | |
legwinskij | 15:baa2672a9b38 | 189 | // Watchdog class ============================================================== |
jkaderka | 0:16fd37cf4a7c | 190 | |
jkaderka | 0:16fd37cf4a7c | 191 | class Watchdog { |
jkaderka | 0:16fd37cf4a7c | 192 | public: |
legwinskij | 15:baa2672a9b38 | 193 | /* Kick the watchdog with time period setting */ |
legwinskij | 19:c2476c0ea3e0 | 194 | void setKick(float s) { |
legwinskij | 15:baa2672a9b38 | 195 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
legwinskij | 15:baa2672a9b38 | 196 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
legwinskij | 15:baa2672a9b38 | 197 | LPC_WDT->WDTC = s * (float)clk; |
legwinskij | 15:baa2672a9b38 | 198 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
jkaderka | 0:16fd37cf4a7c | 199 | kick(); |
jkaderka | 0:16fd37cf4a7c | 200 | } |
legwinskij | 15:baa2672a9b38 | 201 | |
legwinskij | 15:baa2672a9b38 | 202 | /* Kick the watchdog */ |
jkaderka | 0:16fd37cf4a7c | 203 | void kick() { |
jkaderka | 0:16fd37cf4a7c | 204 | LPC_WDT->WDFEED = 0xAA; |
jkaderka | 0:16fd37cf4a7c | 205 | LPC_WDT->WDFEED = 0x55; |
jkaderka | 0:16fd37cf4a7c | 206 | } |
jkaderka | 0:16fd37cf4a7c | 207 | }; |
jkaderka | 0:16fd37cf4a7c | 208 | |
legwinskij | 15:baa2672a9b38 | 209 | // Objects & vars ============================================================== |
legwinskij | 15:baa2672a9b38 | 210 | |
legwinskij | 20:66ecb2f0e307 | 211 | /* Objects */ |
legwinskij | 19:c2476c0ea3e0 | 212 | Watchdog wdt; // Initialize watchdog |
legwinskij | 19:c2476c0ea3e0 | 213 | Ticker Saving_Ticker; // Ticker for interrupts on measuring |
legwinskij | 19:c2476c0ea3e0 | 214 | Ticker Battery_Ticker; // Ticker for voltage measurement |
legwinskij | 19:c2476c0ea3e0 | 215 | Ticker Space_Ticker; // Ticker for free space measurement |
legwinskij | 20:66ecb2f0e307 | 216 | Wifi wifi(p13, p14, p12, p23); // Wifly module |
legwinskij | 18:7acae34b518d | 217 | SPI spi(p5, p6, p7); // NEW MPU6000 SPI connection |
legwinskij | 19:c2476c0ea3e0 | 218 | MPU6000 mpu(spi, p8); // NEW MPU6000 |
legwinskij | 19:c2476c0ea3e0 | 219 | Timer button_timer; // Button press time counter |
legwinskij | 20:66ecb2f0e307 | 220 | Serial pc(USBTX, USBRX); // USB Serial port |
jkaderka | 0:16fd37cf4a7c | 221 | |
legwinskij | 15:baa2672a9b38 | 222 | /* LED indicators */ |
legwinskij | 20:66ecb2f0e307 | 223 | #if REVISION == 1 |
legwinskij | 20:66ecb2f0e307 | 224 | DigitalOut led_system(p30); |
legwinskij | 20:66ecb2f0e307 | 225 | #elif REVISION == 2 |
legwinskij | 20:66ecb2f0e307 | 226 | DigitalOut led_system(p27); |
legwinskij | 20:66ecb2f0e307 | 227 | DigitalOut cs_2(p26); |
legwinskij | 20:66ecb2f0e307 | 228 | #endif |
legwinskij | 15:baa2672a9b38 | 229 | DigitalOut led_measuring(LED1); |
legwinskij | 15:baa2672a9b38 | 230 | DigitalOut led_sending(LED2); |
legwinskij | 15:baa2672a9b38 | 231 | DigitalOut led_working(LED3); |
legwinskij | 15:baa2672a9b38 | 232 | DigitalOut led_saving(LED4); |
jkaderka | 0:16fd37cf4a7c | 233 | |
legwinskij | 19:c2476c0ea3e0 | 234 | /* System LED notification patterns */ |
legwinskij | 19:c2476c0ea3e0 | 235 | //typedef enum {USER_BOOT, UVLO, DISK_FULL, AOK} blink_t; |
legwinskij | 19:c2476c0ea3e0 | 236 | |
legwinskij | 19:c2476c0ea3e0 | 237 | /* Power related stuff */ |
legwinskij | 19:c2476c0ea3e0 | 238 | DigitalOut wifly_enable_n(p9); |
legwinskij | 19:c2476c0ea3e0 | 239 | DigitalOut flash_enable_n(p10); |
legwinskij | 19:c2476c0ea3e0 | 240 | DigitalOut btn_latch_n(p28); |
legwinskij | 19:c2476c0ea3e0 | 241 | InterruptIn btn_input(p29); |
legwinskij | 19:c2476c0ea3e0 | 242 | AnalogIn batt_vol(p15); |
legwinskij | 19:c2476c0ea3e0 | 243 | volatile uint8_t systemRequest = 0; |
legwinskij | 19:c2476c0ea3e0 | 244 | volatile bool userShutdownFlag = 0; |
legwinskij | 19:c2476c0ea3e0 | 245 | volatile bool UVLOShutdownFlag = 0; |
legwinskij | 19:c2476c0ea3e0 | 246 | volatile uint8_t battLevel = 150U; // Batt level between 136U/3.4816V and 165U/4.2240V (High byte of 16bit voltage measurement) |
legwinskij | 19:c2476c0ea3e0 | 247 | volatile uint32_t curTime = 0; |
legwinskij | 19:c2476c0ea3e0 | 248 | volatile uint32_t oldTime = 0; |
legwinskij | 19:c2476c0ea3e0 | 249 | |
legwinskij | 19:c2476c0ea3e0 | 250 | /* Added for test purposes */ |
legwinskij | 19:c2476c0ea3e0 | 251 | #ifdef LOG_TEST_MODE |
legwinskij | 20:66ecb2f0e307 | 252 | DigitalOut flagSetNotify(p19); |
legwinskij | 20:66ecb2f0e307 | 253 | DigitalOut measureNotify(p20); |
legwinskij | 19:c2476c0ea3e0 | 254 | #endif |
legwinskij | 15:baa2672a9b38 | 255 | |
legwinskij | 18:7acae34b518d | 256 | /* USB && saving related stuffs */ |
legwinskij | 15:baa2672a9b38 | 257 | FILE *gfp; // Global file pointer |
legwinskij | 15:baa2672a9b38 | 258 | MSCFileSystem logger(FSNAME); // Initialize USB flash disk |
legwinskij | 15:baa2672a9b38 | 259 | char fname[40]; // Variable for name of Text file saved on USB flash disk |
PavelKumpan | 24:f9809529d3ce | 260 | uint8_t timestamp[8]; // Variable for name of Text file saved on USB flash disk |
legwinskij | 19:c2476c0ea3e0 | 261 | static volatile uint64_t absolute = 0; // Variable for numbering saved data |
legwinskij | 19:c2476c0ea3e0 | 262 | volatile int relative = 0; // Relative packtets saved number |
PavelKumpan | 23:bb76f8ad9557 | 263 | volatile uint8_t swimmer_id = 0; // Global swimmer ID |
legwinskij | 19:c2476c0ea3e0 | 264 | volatile bool measuringFlag = 0; // Flag that is set with interrupt and indicates measurement is to be done |
legwinskij | 19:c2476c0ea3e0 | 265 | volatile bool savingFlag = 0; // Flag that is set and indicates data is being saved periodically |
legwinskij | 19:c2476c0ea3e0 | 266 | volatile bool lowSpaceFlag = 0; // Flag that is set when below SPACE_THRESHOLD MB's remain free |
legwinskij | 19:c2476c0ea3e0 | 267 | volatile uint16_t mbFree = 0; // Variable to store remaining space on USB flash disk |
legwinskij | 19:c2476c0ea3e0 | 268 | |
legwinskij | 15:baa2672a9b38 | 269 | /* Accelerometer buffers */ |
jkaderka | 5:83f69916e571 | 270 | short int acc_x[SIZE]; |
jkaderka | 5:83f69916e571 | 271 | short int acc_y[SIZE]; |
jkaderka | 5:83f69916e571 | 272 | short int acc_z[SIZE]; |
jkaderka | 0:16fd37cf4a7c | 273 | |
legwinskij | 15:baa2672a9b38 | 274 | /* Gyroscope buffers */ |
jkaderka | 13:d00c8a6a2e8c | 275 | short int gyro_x[SIZE]; |
jkaderka | 13:d00c8a6a2e8c | 276 | short int gyro_y[SIZE]; |
jkaderka | 13:d00c8a6a2e8c | 277 | short int gyro_z[SIZE]; |
jkaderka | 0:16fd37cf4a7c | 278 | |
PavelKumpan | 26:5674b8978551 | 279 | /* Command buffers */ |
PavelKumpan | 26:5674b8978551 | 280 | char cmd_header[FRAME_HEADER_LEN]; |
PavelKumpan | 26:5674b8978551 | 281 | char cmd_data[256]; |
PavelKumpan | 26:5674b8978551 | 282 | int cmd_len; |
PavelKumpan | 26:5674b8978551 | 283 | |
legwinskij | 18:7acae34b518d | 284 | // Functions -> File handling ================================================== |
legwinskij | 15:baa2672a9b38 | 285 | |
legwinskij | 18:7acae34b518d | 286 | /* Clear all files in root directory */ |
legwinskij | 18:7acae34b518d | 287 | void clearRoot(void) |
legwinskij | 15:baa2672a9b38 | 288 | { |
legwinskij | 18:7acae34b518d | 289 | char buf[40]; // Buffer for string building |
legwinskij | 18:7acae34b518d | 290 | |
legwinskij | 18:7acae34b518d | 291 | /* Dir related structs & vars */ |
legwinskij | 18:7acae34b518d | 292 | DIR *dir; // Prepare directory handle |
legwinskij | 18:7acae34b518d | 293 | struct dirent *file; // Directory entry structure |
legwinskij | 18:7acae34b518d | 294 | dir = opendir("/usb"); // Open root of usb flash disk |
legwinskij | 18:7acae34b518d | 295 | |
legwinskij | 18:7acae34b518d | 296 | /* Iterate all files beside log file in root directory and delete them */ |
legwinskij | 18:7acae34b518d | 297 | while((file = readdir(dir)) != NULL) // Incrementally points to next files, until NULL is reached |
legwinskij | 18:7acae34b518d | 298 | { |
legwinskij | 18:7acae34b518d | 299 | if (strcmp(file->d_name, "log.txt") != 0) // Only dele non-log files |
legwinskij | 18:7acae34b518d | 300 | { |
legwinskij | 18:7acae34b518d | 301 | sprintf(buf, "/usb/"); |
legwinskij | 18:7acae34b518d | 302 | sprintf(buf+5, file->d_name); |
legwinskij | 18:7acae34b518d | 303 | remove(buf); // Remove file that is currently being pointed to |
legwinskij | 18:7acae34b518d | 304 | } |
legwinskij | 18:7acae34b518d | 305 | } |
PavelKumpan | 26:5674b8978551 | 306 | |
legwinskij | 18:7acae34b518d | 307 | /* If file is not present */ |
legwinskij | 18:7acae34b518d | 308 | closedir(dir); // Close dir & return false |
PavelKumpan | 26:5674b8978551 | 309 | |
PavelKumpan | 26:5674b8978551 | 310 | FILE *records; |
PavelKumpan | 26:5674b8978551 | 311 | records = fopen(RECORDS_FILE, "w"); // Create new file for records |
PavelKumpan | 26:5674b8978551 | 312 | fclose(records); |
legwinskij | 15:baa2672a9b38 | 313 | } |
legwinskij | 15:baa2672a9b38 | 314 | |
legwinskij | 20:66ecb2f0e307 | 315 | /* Append line to log file */ |
legwinskij | 18:7acae34b518d | 316 | void appendLog(char textToAppend[]) |
legwinskij | 18:7acae34b518d | 317 | { |
legwinskij | 18:7acae34b518d | 318 | printf("Writing to log file...\r\n"); |
jkaderka | 0:16fd37cf4a7c | 319 | |
legwinskij | 18:7acae34b518d | 320 | /* Append line to log file */ |
legwinskij | 20:66ecb2f0e307 | 321 | FILE *log; |
legwinskij | 20:66ecb2f0e307 | 322 | log = fopen(LOG_FILE, "a"); // Open file with log name |
legwinskij | 20:66ecb2f0e307 | 323 | fprintf(log, "%s\r\n", textToAppend); // Append line |
legwinskij | 20:66ecb2f0e307 | 324 | fclose(log); // Close file |
legwinskij | 15:baa2672a9b38 | 325 | } |
legwinskij | 15:baa2672a9b38 | 326 | |
PavelKumpan | 21:d1569911500a | 327 | /* Append record to records file */ |
PavelKumpan | 23:bb76f8ad9557 | 328 | void appendRecord(void) |
PavelKumpan | 21:d1569911500a | 329 | { |
PavelKumpan | 21:d1569911500a | 330 | printf("Writing to record file...\r\n"); |
PavelKumpan | 21:d1569911500a | 331 | |
PavelKumpan | 22:69621e10dd26 | 332 | /* Append line to records file */ |
PavelKumpan | 21:d1569911500a | 333 | FILE *records; |
PavelKumpan | 21:d1569911500a | 334 | records = fopen(RECORDS_FILE, "a"); // Open file with records name |
PavelKumpan | 23:bb76f8ad9557 | 335 | |
PavelKumpan | 23:bb76f8ad9557 | 336 | uint8_t recordbuffer[9]; |
PavelKumpan | 23:bb76f8ad9557 | 337 | |
PavelKumpan | 23:bb76f8ad9557 | 338 | recordbuffer[0] = swimmer_id; |
PavelKumpan | 23:bb76f8ad9557 | 339 | |
PavelKumpan | 23:bb76f8ad9557 | 340 | for(int i = 0; i < 8; i++) |
PavelKumpan | 23:bb76f8ad9557 | 341 | { |
PavelKumpan | 23:bb76f8ad9557 | 342 | recordbuffer[i+1] = timestamp[i]; |
PavelKumpan | 23:bb76f8ad9557 | 343 | } |
PavelKumpan | 23:bb76f8ad9557 | 344 | |
PavelKumpan | 26:5674b8978551 | 345 | fwrite(recordbuffer, 1, 9, records); // Write 9 bytes of record |
PavelKumpan | 23:bb76f8ad9557 | 346 | fflush(records); |
PavelKumpan | 21:d1569911500a | 347 | fclose(records); // Close file |
PavelKumpan | 21:d1569911500a | 348 | } |
PavelKumpan | 21:d1569911500a | 349 | |
PavelKumpan | 23:bb76f8ad9557 | 350 | /* Read last swimmer ID */ |
PavelKumpan | 23:bb76f8ad9557 | 351 | void readSwimmerID(void) |
PavelKumpan | 23:bb76f8ad9557 | 352 | { |
PavelKumpan | 24:f9809529d3ce | 353 | size_t fileSize; |
PavelKumpan | 26:5674b8978551 | 354 | FILE *records; |
PavelKumpan | 26:5674b8978551 | 355 | |
PavelKumpan | 23:bb76f8ad9557 | 356 | if(false == doesFileExist(RECORDS_FILE)) |
PavelKumpan | 23:bb76f8ad9557 | 357 | { |
PavelKumpan | 23:bb76f8ad9557 | 358 | swimmer_id = 0; |
PavelKumpan | 23:bb76f8ad9557 | 359 | printf("File with records does not exists, so ID set to 0...\r\n"); |
PavelKumpan | 26:5674b8978551 | 360 | swimmer_id = 0; |
PavelKumpan | 26:5674b8978551 | 361 | printf("New records file created...\r\n"); |
PavelKumpan | 26:5674b8978551 | 362 | records = fopen(RECORDS_FILE, "a"); |
PavelKumpan | 23:bb76f8ad9557 | 363 | } |
PavelKumpan | 23:bb76f8ad9557 | 364 | else |
PavelKumpan | 23:bb76f8ad9557 | 365 | { |
PavelKumpan | 23:bb76f8ad9557 | 366 | printf("Reading last swimmer ID from records file...\r\n"); |
PavelKumpan | 23:bb76f8ad9557 | 367 | |
PavelKumpan | 23:bb76f8ad9557 | 368 | /* Append line to records file */ |
PavelKumpan | 23:bb76f8ad9557 | 369 | records = fopen(RECORDS_FILE, "r"); // Open file with records name |
PavelKumpan | 24:f9809529d3ce | 370 | |
PavelKumpan | 24:f9809529d3ce | 371 | fseek(records, 0L, SEEK_END); |
PavelKumpan | 24:f9809529d3ce | 372 | fileSize = ftell(records); |
PavelKumpan | 24:f9809529d3ce | 373 | fseek(records, 0L, SEEK_SET); |
PavelKumpan | 24:f9809529d3ce | 374 | |
PavelKumpan | 24:f9809529d3ce | 375 | if(true == ferror(records) || fileSize < 9) |
PavelKumpan | 23:bb76f8ad9557 | 376 | { |
PavelKumpan | 23:bb76f8ad9557 | 377 | swimmer_id = 0; |
PavelKumpan | 23:bb76f8ad9557 | 378 | } |
PavelKumpan | 23:bb76f8ad9557 | 379 | else |
PavelKumpan | 23:bb76f8ad9557 | 380 | { |
PavelKumpan | 24:f9809529d3ce | 381 | fseek(records, -9, SEEK_END); |
PavelKumpan | 23:bb76f8ad9557 | 382 | swimmer_id = (uint8_t)fgetc(records) + 1; |
PavelKumpan | 23:bb76f8ad9557 | 383 | } |
PavelKumpan | 26:5674b8978551 | 384 | } |
PavelKumpan | 26:5674b8978551 | 385 | |
PavelKumpan | 26:5674b8978551 | 386 | printf("Actual index is %d...\r\n", swimmer_id); |
PavelKumpan | 26:5674b8978551 | 387 | fclose(records); |
PavelKumpan | 23:bb76f8ad9557 | 388 | } |
PavelKumpan | 23:bb76f8ad9557 | 389 | |
legwinskij | 17:ca53e6d36163 | 390 | /* Sees whether giver file exists on flash disk */ |
legwinskij | 17:ca53e6d36163 | 391 | bool doesFileExist(const char *filename) |
legwinskij | 17:ca53e6d36163 | 392 | { |
legwinskij | 17:ca53e6d36163 | 393 | /* Dir related structs & vars */ |
legwinskij | 17:ca53e6d36163 | 394 | DIR *dir; // Prepare directory handle |
legwinskij | 17:ca53e6d36163 | 395 | struct dirent *file; // Directory entry structure |
legwinskij | 17:ca53e6d36163 | 396 | dir = opendir("/usb"); // Open root of usb flash disk |
legwinskij | 17:ca53e6d36163 | 397 | |
legwinskij | 17:ca53e6d36163 | 398 | /* Iterate all available files in root directory */ |
legwinskij | 17:ca53e6d36163 | 399 | while((file = readdir(dir)) != NULL) // Incrementally points to next files, until NULL is reached |
legwinskij | 17:ca53e6d36163 | 400 | { |
legwinskij | 17:ca53e6d36163 | 401 | /* Check if it's file we are looking for */ |
legwinskij | 17:ca53e6d36163 | 402 | if(strcmp(filename+5, file->d_name) == 0) // +5 moves us beyond /usb/, not cool I know... |
legwinskij | 17:ca53e6d36163 | 403 | { |
legwinskij | 17:ca53e6d36163 | 404 | closedir(dir); // Close dir & return true |
legwinskij | 17:ca53e6d36163 | 405 | return true; |
legwinskij | 17:ca53e6d36163 | 406 | } |
legwinskij | 17:ca53e6d36163 | 407 | } |
legwinskij | 17:ca53e6d36163 | 408 | |
legwinskij | 17:ca53e6d36163 | 409 | /* If file is not present */ |
legwinskij | 17:ca53e6d36163 | 410 | closedir(dir); // Close dir & return false |
legwinskij | 17:ca53e6d36163 | 411 | return false; // If not, return false |
legwinskij | 19:c2476c0ea3e0 | 412 | } |
legwinskij | 19:c2476c0ea3e0 | 413 | |
legwinskij | 19:c2476c0ea3e0 | 414 | /* Measures space left and updates global variable value */ |
legwinskij | 19:c2476c0ea3e0 | 415 | void spaceMeas(void) |
legwinskij | 19:c2476c0ea3e0 | 416 | { |
legwinskij | 19:c2476c0ea3e0 | 417 | FATFS* fs; |
legwinskij | 19:c2476c0ea3e0 | 418 | DWORD fre_clust; |
legwinskij | 19:c2476c0ea3e0 | 419 | f_getfree("0:",&fre_clust,&fs); |
legwinskij | 19:c2476c0ea3e0 | 420 | mbFree = (uint16_t)((fs->csize * fs->free_clust) >> 11); |
legwinskij | 20:66ecb2f0e307 | 421 | } |
legwinskij | 17:ca53e6d36163 | 422 | |
legwinskij | 19:c2476c0ea3e0 | 423 | // Functions -> Power management =============================================== |
legwinskij | 19:c2476c0ea3e0 | 424 | |
legwinskij | 20:66ecb2f0e307 | 425 | void pllInit(void) |
legwinskij | 20:66ecb2f0e307 | 426 | { |
legwinskij | 20:66ecb2f0e307 | 427 | LPC_SC->PLL0CON = 0x00; /* PLL0 Disable */ |
legwinskij | 20:66ecb2f0e307 | 428 | LPC_SC->PLL0FEED = 0xAA; |
legwinskij | 20:66ecb2f0e307 | 429 | LPC_SC->PLL0FEED = 0x55; |
legwinskij | 20:66ecb2f0e307 | 430 | |
legwinskij | 20:66ecb2f0e307 | 431 | LPC_SC->CCLKCFG = 0x00000005; /* Select Clock Divisor = 6 */ |
legwinskij | 20:66ecb2f0e307 | 432 | LPC_SC->PLL0CFG = 0x0000000B; /* configure PLL0 */ |
legwinskij | 20:66ecb2f0e307 | 433 | LPC_SC->PLL0FEED = 0xAA; /* divide by 1 then multiply by 12 */ |
legwinskij | 20:66ecb2f0e307 | 434 | LPC_SC->PLL0FEED = 0x55; /* PLL0 frequency = 288,000,000 */ |
legwinskij | 20:66ecb2f0e307 | 435 | |
legwinskij | 20:66ecb2f0e307 | 436 | LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */ |
legwinskij | 20:66ecb2f0e307 | 437 | LPC_SC->PLL0FEED = 0xAA; |
legwinskij | 20:66ecb2f0e307 | 438 | LPC_SC->PLL0FEED = 0x55; |
legwinskij | 20:66ecb2f0e307 | 439 | while (!(LPC_SC->PLL0STAT & (1<<26)));/* Wait for PLOCK0 */ |
legwinskij | 20:66ecb2f0e307 | 440 | |
legwinskij | 20:66ecb2f0e307 | 441 | LPC_SC->PLL0CON = 0x03; /* PLL0 Enable & Connect */ |
legwinskij | 20:66ecb2f0e307 | 442 | LPC_SC->PLL0FEED = 0xAA; |
legwinskij | 20:66ecb2f0e307 | 443 | LPC_SC->PLL0FEED = 0x55; |
legwinskij | 20:66ecb2f0e307 | 444 | while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));/* Wait for PLLC0_STAT & PLLE0_STAT */ |
legwinskij | 20:66ecb2f0e307 | 445 | |
legwinskij | 20:66ecb2f0e307 | 446 | SystemCoreClockUpdate(); /* Should now be at 48,000,000 */ |
legwinskij | 20:66ecb2f0e307 | 447 | SystemCoreClock = (12000000 * 2 / |
legwinskij | 20:66ecb2f0e307 | 448 | (((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1) * |
legwinskij | 20:66ecb2f0e307 | 449 | ((LPC_SC->PLL0STAT & 0x7FFF) + 1) / |
legwinskij | 20:66ecb2f0e307 | 450 | ((LPC_SC->CCLKCFG & 0xFF)+ 1)); |
legwinskij | 20:66ecb2f0e307 | 451 | } |
legwinskij | 20:66ecb2f0e307 | 452 | |
legwinskij | 19:c2476c0ea3e0 | 453 | /* Callback for button press */ |
legwinskij | 19:c2476c0ea3e0 | 454 | void btnPressCallback(void) |
legwinskij | 19:c2476c0ea3e0 | 455 | { |
legwinskij | 19:c2476c0ea3e0 | 456 | /* Resets and starts timer */ |
legwinskij | 19:c2476c0ea3e0 | 457 | button_timer.reset(); |
legwinskij | 19:c2476c0ea3e0 | 458 | button_timer.start(); |
legwinskij | 19:c2476c0ea3e0 | 459 | } |
legwinskij | 19:c2476c0ea3e0 | 460 | |
legwinskij | 19:c2476c0ea3e0 | 461 | /* Callback for button release */ |
legwinskij | 19:c2476c0ea3e0 | 462 | void btnReleaseCallback(void) |
legwinskij | 19:c2476c0ea3e0 | 463 | { |
legwinskij | 19:c2476c0ea3e0 | 464 | /* Stop timer */ |
legwinskij | 19:c2476c0ea3e0 | 465 | button_timer.stop(); |
legwinskij | 19:c2476c0ea3e0 | 466 | /* See whether user is trying to shutdown */ |
legwinskij | 20:66ecb2f0e307 | 467 | uint32_t buf = button_timer.read_ms(); |
legwinskij | 20:66ecb2f0e307 | 468 | if((buf < PRESS_PERIOD_HIGH) && (buf > PRESS_PERIOD_LOW)) |
legwinskij | 19:c2476c0ea3e0 | 469 | { |
legwinskij | 19:c2476c0ea3e0 | 470 | systemRequest += 1; |
legwinskij | 19:c2476c0ea3e0 | 471 | } |
legwinskij | 19:c2476c0ea3e0 | 472 | else |
legwinskij | 19:c2476c0ea3e0 | 473 | { |
legwinskij | 19:c2476c0ea3e0 | 474 | led_system = !led_system; |
legwinskij | 19:c2476c0ea3e0 | 475 | systemRequest = 0; |
legwinskij | 19:c2476c0ea3e0 | 476 | } |
legwinskij | 19:c2476c0ea3e0 | 477 | } |
legwinskij | 19:c2476c0ea3e0 | 478 | |
legwinskij | 19:c2476c0ea3e0 | 479 | /* Does single battery voltage measurement, and sets UVLO flag if voltage too low */ |
legwinskij | 19:c2476c0ea3e0 | 480 | void battMeas(void) |
legwinskij | 19:c2476c0ea3e0 | 481 | { |
legwinskij | 19:c2476c0ea3e0 | 482 | battLevel = batt_vol.read_u16() >> 8; // Store High byte of voltage measurement to global variable |
legwinskij | 19:c2476c0ea3e0 | 483 | if(battLevel < UVLO_THRESHOLD) // Determine whether we have enough juice |
legwinskij | 19:c2476c0ea3e0 | 484 | { |
legwinskij | 19:c2476c0ea3e0 | 485 | UVLOShutdownFlag = 1; // Raise Under Voltage Lock Out flag if necessary |
legwinskij | 19:c2476c0ea3e0 | 486 | } |
legwinskij | 19:c2476c0ea3e0 | 487 | } |
legwinskij | 19:c2476c0ea3e0 | 488 | |
legwinskij | 19:c2476c0ea3e0 | 489 | /* Checkes whether UVLO flag is set and halts board if necessary */ |
legwinskij | 19:c2476c0ea3e0 | 490 | void shutdownHandler(void) |
legwinskij | 19:c2476c0ea3e0 | 491 | { |
legwinskij | 19:c2476c0ea3e0 | 492 | /* Button shutdown handler */ |
legwinskij | 19:c2476c0ea3e0 | 493 | if(3 == systemRequest) |
legwinskij | 19:c2476c0ea3e0 | 494 | { |
legwinskij | 20:66ecb2f0e307 | 495 | halt(); // Halt |
legwinskij | 19:c2476c0ea3e0 | 496 | } |
legwinskij | 19:c2476c0ea3e0 | 497 | /* UVLO shutdown handler */ |
legwinskij | 19:c2476c0ea3e0 | 498 | if(1 == UVLOShutdownFlag) // Control of flag that is being set asynchronously if battLevel drops below threshold |
legwinskij | 19:c2476c0ea3e0 | 499 | { |
legwinskij | 19:c2476c0ea3e0 | 500 | if (1 == savingFlag) // If saving's in progress stop it, to make sure file system won't get corrupted |
legwinskij | 19:c2476c0ea3e0 | 501 | { |
legwinskij | 19:c2476c0ea3e0 | 502 | stopSaving(); |
legwinskij | 19:c2476c0ea3e0 | 503 | } |
legwinskij | 19:c2476c0ea3e0 | 504 | UVLOShutdownFlag = 0; |
legwinskij | 19:c2476c0ea3e0 | 505 | blink(); |
legwinskij | 19:c2476c0ea3e0 | 506 | blink(); |
legwinskij | 20:66ecb2f0e307 | 507 | appendLog("Battery low halting"); |
legwinskij | 19:c2476c0ea3e0 | 508 | printf("Battery low, halting.\r\n"); |
legwinskij | 19:c2476c0ea3e0 | 509 | btn_latch_n = 1; // Halt |
legwinskij | 19:c2476c0ea3e0 | 510 | } |
legwinskij | 19:c2476c0ea3e0 | 511 | } |
legwinskij | 19:c2476c0ea3e0 | 512 | |
legwinskij | 18:7acae34b518d | 513 | // Functions -> Init =========================================================== |
legwinskij | 18:7acae34b518d | 514 | |
legwinskij | 18:7acae34b518d | 515 | /* Prepares board for operation (adc's, gyroscope etc.) */ |
legwinskij | 18:7acae34b518d | 516 | void boardInit (void) |
legwinskij | 20:66ecb2f0e307 | 517 | { |
legwinskij | 20:66ecb2f0e307 | 518 | //pllInit(); |
legwinskij | 20:66ecb2f0e307 | 519 | pc.baud(9600); |
legwinskij | 20:66ecb2f0e307 | 520 | pc.printf("\r\nBooting...\r\n"); // Notify user that board is booting |
legwinskij | 20:66ecb2f0e307 | 521 | pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock); |
legwinskij | 20:66ecb2f0e307 | 522 | |
legwinskij | 19:c2476c0ea3e0 | 523 | /* Watchdog */ |
legwinskij | 19:c2476c0ea3e0 | 524 | if ((LPC_WDT->WDMOD >> 2) & 1) |
legwinskij | 19:c2476c0ea3e0 | 525 | { |
legwinskij | 19:c2476c0ea3e0 | 526 | led_system = 1; |
legwinskij | 19:c2476c0ea3e0 | 527 | wait(1); |
legwinskij | 19:c2476c0ea3e0 | 528 | led_system = 0; |
legwinskij | 19:c2476c0ea3e0 | 529 | } |
legwinskij | 19:c2476c0ea3e0 | 530 | wdt.setKick(15.0); |
legwinskij | 18:7acae34b518d | 531 | |
legwinskij | 19:c2476c0ea3e0 | 532 | /* Power related stuff */ |
legwinskij | 19:c2476c0ea3e0 | 533 | btn_latch_n = 0; // This latch holds power P-MOS opened in order to provide power to whole board |
legwinskij | 19:c2476c0ea3e0 | 534 | wait_ms(20); |
legwinskij | 19:c2476c0ea3e0 | 535 | flash_enable_n = 0; |
legwinskij | 19:c2476c0ea3e0 | 536 | wait_ms(20); |
legwinskij | 19:c2476c0ea3e0 | 537 | wifly_enable_n = 0; |
legwinskij | 20:66ecb2f0e307 | 538 | wifi.init(); // Initialize wifly |
legwinskij | 19:c2476c0ea3e0 | 539 | led_measuring = 1; // Use incremental LED's to notify user about boot stages |
legwinskij | 20:66ecb2f0e307 | 540 | #if REVISION == 2 |
legwinskij | 20:66ecb2f0e307 | 541 | cs_2 = 1; |
legwinskij | 20:66ecb2f0e307 | 542 | #endif |
legwinskij | 19:c2476c0ea3e0 | 543 | /* IMU init */ |
legwinskij | 20:66ecb2f0e307 | 544 | uint8_t name = mpu.init(); // Initializes both gyro and accelerometer |
legwinskij | 20:66ecb2f0e307 | 545 | printf("0x%02X MPUID\r\n", name); |
legwinskij | 19:c2476c0ea3e0 | 546 | wait_ms(40); // Wait for settings to stabilize |
legwinskij | 18:7acae34b518d | 547 | led_sending = 1; // Light up second LED to notify mpu init ok |
legwinskij | 20:66ecb2f0e307 | 548 | |
legwinskij | 19:c2476c0ea3e0 | 549 | /* Power saving features */ |
legwinskij | 18:7acae34b518d | 550 | PHY_PowerDown(); // Disable ETH PHY to conserve power (about 40mA) |
legwinskij | 19:c2476c0ea3e0 | 551 | semihost_powerdown(); // Disable USB Host to conserve power (about 30mA) |
legwinskij | 19:c2476c0ea3e0 | 552 | wait_ms(40); // Wait a bit |
legwinskij | 19:c2476c0ea3e0 | 553 | led_working = 1; // Light up third LED |
legwinskij | 19:c2476c0ea3e0 | 554 | |
legwinskij | 19:c2476c0ea3e0 | 555 | /* Measure voltage */ |
legwinskij | 19:c2476c0ea3e0 | 556 | battMeas(); |
legwinskij | 19:c2476c0ea3e0 | 557 | printf("%d mV\r\n", (battLevel << 8) / 10); |
legwinskij | 19:c2476c0ea3e0 | 558 | spaceMeas(); |
legwinskij | 19:c2476c0ea3e0 | 559 | printf("%d MB\r\n", mbFree); |
legwinskij | 19:c2476c0ea3e0 | 560 | shutdownHandler(); |
legwinskij | 19:c2476c0ea3e0 | 561 | wait_ms(40); |
legwinskij | 19:c2476c0ea3e0 | 562 | led_saving = 1; |
legwinskij | 19:c2476c0ea3e0 | 563 | |
legwinskij | 19:c2476c0ea3e0 | 564 | /* Timers & Tickers init */ |
legwinskij | 19:c2476c0ea3e0 | 565 | Battery_Ticker.attach(&battMeas, BATTERY_PERIOD); |
legwinskij | 19:c2476c0ea3e0 | 566 | Space_Ticker.attach(&spaceMeas, SPACE_PERIOD); |
legwinskij | 19:c2476c0ea3e0 | 567 | button_timer.start(); // Start timer that counts lengths of button presses |
legwinskij | 19:c2476c0ea3e0 | 568 | btn_input.fall(&btnPressCallback); // Attach button press callback |
legwinskij | 19:c2476c0ea3e0 | 569 | btn_input.rise(&btnReleaseCallback); // Attach button release callback |
legwinskij | 19:c2476c0ea3e0 | 570 | wait_ms(40); |
legwinskij | 18:7acae34b518d | 571 | |
PavelKumpan | 23:bb76f8ad9557 | 572 | readSwimmerID(); |
PavelKumpan | 23:bb76f8ad9557 | 573 | |
legwinskij | 18:7acae34b518d | 574 | /* End of init */ |
legwinskij | 19:c2476c0ea3e0 | 575 | appendLog("Booted"); // Append line to log file |
legwinskij | 18:7acae34b518d | 576 | leds_off(); // Turn all LEDs off |
legwinskij | 19:c2476c0ea3e0 | 577 | blink(); // Blink system led to notify user |
legwinskij | 20:66ecb2f0e307 | 578 | wdt.setKick(5); |
legwinskij | 20:66ecb2f0e307 | 579 | pc.printf("Ready...\r\n"); // Notify user of end init |
legwinskij | 18:7acae34b518d | 580 | } |
legwinskij | 18:7acae34b518d | 581 | |
legwinskij | 18:7acae34b518d | 582 | // Functions -> Measuring ====================================================== |
legwinskij | 18:7acae34b518d | 583 | |
legwinskij | 18:7acae34b518d | 584 | /* Main measuring function */ |
legwinskij | 19:c2476c0ea3e0 | 585 | void measure(void) |
legwinskij | 18:7acae34b518d | 586 | { |
legwinskij | 18:7acae34b518d | 587 | /* Vars */ |
legwinskij | 18:7acae34b518d | 588 | char buffer[SIZE][BUFFER_SIZE]; // Buffer |
legwinskij | 18:7acae34b518d | 589 | int pos = 0; // Position |
legwinskij | 18:7acae34b518d | 590 | |
legwinskij | 18:7acae34b518d | 591 | /* Read IMU values */ |
legwinskij | 18:7acae34b518d | 592 | mpu.valRead(ACCEL, acc_x[relative], acc_y[relative], acc_z[relative]); // Read Accelerometer values |
legwinskij | 18:7acae34b518d | 593 | mpu.valRead(GYRO, gyro_x[relative], gyro_y[relative],gyro_z[relative]); // Read Gyroscope values |
legwinskij | 18:7acae34b518d | 594 | |
legwinskij | 18:7acae34b518d | 595 | /* Increment counters */ |
legwinskij | 18:7acae34b518d | 596 | absolute++; // Increment absolute counter |
legwinskij | 18:7acae34b518d | 597 | relative++; // Increment relative counter |
legwinskij | 18:7acae34b518d | 598 | |
legwinskij | 18:7acae34b518d | 599 | /* After our buffers are full save the data */ |
legwinskij | 18:7acae34b518d | 600 | if ( absolute % SIZE == 0 ) |
legwinskij | 18:7acae34b518d | 601 | { |
legwinskij | 18:7acae34b518d | 602 | /* Flip led_saving state */ |
legwinskij | 18:7acae34b518d | 603 | led_saving = !led_saving; |
legwinskij | 18:7acae34b518d | 604 | |
legwinskij | 18:7acae34b518d | 605 | /* Handle file error */ |
legwinskij | 18:7acae34b518d | 606 | if (gfp == NULL) { |
legwinskij | 18:7acae34b518d | 607 | printf("Unable to append data to file %s\r\n", fname); // Notify user |
legwinskij | 18:7acae34b518d | 608 | return; // Return |
legwinskij | 18:7acae34b518d | 609 | } |
legwinskij | 18:7acae34b518d | 610 | |
legwinskij | 18:7acae34b518d | 611 | /* Iterate all buffer elements */ |
legwinskij | 18:7acae34b518d | 612 | for (int k = 0; k < SIZE; k++) |
legwinskij | 18:7acae34b518d | 613 | { |
legwinskij | 18:7acae34b518d | 614 | pos = absolute-SIZE+k; // Increment position |
legwinskij | 18:7acae34b518d | 615 | toBytes(buffer[k], &pos, sizeof(int)); // Append data to buffer in byte form |
legwinskij | 18:7acae34b518d | 616 | toBytes(buffer[k]+sizeof(int), &acc_x[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 617 | toBytes(buffer[k]+sizeof(int) + sizeof(short), &acc_y[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 618 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*2, &acc_z[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 619 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*3, &gyro_x[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 620 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*4, &gyro_y[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 621 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*5, &gyro_z[k], sizeof(short)); |
legwinskij | 18:7acae34b518d | 622 | } |
legwinskij | 18:7acae34b518d | 623 | |
legwinskij | 18:7acae34b518d | 624 | /* Write */ |
legwinskij | 19:c2476c0ea3e0 | 625 | #ifdef LOG_TEST_MODE |
legwinskij | 19:c2476c0ea3e0 | 626 | measureNotify = 1; // Used for saving debug |
legwinskij | 19:c2476c0ea3e0 | 627 | #endif |
legwinskij | 18:7acae34b518d | 628 | fwrite(buffer, SIZE, BUFFER_SIZE, gfp); // Write 32 buffer lines (16 bytes) to file (512 bytes total -> one block) |
legwinskij | 18:7acae34b518d | 629 | fflush(gfp); // Flush to make sure nothing is left in buffer |
legwinskij | 19:c2476c0ea3e0 | 630 | #ifdef LOG_TEST_MODE |
legwinskij | 19:c2476c0ea3e0 | 631 | measureNotify = 0; // Used for saving debug |
legwinskij | 19:c2476c0ea3e0 | 632 | #endif |
legwinskij | 18:7acae34b518d | 633 | |
legwinskij | 18:7acae34b518d | 634 | /* Reset relative counter */ |
legwinskij | 18:7acae34b518d | 635 | relative = 0; |
legwinskij | 18:7acae34b518d | 636 | } |
legwinskij | 18:7acae34b518d | 637 | } |
legwinskij | 18:7acae34b518d | 638 | |
legwinskij | 18:7acae34b518d | 639 | /* Sets the measuring flag */ |
legwinskij | 18:7acae34b518d | 640 | void setMeasuringFlag(void) |
legwinskij | 18:7acae34b518d | 641 | { |
legwinskij | 19:c2476c0ea3e0 | 642 | #ifdef LOG_TEST_MODE |
legwinskij | 19:c2476c0ea3e0 | 643 | flagSetNotify = 1; // Used for saving debug |
legwinskij | 19:c2476c0ea3e0 | 644 | #endif |
legwinskij | 18:7acae34b518d | 645 | measuringFlag = 1; // Set measuring flag |
legwinskij | 19:c2476c0ea3e0 | 646 | #ifdef LOG_TEST_MODE |
PavelKumpan | 23:bb76f8ad9557 | 647 | flagSetNotify = 0; // Used for saving debug |
legwinskij | 19:c2476c0ea3e0 | 648 | #endif |
legwinskij | 18:7acae34b518d | 649 | } |
legwinskij | 18:7acae34b518d | 650 | |
legwinskij | 17:ca53e6d36163 | 651 | // Functions -> Commands ======================================================= |
legwinskij | 17:ca53e6d36163 | 652 | |
PavelKumpan | 25:2f964ebcdfd8 | 653 | /* Prepares all to acquire data from sensors */ |
PavelKumpan | 25:2f964ebcdfd8 | 654 | void prepareSaving(void) |
legwinskij | 15:baa2672a9b38 | 655 | { |
PavelKumpan | 26:5674b8978551 | 656 | for(int i = 0; i < 8; i++) |
PavelKumpan | 25:2f964ebcdfd8 | 657 | { |
PavelKumpan | 26:5674b8978551 | 658 | timestamp[i] = cmd_data[i]; |
PavelKumpan | 21:d1569911500a | 659 | } |
PavelKumpan | 21:d1569911500a | 660 | |
legwinskij | 19:c2476c0ea3e0 | 661 | savingFlag = 1; // Set saving flag |
legwinskij | 15:baa2672a9b38 | 662 | /* Open global file with current swimmer ID */ |
legwinskij | 15:baa2672a9b38 | 663 | gfp = fopen(fname, "wb"); // Try to open that file |
legwinskij | 15:baa2672a9b38 | 664 | if(gfp == NULL) { |
legwinskij | 15:baa2672a9b38 | 665 | leds_error(); // Turn on all leds |
legwinskij | 18:7acae34b518d | 666 | printf("Unable to open file %s for writing\r\n", fname); // Notify user |
legwinskij | 15:baa2672a9b38 | 667 | return; // Exit function |
legwinskij | 15:baa2672a9b38 | 668 | } |
legwinskij | 15:baa2672a9b38 | 669 | |
legwinskij | 15:baa2672a9b38 | 670 | /* Prepare file & vars for data acquisition */ |
legwinskij | 15:baa2672a9b38 | 671 | fprintf(gfp, "%c%c%c", -1, -1, swimmer_id); // Append header to current swimmer output file |
legwinskij | 18:7acae34b518d | 672 | fflush(gfp); |
legwinskij | 15:baa2672a9b38 | 673 | |
legwinskij | 15:baa2672a9b38 | 674 | /* Reset counters */ |
legwinskij | 15:baa2672a9b38 | 675 | absolute = 0; // Reset absolute counter |
legwinskij | 15:baa2672a9b38 | 676 | relative = 0; // Reset relative counter |
PavelKumpan | 25:2f964ebcdfd8 | 677 | } |
legwinskij | 15:baa2672a9b38 | 678 | |
PavelKumpan | 25:2f964ebcdfd8 | 679 | /* Starts to acquire data from sensors */ |
PavelKumpan | 25:2f964ebcdfd8 | 680 | void startSaving(void) |
PavelKumpan | 25:2f964ebcdfd8 | 681 | { |
legwinskij | 19:c2476c0ea3e0 | 682 | Saving_Ticker.attach(&setMeasuringFlag, SAMPLE_PERIOD); // Attach function that periodically sets the flag |
legwinskij | 15:baa2672a9b38 | 683 | } |
legwinskij | 15:baa2672a9b38 | 684 | |
legwinskij | 19:c2476c0ea3e0 | 685 | /* Stops acquiring data, and save data left in buffers to USB */ |
legwinskij | 15:baa2672a9b38 | 686 | void stopSaving(void) |
legwinskij | 15:baa2672a9b38 | 687 | { |
legwinskij | 19:c2476c0ea3e0 | 688 | savingFlag = 0; // Clear saving flag |
legwinskij | 15:baa2672a9b38 | 689 | /* Stop saving */ |
legwinskij | 15:baa2672a9b38 | 690 | Saving_Ticker.detach(); // Stop setting measuring flag |
jkaderka | 1:3ec5f7df2410 | 691 | |
legwinskij | 15:baa2672a9b38 | 692 | /* Try to append data left in buffers */ |
legwinskij | 15:baa2672a9b38 | 693 | if(gfp == NULL) { |
legwinskij | 15:baa2672a9b38 | 694 | leds_error(); // Turn on all leds |
legwinskij | 18:7acae34b518d | 695 | printf("Unable to open file %s before sending data\r\n", fname); // Notify user |
legwinskij | 15:baa2672a9b38 | 696 | return; // Exit function |
legwinskij | 15:baa2672a9b38 | 697 | } |
legwinskij | 15:baa2672a9b38 | 698 | |
legwinskij | 15:baa2672a9b38 | 699 | /* Append data left to buffer */ |
legwinskij | 15:baa2672a9b38 | 700 | int base = ((absolute - (absolute%SIZE)) < 0) ? 0 : absolute - (absolute%SIZE); |
legwinskij | 15:baa2672a9b38 | 701 | char buffer[relative][BUFFER_SIZE]; // packet array |
legwinskij | 15:baa2672a9b38 | 702 | |
legwinskij | 15:baa2672a9b38 | 703 | /* Iterate all array elements */ |
legwinskij | 15:baa2672a9b38 | 704 | for (int k = 0; k < relative; k++) { |
legwinskij | 15:baa2672a9b38 | 705 | int pos = base+k; |
legwinskij | 15:baa2672a9b38 | 706 | toBytes(buffer[k], &pos, sizeof(int)); |
legwinskij | 15:baa2672a9b38 | 707 | toBytes(buffer[k]+sizeof(int), &acc_x[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 708 | toBytes(buffer[k]+sizeof(int) + sizeof(short), &acc_y[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 709 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*2, &acc_z[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 710 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*3, &gyro_x[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 711 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*4, &gyro_y[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 712 | toBytes(buffer[k]+sizeof(int) + sizeof(short)*5, &gyro_z[k], sizeof(short)); |
legwinskij | 15:baa2672a9b38 | 713 | } |
legwinskij | 15:baa2672a9b38 | 714 | |
legwinskij | 15:baa2672a9b38 | 715 | /* Write what's left */ |
legwinskij | 15:baa2672a9b38 | 716 | fwrite(buffer, relative, BUFFER_SIZE, gfp); // Write all data at once |
legwinskij | 18:7acae34b518d | 717 | fflush(gfp); |
legwinskij | 15:baa2672a9b38 | 718 | |
legwinskij | 15:baa2672a9b38 | 719 | /* Append EOF and close file */ |
legwinskij | 15:baa2672a9b38 | 720 | fprintf(gfp, "%c%c", -1,-1); // Write EOF characters |
legwinskij | 18:7acae34b518d | 721 | fflush(gfp); |
legwinskij | 15:baa2672a9b38 | 722 | fclose(gfp); // Close file |
PavelKumpan | 21:d1569911500a | 723 | |
PavelKumpan | 21:d1569911500a | 724 | /* Append this file to the records file */ |
PavelKumpan | 23:bb76f8ad9557 | 725 | appendRecord(); |
PavelKumpan | 21:d1569911500a | 726 | |
legwinskij | 15:baa2672a9b38 | 727 | /* Prepare for next swimmer */ |
legwinskij | 15:baa2672a9b38 | 728 | swimmer_id++; // Increment swimmer ID |
legwinskij | 15:baa2672a9b38 | 729 | sprintf(fname, "/usb/swimmer%d.txt", swimmer_id); // Update current file name |
legwinskij | 15:baa2672a9b38 | 730 | remove(fname); // If file with such ID exists remove it to avoid reading old data |
legwinskij | 15:baa2672a9b38 | 731 | } |
legwinskij | 15:baa2672a9b38 | 732 | |
legwinskij | 15:baa2672a9b38 | 733 | /* Sends acquired data via Wi-Fi */ |
legwinskij | 15:baa2672a9b38 | 734 | void transfer(void) |
legwinskij | 15:baa2672a9b38 | 735 | { |
legwinskij | 17:ca53e6d36163 | 736 | /* Vars */ |
legwinskij | 17:ca53e6d36163 | 737 | char name[30]; // File name buffer |
legwinskij | 17:ca53e6d36163 | 738 | |
legwinskij | 17:ca53e6d36163 | 739 | /* File transfer prerequisites */ |
PavelKumpan | 26:5674b8978551 | 740 | printf("closing"); |
PavelKumpan | 26:5674b8978551 | 741 | if (gfp != NULL) { |
PavelKumpan | 26:5674b8978551 | 742 | fclose(gfp); |
legwinskij | 20:66ecb2f0e307 | 743 | } |
PavelKumpan | 26:5674b8978551 | 744 | printf(" file %d", cmd_data[0]); |
PavelKumpan | 26:5674b8978551 | 745 | sprintf(name, "/usb/swimmer%d.txt", cmd_data[0]); // Create file name based on current swimmer ID |
PavelKumpan | 26:5674b8978551 | 746 | printf(name); |
PavelKumpan | 26:5674b8978551 | 747 | sendFile(name); |
PavelKumpan | 21:d1569911500a | 748 | } |
PavelKumpan | 21:d1569911500a | 749 | |
legwinskij | 20:66ecb2f0e307 | 750 | /* Halts board */ |
legwinskij | 20:66ecb2f0e307 | 751 | void halt(void) |
legwinskij | 20:66ecb2f0e307 | 752 | { |
legwinskij | 20:66ecb2f0e307 | 753 | if (1 == savingFlag) // If saving's in progress stop it, to make sure file system won't get corrupted |
legwinskij | 20:66ecb2f0e307 | 754 | { |
legwinskij | 20:66ecb2f0e307 | 755 | stopSaving(); |
legwinskij | 20:66ecb2f0e307 | 756 | } |
legwinskij | 20:66ecb2f0e307 | 757 | systemRequest = 0; |
legwinskij | 20:66ecb2f0e307 | 758 | blink(); |
legwinskij | 20:66ecb2f0e307 | 759 | btn_latch_n = 1; |
legwinskij | 20:66ecb2f0e307 | 760 | } |
legwinskij | 20:66ecb2f0e307 | 761 | |
legwinskij | 19:c2476c0ea3e0 | 762 | /* Begins new scope (clears all data && rests swimmer_id counder) */ |
legwinskij | 18:7acae34b518d | 763 | void beginScope(void) |
legwinskij | 18:7acae34b518d | 764 | { |
legwinskij | 18:7acae34b518d | 765 | /* Clear all files and reset swimmer_id only when not after reset */ |
legwinskij | 18:7acae34b518d | 766 | if(swimmer_id != 0) // Redundancy check |
legwinskij | 18:7acae34b518d | 767 | { |
legwinskij | 18:7acae34b518d | 768 | appendLog("New scope"); |
legwinskij | 18:7acae34b518d | 769 | clearRoot(); // Clears all files on root of USB flash disk |
legwinskij | 18:7acae34b518d | 770 | swimmer_id = 0; // Resets simmer_id counter |
legwinskij | 18:7acae34b518d | 771 | sprintf(fname, "/usb/swimmer%d.txt", swimmer_id); // Update current file name |
jkaderka | 0:16fd37cf4a7c | 772 | } |
jkaderka | 0:16fd37cf4a7c | 773 | } |
jkaderka | 0:16fd37cf4a7c | 774 | |
legwinskij | 19:c2476c0ea3e0 | 775 | // Functions -> Generic functions ============================================== |
legwinskij | 19:c2476c0ea3e0 | 776 | |
PavelKumpan | 26:5674b8978551 | 777 | /* Sends acquired data via Wi-Fi */ |
PavelKumpan | 26:5674b8978551 | 778 | void sendFile(char* name) |
PavelKumpan | 26:5674b8978551 | 779 | { |
PavelKumpan | 26:5674b8978551 | 780 | /* Handle transfer */ |
PavelKumpan | 26:5674b8978551 | 781 | if(doesFileExist(name)) // At first check whether file exists (fopen used to freeze mbed) |
PavelKumpan | 26:5674b8978551 | 782 | { // Timeout is used to make sure C# gets ready (sockets are messy, flushing is pain) |
PavelKumpan | 26:5674b8978551 | 783 | /* Actually try to send file */ |
PavelKumpan | 26:5674b8978551 | 784 | printf("Sending %s...\r\n", name); // Notify user which user is being sent |
PavelKumpan | 26:5674b8978551 | 785 | if (wifi.sendFile(name) != 0) // Send ! |
PavelKumpan | 26:5674b8978551 | 786 | { |
PavelKumpan | 26:5674b8978551 | 787 | leds_error(); // Handle error |
PavelKumpan | 26:5674b8978551 | 788 | wifi.sendFail(); |
PavelKumpan | 26:5674b8978551 | 789 | printf("Unable to send file.\r\n"); // Notify user via text also |
PavelKumpan | 26:5674b8978551 | 790 | } |
PavelKumpan | 26:5674b8978551 | 791 | else |
PavelKumpan | 26:5674b8978551 | 792 | { |
PavelKumpan | 26:5674b8978551 | 793 | printf("File transfer finished.\r\n"); // Otherwise all is AOK |
PavelKumpan | 26:5674b8978551 | 794 | } |
PavelKumpan | 26:5674b8978551 | 795 | } |
PavelKumpan | 26:5674b8978551 | 796 | else |
PavelKumpan | 26:5674b8978551 | 797 | { |
PavelKumpan | 26:5674b8978551 | 798 | /* In case file doest not exist send NACK */ |
PavelKumpan | 26:5674b8978551 | 799 | wifi.sendNack(); |
PavelKumpan | 26:5674b8978551 | 800 | printf("Requested non-existing file.\r\n"); |
PavelKumpan | 26:5674b8978551 | 801 | } |
PavelKumpan | 26:5674b8978551 | 802 | } |
PavelKumpan | 26:5674b8978551 | 803 | |
legwinskij | 19:c2476c0ea3e0 | 804 | /* Sends status */ |
legwinskij | 19:c2476c0ea3e0 | 805 | void sendStatus(void) |
legwinskij | 19:c2476c0ea3e0 | 806 | { |
PavelKumpan | 21:d1569911500a | 807 | uint8_t checksum = SOH + savingFlag + battLevel + swimmer_id; // Calculate checksum |
PavelKumpan | 26:5674b8978551 | 808 | wifi.sendByte('#'); // # |
PavelKumpan | 26:5674b8978551 | 809 | wifi.sendByte('A'); // A |
PavelKumpan | 26:5674b8978551 | 810 | wifi.sendByte(5); // Send count of the data |
PavelKumpan | 26:5674b8978551 | 811 | wifi.sendByte(0); |
PavelKumpan | 26:5674b8978551 | 812 | wifi.sendByte(0); |
PavelKumpan | 26:5674b8978551 | 813 | wifi.sendByte(0); |
legwinskij | 19:c2476c0ea3e0 | 814 | wifi.sendByte(SOH); // Send start of heading |
legwinskij | 19:c2476c0ea3e0 | 815 | wifi.sendByte(savingFlag); // Send state of saving |
legwinskij | 19:c2476c0ea3e0 | 816 | wifi.sendByte(battLevel); // Sends current battery level |
PavelKumpan | 21:d1569911500a | 817 | wifi.sendByte(swimmer_id); // Sends current swimmer id index |
legwinskij | 19:c2476c0ea3e0 | 818 | wifi.sendByte(checksum); // Send checksum |
PavelKumpan | 26:5674b8978551 | 819 | |
PavelKumpan | 26:5674b8978551 | 820 | wifi.waitForAck(); |
legwinskij | 19:c2476c0ea3e0 | 821 | } |
legwinskij | 19:c2476c0ea3e0 | 822 | |
legwinskij | 19:c2476c0ea3e0 | 823 | void blink(void) |
legwinskij | 19:c2476c0ea3e0 | 824 | { |
legwinskij | 19:c2476c0ea3e0 | 825 | if(led_system) |
legwinskij | 19:c2476c0ea3e0 | 826 | { |
legwinskij | 19:c2476c0ea3e0 | 827 | led_system = 0; |
legwinskij | 19:c2476c0ea3e0 | 828 | } |
legwinskij | 19:c2476c0ea3e0 | 829 | wait_ms(200); |
legwinskij | 19:c2476c0ea3e0 | 830 | led_system = 1; |
legwinskij | 19:c2476c0ea3e0 | 831 | wait_ms(200); |
legwinskij | 19:c2476c0ea3e0 | 832 | led_system = 0; |
legwinskij | 19:c2476c0ea3e0 | 833 | } |
legwinskij | 19:c2476c0ea3e0 | 834 | |
legwinskij | 15:baa2672a9b38 | 835 | // Main ======================================================================== |
legwinskij | 15:baa2672a9b38 | 836 | |
jkaderka | 0:16fd37cf4a7c | 837 | int main() |
jkaderka | 0:16fd37cf4a7c | 838 | { |
legwinskij | 20:66ecb2f0e307 | 839 | /* Begin by initialization */ |
legwinskij | 19:c2476c0ea3e0 | 840 | boardInit(); // Initialize board |
PavelKumpan | 26:5674b8978551 | 841 | |
legwinskij | 15:baa2672a9b38 | 842 | /* Set zeroth swimmer name */ |
legwinskij | 15:baa2672a9b38 | 843 | sprintf(fname, "/usb/swimmer%d.txt", swimmer_id); // Prepare zeroth swimmer filename |
PavelKumpan | 26:5674b8978551 | 844 | |
legwinskij | 15:baa2672a9b38 | 845 | /* Main while */ |
legwinskij | 15:baa2672a9b38 | 846 | while (1) |
legwinskij | 15:baa2672a9b38 | 847 | { |
legwinskij | 19:c2476c0ea3e0 | 848 | /* Shutdown handler */ |
legwinskij | 19:c2476c0ea3e0 | 849 | shutdownHandler(); |
legwinskij | 15:baa2672a9b38 | 850 | /* Read command from wifly */ |
PavelKumpan | 26:5674b8978551 | 851 | cmd_len = wifi.getCmd(cmd_header, cmd_data); |
PavelKumpan | 26:5674b8978551 | 852 | |
PavelKumpan | 26:5674b8978551 | 853 | if(cmd_header[1] != 'N') |
PavelKumpan | 26:5674b8978551 | 854 | { |
PavelKumpan | 26:5674b8978551 | 855 | printf(">> #%c and %d bytes\r\n", cmd_header[1], cmd_len); |
PavelKumpan | 26:5674b8978551 | 856 | } |
PavelKumpan | 26:5674b8978551 | 857 | |
legwinskij | 15:baa2672a9b38 | 858 | /* Master switch */ |
PavelKumpan | 26:5674b8978551 | 859 | switch (cmd_header[1]) { |
legwinskij | 15:baa2672a9b38 | 860 | /* start measuring data periodically and save them info file */ |
PavelKumpan | 25:2f964ebcdfd8 | 861 | case PREPARE_SAVING: |
PavelKumpan | 25:2f964ebcdfd8 | 862 | { |
PavelKumpan | 25:2f964ebcdfd8 | 863 | wifi.sendAck(); |
PavelKumpan | 25:2f964ebcdfd8 | 864 | leds_off(); // Turn off all leds |
PavelKumpan | 25:2f964ebcdfd8 | 865 | prepareSaving(); // Prepares all to saving |
PavelKumpan | 25:2f964ebcdfd8 | 866 | printf("Opening file for swimmer %d...\r\n", swimmer_id); // Notify user |
PavelKumpan | 25:2f964ebcdfd8 | 867 | break; // Break from switch |
PavelKumpan | 25:2f964ebcdfd8 | 868 | } |
PavelKumpan | 25:2f964ebcdfd8 | 869 | |
PavelKumpan | 25:2f964ebcdfd8 | 870 | /* start measuring data periodically and save them info file */ |
legwinskij | 15:baa2672a9b38 | 871 | case SAVING_START: |
legwinskij | 15:baa2672a9b38 | 872 | { |
legwinskij | 20:66ecb2f0e307 | 873 | wifi.sendAck(); |
legwinskij | 15:baa2672a9b38 | 874 | leds_off(); // Turn off all leds |
legwinskij | 15:baa2672a9b38 | 875 | startSaving(); // Actually start saving |
legwinskij | 18:7acae34b518d | 876 | printf("Saving swimmer %d...\r\n", swimmer_id); // Notify user |
legwinskij | 15:baa2672a9b38 | 877 | led_measuring = 1; // Turn on led_measuring |
legwinskij | 15:baa2672a9b38 | 878 | break; // Break from switch |
jkaderka | 1:3ec5f7df2410 | 879 | } |
jkaderka | 1:3ec5f7df2410 | 880 | |
legwinskij | 15:baa2672a9b38 | 881 | /* stop saving data */ |
legwinskij | 15:baa2672a9b38 | 882 | case SAVING_STOP: |
legwinskij | 15:baa2672a9b38 | 883 | { |
legwinskij | 15:baa2672a9b38 | 884 | leds_off(); // Turn off all leds |
legwinskij | 15:baa2672a9b38 | 885 | stopSaving(); // Actually stop saving |
PavelKumpan | 26:5674b8978551 | 886 | wifi.sendAck(); |
legwinskij | 18:7acae34b518d | 887 | printf("Stopped...\r\n"); // Notify user |
legwinskij | 15:baa2672a9b38 | 888 | break; // Break from switch |
jkaderka | 1:3ec5f7df2410 | 889 | } |
legwinskij | 15:baa2672a9b38 | 890 | |
legwinskij | 15:baa2672a9b38 | 891 | /* Send all data */ |
legwinskij | 18:7acae34b518d | 892 | case TRANSFER_REQUEST: |
legwinskij | 15:baa2672a9b38 | 893 | { |
legwinskij | 15:baa2672a9b38 | 894 | leds_off(); // Turn off all leds |
legwinskij | 15:baa2672a9b38 | 895 | led_sending = 1; // Turn on led_sending |
legwinskij | 18:7acae34b518d | 896 | printf("Sending data...\r\n"); // Notify user that data is being sent |
legwinskij | 15:baa2672a9b38 | 897 | transfer(); // Actually transfer data |
legwinskij | 18:7acae34b518d | 898 | printf("Sending finished...\r\n"); // Notify user that data was sent successfully |
legwinskij | 15:baa2672a9b38 | 899 | leds_off(); // Turn off all leds again |
legwinskij | 15:baa2672a9b38 | 900 | break; // Break from switch |
jkaderka | 1:3ec5f7df2410 | 901 | } |
jkaderka | 1:3ec5f7df2410 | 902 | |
legwinskij | 18:7acae34b518d | 903 | /* Begin new scope */ |
legwinskij | 18:7acae34b518d | 904 | case SCOPE_BEGIN: |
legwinskij | 18:7acae34b518d | 905 | { |
legwinskij | 20:66ecb2f0e307 | 906 | wifi.sendAck(); |
legwinskij | 18:7acae34b518d | 907 | leds_off(); // Turn off all leds |
legwinskij | 18:7acae34b518d | 908 | led_measuring = led_saving = 1; // Turn on two leds to depict special behaviour |
legwinskij | 18:7acae34b518d | 909 | printf("Starting new scope...\r\n"); // Notify user that new scope is beginning |
legwinskij | 18:7acae34b518d | 910 | beginScope(); // Begin scope ! (clear all data && reset swimmer_id counter) |
legwinskij | 18:7acae34b518d | 911 | leds_off(); // Turn off all leds |
legwinskij | 18:7acae34b518d | 912 | break; // Break |
legwinskij | 18:7acae34b518d | 913 | } |
legwinskij | 18:7acae34b518d | 914 | |
legwinskij | 20:66ecb2f0e307 | 915 | /* Send log file */ |
legwinskij | 20:66ecb2f0e307 | 916 | case SEND_LOG: |
legwinskij | 20:66ecb2f0e307 | 917 | { |
legwinskij | 20:66ecb2f0e307 | 918 | wifi.sendAck(); |
legwinskij | 20:66ecb2f0e307 | 919 | leds_off(); // Turn off all leds |
legwinskij | 20:66ecb2f0e307 | 920 | led_measuring = led_saving = 1; // Turn on two leds to depict special behaviour |
legwinskij | 20:66ecb2f0e307 | 921 | printf("Sending log...\r\n"); // Notify user that new scope is beginning |
PavelKumpan | 26:5674b8978551 | 922 | sendFile(LOG_FILE); // Send log file |
legwinskij | 20:66ecb2f0e307 | 923 | leds_off(); // Turn off all leds |
legwinskij | 20:66ecb2f0e307 | 924 | break; // Break |
legwinskij | 20:66ecb2f0e307 | 925 | } |
legwinskij | 20:66ecb2f0e307 | 926 | |
PavelKumpan | 21:d1569911500a | 927 | /* Send records file */ |
PavelKumpan | 21:d1569911500a | 928 | case SEND_RECORDS: |
PavelKumpan | 21:d1569911500a | 929 | { |
PavelKumpan | 21:d1569911500a | 930 | leds_off(); // Turn off all leds |
PavelKumpan | 21:d1569911500a | 931 | led_measuring = led_saving = 1; // Turn on two leds to depict special behaviour |
PavelKumpan | 22:69621e10dd26 | 932 | printf("Sending records...\r\n"); // Notify user that new scope is beginning |
PavelKumpan | 26:5674b8978551 | 933 | sendFile(RECORDS_FILE); // Send records file |
PavelKumpan | 21:d1569911500a | 934 | leds_off(); // Turn off all leds |
PavelKumpan | 21:d1569911500a | 935 | break; // Break |
PavelKumpan | 21:d1569911500a | 936 | } |
PavelKumpan | 21:d1569911500a | 937 | |
legwinskij | 20:66ecb2f0e307 | 938 | /* Halt swimfly */ |
legwinskij | 20:66ecb2f0e307 | 939 | case HALT: |
legwinskij | 20:66ecb2f0e307 | 940 | { |
legwinskij | 20:66ecb2f0e307 | 941 | wifi.sendAck(); |
legwinskij | 20:66ecb2f0e307 | 942 | leds_off(); // Turn off all leds |
legwinskij | 20:66ecb2f0e307 | 943 | led_measuring = led_saving = 1; // Turn on two leds to depict special behaviour |
legwinskij | 20:66ecb2f0e307 | 944 | halt(); // Halt swimfly |
legwinskij | 20:66ecb2f0e307 | 945 | leds_off(); // Turn off all leds |
legwinskij | 20:66ecb2f0e307 | 946 | break; // Break |
legwinskij | 20:66ecb2f0e307 | 947 | } |
legwinskij | 20:66ecb2f0e307 | 948 | |
legwinskij | 19:c2476c0ea3e0 | 949 | /* Ready */ |
legwinskij | 18:7acae34b518d | 950 | case CHECK_READY: |
legwinskij | 19:c2476c0ea3e0 | 951 | { |
PavelKumpan | 26:5674b8978551 | 952 | printf("Sending status...\r\n"); // |
legwinskij | 19:c2476c0ea3e0 | 953 | leds_off(); // Turn off all leds |
legwinskij | 19:c2476c0ea3e0 | 954 | sendStatus(); // Sends status message |
legwinskij | 19:c2476c0ea3e0 | 955 | break; // Break |
legwinskij | 19:c2476c0ea3e0 | 956 | } |
PavelKumpan | 22:69621e10dd26 | 957 | |
legwinskij | 19:c2476c0ea3e0 | 958 | /* Nothing on serial */ |
legwinskij | 15:baa2672a9b38 | 959 | case 'N': |
legwinskij | 15:baa2672a9b38 | 960 | { |
legwinskij | 15:baa2672a9b38 | 961 | /* Handle data measuring */ |
legwinskij | 15:baa2672a9b38 | 962 | if(measuringFlag) // Wait for timer to set the flag again and again |
legwinskij | 15:baa2672a9b38 | 963 | { |
legwinskij | 19:c2476c0ea3e0 | 964 | measure(); // Measure ! |
legwinskij | 15:baa2672a9b38 | 965 | measuringFlag = 0; // Reset flag |
legwinskij | 15:baa2672a9b38 | 966 | } |
legwinskij | 15:baa2672a9b38 | 967 | break; // Break from switch |
legwinskij | 15:baa2672a9b38 | 968 | } |
legwinskij | 15:baa2672a9b38 | 969 | |
PavelKumpan | 26:5674b8978551 | 970 | /* Fail (should be handled in some inner function */ |
PavelKumpan | 26:5674b8978551 | 971 | case 'F': |
PavelKumpan | 26:5674b8978551 | 972 | { |
PavelKumpan | 26:5674b8978551 | 973 | break; // Break from switch |
PavelKumpan | 26:5674b8978551 | 974 | } |
PavelKumpan | 26:5674b8978551 | 975 | |
PavelKumpan | 26:5674b8978551 | 976 | /* Nothing on serial */ |
PavelKumpan | 26:5674b8978551 | 977 | case 'A': |
PavelKumpan | 26:5674b8978551 | 978 | { |
PavelKumpan | 26:5674b8978551 | 979 | break; // Break from switch |
PavelKumpan | 26:5674b8978551 | 980 | } |
PavelKumpan | 26:5674b8978551 | 981 | |
legwinskij | 15:baa2672a9b38 | 982 | /* Everything else is ballast */ |
legwinskij | 15:baa2672a9b38 | 983 | default : |
legwinskij | 15:baa2672a9b38 | 984 | { |
legwinskij | 20:66ecb2f0e307 | 985 | wifi.sendNack(); |
legwinskij | 15:baa2672a9b38 | 986 | leds_error(); // Turn on all leds |
PavelKumpan | 26:5674b8978551 | 987 | printf("Command %c is unknown!\r\n", cmd_header[1]); // Notify user |
legwinskij | 15:baa2672a9b38 | 988 | break; // Break from switch |
legwinskij | 15:baa2672a9b38 | 989 | } |
jkaderka | 0:16fd37cf4a7c | 990 | } |
legwinskij | 19:c2476c0ea3e0 | 991 | wdt.kick(); // Kick watchdog each iteration |
jkaderka | 0:16fd37cf4a7c | 992 | } |
jkaderka | 0:16fd37cf4a7c | 993 | } |