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