Dependencies: USBDevice
main.cpp@30:3a1d9c2b49a5, 2019-05-28 (annotated)
- Committer:
- cyberjoey
- Date:
- Tue May 28 18:14:55 2019 +0000
- Revision:
- 30:3a1d9c2b49a5
- Parent:
- 29:88f36da6a119
- Child:
- 31:2e7483c7281b
changed delay from 20ms to 30ms after convert command is sent
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:60a522ae2e35 | 1 | #include "mbed.h" |
switches | 1:90313362ec11 | 2 | #include "SDFileSystem.h" |
cyberjoey | 9:36ba3626aab7 | 3 | #include "max32630fthr.h" |
cyberjoey | 9:36ba3626aab7 | 4 | #include "USBSerial.h" |
cyberjoey | 11:94a7379c0db8 | 5 | #include <ctype.h> |
switches | 0:60a522ae2e35 | 6 | |
cyberjoey | 9:36ba3626aab7 | 7 | #define PERIOD 10 // Logging period in seconds |
cyberjoey | 30:3a1d9c2b49a5 | 8 | #define CONVERT_T_DELAY 30 |
cyberjoey | 9:36ba3626aab7 | 9 | |
cyberjoey | 9:36ba3626aab7 | 10 | //OT07 Registers |
cyberjoey | 9:36ba3626aab7 | 11 | #define OT07_STATUS 0x00 // OT07 status regiter |
cyberjoey | 9:36ba3626aab7 | 12 | #define OT07_INT_EN 0x01 // OT07 Interrupt Enable |
cyberjoey | 9:36ba3626aab7 | 13 | #define OT07_FIFO_W 0x04 // OT07 FIFO Write Pointer |
cyberjoey | 9:36ba3626aab7 | 14 | #define OT07_FIFO_R 0x05 // OT07 FIFO Read Pointer |
cyberjoey | 9:36ba3626aab7 | 15 | #define OT07_FIFO_OF 0x06 // OT07 FIFO Overflow Counter |
cyberjoey | 9:36ba3626aab7 | 16 | #define OT07_FIFO_COUNT 0x07 // OT07 FIFO Data Count |
cyberjoey | 9:36ba3626aab7 | 17 | #define OT07_FIFO_DATA 0x08 // OT07 FIFO Data |
cyberjoey | 9:36ba3626aab7 | 18 | #define OT07_FIFO_CNFG1 0x09 // OT07 FIFO Configuration 1 (FIFO_A_FULL) |
cyberjoey | 9:36ba3626aab7 | 19 | #define OT07_FIFO_CNFG2 0x0A // OT07 FIFO Configuration 2 |
cyberjoey | 9:36ba3626aab7 | 20 | #define OT07_SYS 0x0C // OT07 System Configuration |
cyberjoey | 9:36ba3626aab7 | 21 | #define OT07_ALARM_HIGH_MSB 0x10 // OT07 Alarm High MSB |
cyberjoey | 9:36ba3626aab7 | 22 | #define OT07_ALARM_HIGH_LSB 0x11 // OT07 Alarm High LSB |
cyberjoey | 9:36ba3626aab7 | 23 | #define OT07_ALARM_LOW_MSB 0x12 // OT07 Alarm Low MSB |
cyberjoey | 9:36ba3626aab7 | 24 | #define OT07_ALARM_LOW_LSB 0x13 // OT07 Alarm LOW LSB |
cyberjoey | 17:401a5bb8d403 | 25 | #define OT07_ADC_SETUP 0x14 // OT07 Temp Seneor Setup (ADC_RES[7:6]) & Convert Temperature [0] |
cyberjoey | 9:36ba3626aab7 | 26 | #define OT07_GPIO_SETUP 0x20 // OT07 GPIO Setup, sets GPIO modes |
cyberjoey | 17:401a5bb8d403 | 27 | #define OT07_GPIO_CTRL 0x21 // OT07 GPIO control |
cyberjoey | 26:9ae9d097253a | 28 | #define OT07_ROM_ID 0x31 // OT07 ROM_ID address of LSB? |
cyberjoey | 9:36ba3626aab7 | 29 | |
cyberjoey | 25:08b220797bd9 | 30 | #define DEVICE_ACK 0 |
cyberjoey | 25:08b220797bd9 | 31 | #define DEVICE_NACK 1 |
cyberjoey | 25:08b220797bd9 | 32 | #define DEVICE_BAD_RESP 2 |
cyberjoey | 9:36ba3626aab7 | 33 | |
cyberjoey | 9:36ba3626aab7 | 34 | #define MAX_DEVICES 64 // Maximum number of rom devices allowed |
cyberjoey | 26:9ae9d097253a | 35 | #define ID_LENGTH 6 // Rom ID length in bytes |
cyberjoey | 9:36ba3626aab7 | 36 | |
cyberjoey | 9:36ba3626aab7 | 37 | |
cyberjoey | 9:36ba3626aab7 | 38 | #define BS 8 // ASCII Back Space |
cyberjoey | 9:36ba3626aab7 | 39 | #define CR 13 // ASCII Carriage Return |
cyberjoey | 9:36ba3626aab7 | 40 | |
cyberjoey | 17:401a5bb8d403 | 41 | struct OT07_struct { |
cyberjoey | 26:9ae9d097253a | 42 | char rom_id[ID_LENGTH]; // device ROM ID |
cyberjoey | 17:401a5bb8d403 | 43 | char I2C_address; // I2C addess, based on GPIO0 and GPIO1 at power up |
cyberjoey | 17:401a5bb8d403 | 44 | }; |
cyberjoey | 17:401a5bb8d403 | 45 | |
cyberjoey | 24:454dc350bb17 | 46 | struct TempResponse { |
cyberjoey | 24:454dc350bb17 | 47 | double tempC; // Temperature in °C |
cyberjoey | 24:454dc350bb17 | 48 | int status; // Status of Temperature read. 0 for success, 1 for error |
cyberjoey | 24:454dc350bb17 | 49 | }; |
cyberjoey | 24:454dc350bb17 | 50 | |
cyberjoey | 10:148da21c297e | 51 | const char* settings_file = "/sd/settings.txt"; |
cyberjoey | 22:fe94be11bae0 | 52 | const char* log_file = "/sd/MAX30208Log.csv"; |
cyberjoey | 10:148da21c297e | 53 | |
cyberjoey | 9:36ba3626aab7 | 54 | //global variable |
cyberjoey | 9:36ba3626aab7 | 55 | |
cyberjoey | 9:36ba3626aab7 | 56 | //******************** init Feather Boared Hardware *********************** |
cyberjoey | 9:36ba3626aab7 | 57 | |
cyberjoey | 9:36ba3626aab7 | 58 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
cyberjoey | 9:36ba3626aab7 | 59 | |
cyberjoey | 9:36ba3626aab7 | 60 | //microSD logging file system setup |
cyberjoey | 9:36ba3626aab7 | 61 | SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); // mosi, miso, sclk, cs |
cyberjoey | 9:36ba3626aab7 | 62 | |
cyberjoey | 9:36ba3626aab7 | 63 | //SD card insertion detection pin |
cyberjoey | 9:36ba3626aab7 | 64 | DigitalIn SDDetect(P2_2, PullUp); |
cyberjoey | 11:94a7379c0db8 | 65 | InterruptIn SDInsert(P2_2); |
cyberjoey | 9:36ba3626aab7 | 66 | |
cyberjoey | 9:36ba3626aab7 | 67 | |
cyberjoey | 9:36ba3626aab7 | 68 | // Virtual serial port over USB |
cyberjoey | 15:61dce4bef44f | 69 | USBSerial pc(0x0B6A, 0x0042, 0x0001, false); |
cyberjoey | 9:36ba3626aab7 | 70 | |
cyberjoey | 9:36ba3626aab7 | 71 | // I2C setup |
cyberjoey | 9:36ba3626aab7 | 72 | I2C i2c(P3_4,P3_5); // P3_4 -> I2C1_SDA, P3_5-> I2C1_SCL |
cyberjoey | 9:36ba3626aab7 | 73 | |
cyberjoey | 9:36ba3626aab7 | 74 | //Timer setup |
cyberjoey | 9:36ba3626aab7 | 75 | Ticker timer_1; // timer for blinking led heartbeat |
cyberjoey | 9:36ba3626aab7 | 76 | |
cyberjoey | 9:36ba3626aab7 | 77 | bool tick_flag; // used for counting seconds |
cyberjoey | 9:36ba3626aab7 | 78 | bool log_flag = false; |
cyberjoey | 9:36ba3626aab7 | 79 | bool led_toggle_flag = false; |
cyberjoey | 9:36ba3626aab7 | 80 | bool button_flag = false; |
cyberjoey | 11:94a7379c0db8 | 81 | bool sd_insert_flag = false; |
cyberjoey | 9:36ba3626aab7 | 82 | bool error_flag; |
cyberjoey | 9:36ba3626aab7 | 83 | int error_ticks; |
cyberjoey | 9:36ba3626aab7 | 84 | |
cyberjoey | 9:36ba3626aab7 | 85 | //LED blink setup |
switches | 1:90313362ec11 | 86 | DigitalOut rLED(LED1); |
switches | 1:90313362ec11 | 87 | DigitalOut gLED(LED2); |
cyberjoey | 9:36ba3626aab7 | 88 | DigitalOut bLED(LED3); |
switches | 0:60a522ae2e35 | 89 | |
cyberjoey | 9:36ba3626aab7 | 90 | InterruptIn button(SW1); |
cyberjoey | 9:36ba3626aab7 | 91 | |
cyberjoey | 9:36ba3626aab7 | 92 | |
cyberjoey | 9:36ba3626aab7 | 93 | void LED_blink_callback(){ // LED Heart beat |
cyberjoey | 9:36ba3626aab7 | 94 | |
cyberjoey | 9:36ba3626aab7 | 95 | led_toggle_flag = !led_toggle_flag; |
cyberjoey | 9:36ba3626aab7 | 96 | |
cyberjoey | 9:36ba3626aab7 | 97 | if(log_flag) //if logging |
cyberjoey | 9:36ba3626aab7 | 98 | { |
cyberjoey | 9:36ba3626aab7 | 99 | |
cyberjoey | 9:36ba3626aab7 | 100 | if(led_toggle_flag) |
cyberjoey | 9:36ba3626aab7 | 101 | { |
cyberjoey | 9:36ba3626aab7 | 102 | //toggle red led |
cyberjoey | 9:36ba3626aab7 | 103 | rLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 104 | } |
cyberjoey | 9:36ba3626aab7 | 105 | else |
cyberjoey | 9:36ba3626aab7 | 106 | { |
cyberjoey | 9:36ba3626aab7 | 107 | rLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 108 | gLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 109 | bLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 110 | } |
cyberjoey | 9:36ba3626aab7 | 111 | } |
cyberjoey | 9:36ba3626aab7 | 112 | else if(error_flag) //if error (no sd card) |
cyberjoey | 9:36ba3626aab7 | 113 | { |
cyberjoey | 9:36ba3626aab7 | 114 | if(led_toggle_flag) |
cyberjoey | 9:36ba3626aab7 | 115 | { |
cyberjoey | 9:36ba3626aab7 | 116 | //toggle red led |
cyberjoey | 9:36ba3626aab7 | 117 | rLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 118 | gLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 119 | } |
cyberjoey | 9:36ba3626aab7 | 120 | else |
cyberjoey | 9:36ba3626aab7 | 121 | { |
cyberjoey | 9:36ba3626aab7 | 122 | rLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 123 | gLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 124 | bLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 125 | } |
cyberjoey | 9:36ba3626aab7 | 126 | error_ticks--; |
cyberjoey | 9:36ba3626aab7 | 127 | if(error_ticks <= 0) |
cyberjoey | 9:36ba3626aab7 | 128 | error_flag = false; |
cyberjoey | 9:36ba3626aab7 | 129 | } |
cyberjoey | 9:36ba3626aab7 | 130 | else |
cyberjoey | 9:36ba3626aab7 | 131 | { |
cyberjoey | 9:36ba3626aab7 | 132 | if(led_toggle_flag) |
cyberjoey | 9:36ba3626aab7 | 133 | { |
cyberjoey | 9:36ba3626aab7 | 134 | //toggle teal leds |
cyberjoey | 9:36ba3626aab7 | 135 | gLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 136 | bLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 137 | } |
cyberjoey | 9:36ba3626aab7 | 138 | else |
cyberjoey | 9:36ba3626aab7 | 139 | { |
cyberjoey | 9:36ba3626aab7 | 140 | rLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 141 | gLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 142 | bLED = LED_OFF; |
cyberjoey | 9:36ba3626aab7 | 143 | } |
cyberjoey | 9:36ba3626aab7 | 144 | } |
cyberjoey | 9:36ba3626aab7 | 145 | |
cyberjoey | 9:36ba3626aab7 | 146 | tick_flag = true; |
cyberjoey | 9:36ba3626aab7 | 147 | } |
cyberjoey | 9:36ba3626aab7 | 148 | |
cyberjoey | 9:36ba3626aab7 | 149 | void btn_pressed() //button pressed isr |
cyberjoey | 9:36ba3626aab7 | 150 | { |
cyberjoey | 9:36ba3626aab7 | 151 | button_flag = true; |
cyberjoey | 9:36ba3626aab7 | 152 | } |
cyberjoey | 9:36ba3626aab7 | 153 | |
cyberjoey | 11:94a7379c0db8 | 154 | void sd_insert() //sd_insert pressed isr |
cyberjoey | 11:94a7379c0db8 | 155 | { |
cyberjoey | 11:94a7379c0db8 | 156 | sd_insert_flag = true; |
cyberjoey | 11:94a7379c0db8 | 157 | } |
cyberjoey | 11:94a7379c0db8 | 158 | |
cyberjoey | 9:36ba3626aab7 | 159 | |
cyberjoey | 9:36ba3626aab7 | 160 | // ***************************************************************************** |
cyberjoey | 9:36ba3626aab7 | 161 | // Define CRC-8 Table |
cyberjoey | 9:36ba3626aab7 | 162 | // ***************************************************************************** |
cyberjoey | 9:36ba3626aab7 | 163 | |
cyberjoey | 9:36ba3626aab7 | 164 | static unsigned char crc_table[] = { |
cyberjoey | 9:36ba3626aab7 | 165 | 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, |
cyberjoey | 9:36ba3626aab7 | 166 | 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, |
cyberjoey | 9:36ba3626aab7 | 167 | 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, |
cyberjoey | 9:36ba3626aab7 | 168 | 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, |
cyberjoey | 9:36ba3626aab7 | 169 | 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, |
cyberjoey | 9:36ba3626aab7 | 170 | 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, |
cyberjoey | 9:36ba3626aab7 | 171 | 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, |
cyberjoey | 9:36ba3626aab7 | 172 | 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, |
cyberjoey | 9:36ba3626aab7 | 173 | 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, |
cyberjoey | 9:36ba3626aab7 | 174 | 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, |
cyberjoey | 9:36ba3626aab7 | 175 | 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, |
cyberjoey | 9:36ba3626aab7 | 176 | 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, |
cyberjoey | 9:36ba3626aab7 | 177 | 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, |
cyberjoey | 9:36ba3626aab7 | 178 | 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, |
cyberjoey | 9:36ba3626aab7 | 179 | 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, |
cyberjoey | 9:36ba3626aab7 | 180 | 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; |
cyberjoey | 9:36ba3626aab7 | 181 | |
cyberjoey | 9:36ba3626aab7 | 182 | |
cyberjoey | 9:36ba3626aab7 | 183 | |
cyberjoey | 9:36ba3626aab7 | 184 | // ----------------------------------------------------------------------------- |
cyberjoey | 9:36ba3626aab7 | 185 | // Calculate the CRC8 of the byte value provided with the current total |
cyberjoey | 9:36ba3626aab7 | 186 | // calc_crc8(unsigned char, unsigned char) crc8 total, byte to add |
cyberjoey | 9:36ba3626aab7 | 187 | // pass crc8 total = 0 to start new crc sum. |
cyberjoey | 9:36ba3626aab7 | 188 | |
cyberjoey | 9:36ba3626aab7 | 189 | // Returns new crc8 total |
cyberjoey | 9:36ba3626aab7 | 190 | // ----------------------------------------------------------------------------- |
cyberjoey | 9:36ba3626aab7 | 191 | |
cyberjoey | 9:36ba3626aab7 | 192 | unsigned char calc_crc8(unsigned char crc8, unsigned char value){ |
cyberjoey | 9:36ba3626aab7 | 193 | |
cyberjoey | 9:36ba3626aab7 | 194 | return crc_table[crc8 ^ value]; |
cyberjoey | 9:36ba3626aab7 | 195 | } |
cyberjoey | 9:36ba3626aab7 | 196 | |
cyberjoey | 9:36ba3626aab7 | 197 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 198 | // OT07_write_register(char, char, char) writes single byte to OT07 |
cyberjoey | 17:401a5bb8d403 | 199 | // char I2C address |
cyberjoey | 17:401a5bb8d403 | 200 | // char OT07 register address |
cyberjoey | 17:401a5bb8d403 | 201 | // char data byte to be writen |
cyberjoey | 17:401a5bb8d403 | 202 | // returns 0 on success ACK, 1 on NACK |
cyberjoey | 17:401a5bb8d403 | 203 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 204 | |
cyberjoey | 17:401a5bb8d403 | 205 | int OT07_write_register(char I2C_add, char reg_add, char byte){ |
cyberjoey | 17:401a5bb8d403 | 206 | char data[2]; |
cyberjoey | 17:401a5bb8d403 | 207 | int error; |
cyberjoey | 17:401a5bb8d403 | 208 | data[0] = reg_add; |
cyberjoey | 17:401a5bb8d403 | 209 | data[1] = byte; |
cyberjoey | 17:401a5bb8d403 | 210 | error = i2c.write(I2C_add,data,2); |
cyberjoey | 17:401a5bb8d403 | 211 | //if(DEBUG)db.printf("wr[%02X %02X %d]\r\n", data[0], data[1], error); |
cyberjoey | 17:401a5bb8d403 | 212 | return error; |
cyberjoey | 17:401a5bb8d403 | 213 | |
cyberjoey | 17:401a5bb8d403 | 214 | } |
cyberjoey | 17:401a5bb8d403 | 215 | |
cyberjoey | 17:401a5bb8d403 | 216 | /// **************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 217 | // OT07_write_register(char, char, char *, int) writes multiple bytes to OT07 |
cyberjoey | 17:401a5bb8d403 | 218 | // char I2C address |
cyberjoey | 17:401a5bb8d403 | 219 | // char OT07 register address |
cyberjoey | 17:401a5bb8d403 | 220 | // char * data vector of bytes to be written |
cyberjoey | 17:401a5bb8d403 | 221 | // int number of bytes to write |
cyberjoey | 17:401a5bb8d403 | 222 | // returns 0 on success ACK, 1 on NACK |
cyberjoey | 17:401a5bb8d403 | 223 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 224 | |
cyberjoey | 17:401a5bb8d403 | 225 | int OT07_write_register(char I2C_add, char reg_add, char *bytes, int n){ |
cyberjoey | 17:401a5bb8d403 | 226 | int i; |
cyberjoey | 17:401a5bb8d403 | 227 | //set start address |
cyberjoey | 17:401a5bb8d403 | 228 | char data[16]; |
cyberjoey | 17:401a5bb8d403 | 229 | int error; |
cyberjoey | 17:401a5bb8d403 | 230 | data[0] = reg_add; |
cyberjoey | 17:401a5bb8d403 | 231 | for(i=1;i<=n;i++){ |
cyberjoey | 17:401a5bb8d403 | 232 | data[i] = bytes[i-1]; |
cyberjoey | 17:401a5bb8d403 | 233 | } |
cyberjoey | 17:401a5bb8d403 | 234 | error = i2c.write(I2C_add,data,n+1); // send n bytes of data |
cyberjoey | 17:401a5bb8d403 | 235 | |
cyberjoey | 17:401a5bb8d403 | 236 | return error; |
cyberjoey | 17:401a5bb8d403 | 237 | } |
cyberjoey | 17:401a5bb8d403 | 238 | |
cyberjoey | 17:401a5bb8d403 | 239 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 240 | // OT07_read_register(char, char, char *, int) writes single byte to OT07 |
cyberjoey | 17:401a5bb8d403 | 241 | // char I2C address |
cyberjoey | 17:401a5bb8d403 | 242 | // char OT07 register address |
cyberjoey | 17:401a5bb8d403 | 243 | // char * data vector for read bytes to be stored in |
cyberjoey | 17:401a5bb8d403 | 244 | // int number of bytes to read |
cyberjoey | 17:401a5bb8d403 | 245 | // returns 0 on success, 1 on fail |
cyberjoey | 17:401a5bb8d403 | 246 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 247 | |
cyberjoey | 17:401a5bb8d403 | 248 | int OT07_read_register(char I2C_add, char reg_add, char *bytes, int n){ |
cyberjoey | 17:401a5bb8d403 | 249 | int error; |
cyberjoey | 17:401a5bb8d403 | 250 | error = i2c.write(I2C_add,®_add,1,1); |
cyberjoey | 17:401a5bb8d403 | 251 | if(error)return error; |
cyberjoey | 17:401a5bb8d403 | 252 | error = i2c.read(I2C_add,bytes,n); |
cyberjoey | 17:401a5bb8d403 | 253 | //if(DEBUG)db.printf("rr e[%d]\r\n",error); |
cyberjoey | 17:401a5bb8d403 | 254 | return error; |
cyberjoey | 17:401a5bb8d403 | 255 | } |
cyberjoey | 17:401a5bb8d403 | 256 | |
cyberjoey | 17:401a5bb8d403 | 257 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 258 | // search_I2C_bus(OT07_struct *) searches I2C address 0xA0, 0xA2, 0xA4 and 0xA6 |
cyberjoey | 17:401a5bb8d403 | 259 | // OT07_struct * structure array to holds I2C address and rom_ids |
cyberjoey | 17:401a5bb8d403 | 260 | // returns number of devices found |
cyberjoey | 17:401a5bb8d403 | 261 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 262 | |
cyberjoey | 17:401a5bb8d403 | 263 | int search_I2C_bus(OT07_struct OT07[]){ |
cyberjoey | 17:401a5bb8d403 | 264 | char data[16]; |
cyberjoey | 17:401a5bb8d403 | 265 | char I2C_add; |
cyberjoey | 17:401a5bb8d403 | 266 | //char GPIO; |
cyberjoey | 17:401a5bb8d403 | 267 | int error; |
cyberjoey | 17:401a5bb8d403 | 268 | int device_count = 0; |
cyberjoey | 17:401a5bb8d403 | 269 | int i; |
cyberjoey | 17:401a5bb8d403 | 270 | int j; |
cyberjoey | 17:401a5bb8d403 | 271 | for(i = 0;i<4;i++){ |
cyberjoey | 17:401a5bb8d403 | 272 | I2C_add = 0xA0 + i*2; |
cyberjoey | 17:401a5bb8d403 | 273 | error = OT07_read_register(I2C_add,0xff,data,1); |
cyberjoey | 17:401a5bb8d403 | 274 | |
cyberjoey | 17:401a5bb8d403 | 275 | if(error == 0){ |
cyberjoey | 17:401a5bb8d403 | 276 | if(data[0] == 0x30){ |
cyberjoey | 17:401a5bb8d403 | 277 | |
cyberjoey | 17:401a5bb8d403 | 278 | OT07[device_count].I2C_address = I2C_add; |
cyberjoey | 17:401a5bb8d403 | 279 | |
cyberjoey | 26:9ae9d097253a | 280 | OT07_read_register(I2C_add,OT07_ROM_ID,data,ID_LENGTH); |
cyberjoey | 26:9ae9d097253a | 281 | for(j=ID_LENGTH-1;j>=0;j--){ |
cyberjoey | 17:401a5bb8d403 | 282 | OT07[device_count].rom_id[j] = data[j]; |
cyberjoey | 17:401a5bb8d403 | 283 | |
cyberjoey | 17:401a5bb8d403 | 284 | } |
cyberjoey | 17:401a5bb8d403 | 285 | device_count++; |
cyberjoey | 17:401a5bb8d403 | 286 | } |
cyberjoey | 17:401a5bb8d403 | 287 | } |
cyberjoey | 17:401a5bb8d403 | 288 | |
cyberjoey | 17:401a5bb8d403 | 289 | } |
cyberjoey | 17:401a5bb8d403 | 290 | return device_count; |
cyberjoey | 17:401a5bb8d403 | 291 | } |
cyberjoey | 17:401a5bb8d403 | 292 | |
cyberjoey | 17:401a5bb8d403 | 293 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 294 | // convert_temperature(char) sends convert command to OT07 device |
cyberjoey | 17:401a5bb8d403 | 295 | // char I2C address |
cyberjoey | 17:401a5bb8d403 | 296 | // ***************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 297 | |
cyberjoey | 17:401a5bb8d403 | 298 | void convert_temperature(char I2C_add){ // set convert bit to start conversion |
cyberjoey | 17:401a5bb8d403 | 299 | |
cyberjoey | 17:401a5bb8d403 | 300 | char data[2]; |
cyberjoey | 17:401a5bb8d403 | 301 | |
cyberjoey | 17:401a5bb8d403 | 302 | //read ADC_SETUP register 0x14 |
cyberjoey | 17:401a5bb8d403 | 303 | OT07_read_register(I2C_add, OT07_ADC_SETUP,data,1); |
cyberjoey | 17:401a5bb8d403 | 304 | |
cyberjoey | 17:401a5bb8d403 | 305 | //mask convert register value with 0x01 and write back register 0x14 |
cyberjoey | 17:401a5bb8d403 | 306 | OT07_write_register(I2C_add, OT07_ADC_SETUP, data[0]|0x01); |
cyberjoey | 17:401a5bb8d403 | 307 | } |
cyberjoey | 17:401a5bb8d403 | 308 | |
cyberjoey | 17:401a5bb8d403 | 309 | //****************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 310 | // get_temperature(char) read temperature from OT07 device FIFO register |
cyberjoey | 17:401a5bb8d403 | 311 | // char I2C address |
cyberjoey | 24:454dc350bb17 | 312 | // returns TempResponse tempC = temperature in oC |
cyberjoey | 24:454dc350bb17 | 313 | // status = register read result |
cyberjoey | 17:401a5bb8d403 | 314 | //****************************************************************************** |
cyberjoey | 17:401a5bb8d403 | 315 | |
cyberjoey | 24:454dc350bb17 | 316 | TempResponse get_temperature(char I2C_add){ |
cyberjoey | 17:401a5bb8d403 | 317 | char data[2]; |
cyberjoey | 17:401a5bb8d403 | 318 | double T; |
cyberjoey | 17:401a5bb8d403 | 319 | int count; |
cyberjoey | 24:454dc350bb17 | 320 | |
cyberjoey | 24:454dc350bb17 | 321 | // Read temperature from FIFO, 2 bytes |
cyberjoey | 24:454dc350bb17 | 322 | int error = OT07_read_register(I2C_add,OT07_FIFO_DATA,data,2); |
cyberjoey | 24:454dc350bb17 | 323 | |
cyberjoey | 17:401a5bb8d403 | 324 | //calculate temperture from data |
cyberjoey | 17:401a5bb8d403 | 325 | count = (int)(data[0]*256 + data[1]); |
cyberjoey | 17:401a5bb8d403 | 326 | if (count >= 32768)count = count - 65536; // 2s comp |
cyberjoey | 17:401a5bb8d403 | 327 | T = (double)count*0.005; |
cyberjoey | 24:454dc350bb17 | 328 | |
cyberjoey | 24:454dc350bb17 | 329 | TempResponse resp; |
cyberjoey | 24:454dc350bb17 | 330 | resp.tempC = T; |
cyberjoey | 24:454dc350bb17 | 331 | resp.status = error; // 1 for nack/error. 0 for ack/success |
cyberjoey | 24:454dc350bb17 | 332 | return resp; |
cyberjoey | 17:401a5bb8d403 | 333 | } |
cyberjoey | 17:401a5bb8d403 | 334 | |
cyberjoey | 9:36ba3626aab7 | 335 | |
cyberjoey | 10:148da21c297e | 336 | void write_settings_file(int interval, bool device_logged[MAX_DEVICES]) |
cyberjoey | 10:148da21c297e | 337 | { |
cyberjoey | 10:148da21c297e | 338 | FILE *fp = fopen(settings_file, "w"); |
cyberjoey | 10:148da21c297e | 339 | if (fp != NULL) |
cyberjoey | 10:148da21c297e | 340 | { |
cyberjoey | 10:148da21c297e | 341 | fprintf(fp, "i %d\r\n", interval); |
cyberjoey | 10:148da21c297e | 342 | |
cyberjoey | 10:148da21c297e | 343 | fprintf(fp, "d"); |
cyberjoey | 10:148da21c297e | 344 | |
cyberjoey | 10:148da21c297e | 345 | for(int i = 0; i < MAX_DEVICES; i++) |
cyberjoey | 10:148da21c297e | 346 | { |
cyberjoey | 10:148da21c297e | 347 | if(device_logged[i] == true) |
cyberjoey | 10:148da21c297e | 348 | { |
cyberjoey | 10:148da21c297e | 349 | fprintf(fp," %d", i); |
cyberjoey | 10:148da21c297e | 350 | } |
cyberjoey | 10:148da21c297e | 351 | } |
cyberjoey | 10:148da21c297e | 352 | fprintf(fp,"\r\n"); |
cyberjoey | 10:148da21c297e | 353 | |
cyberjoey | 10:148da21c297e | 354 | fclose(fp); |
cyberjoey | 10:148da21c297e | 355 | } |
cyberjoey | 10:148da21c297e | 356 | return; |
cyberjoey | 10:148da21c297e | 357 | } |
cyberjoey | 10:148da21c297e | 358 | |
cyberjoey | 20:ba06efe24970 | 359 | void clear_log_file() |
cyberjoey | 20:ba06efe24970 | 360 | { |
cyberjoey | 21:0f358a702561 | 361 | FILE *fp = fopen(log_file, "w"); |
cyberjoey | 20:ba06efe24970 | 362 | if (fp != NULL) |
cyberjoey | 20:ba06efe24970 | 363 | { |
cyberjoey | 20:ba06efe24970 | 364 | fclose(fp); |
cyberjoey | 20:ba06efe24970 | 365 | } |
cyberjoey | 20:ba06efe24970 | 366 | return; |
cyberjoey | 20:ba06efe24970 | 367 | } |
cyberjoey | 20:ba06efe24970 | 368 | |
cyberjoey | 10:148da21c297e | 369 | bool print_settings_file() |
cyberjoey | 10:148da21c297e | 370 | { |
cyberjoey | 10:148da21c297e | 371 | FILE *fp = fopen(settings_file, "r"); |
cyberjoey | 10:148da21c297e | 372 | if (fp != NULL) |
cyberjoey | 10:148da21c297e | 373 | { |
cyberjoey | 10:148da21c297e | 374 | pc.printf("*\r\n"); |
cyberjoey | 10:148da21c297e | 375 | |
cyberjoey | 10:148da21c297e | 376 | // Read contents from file |
cyberjoey | 10:148da21c297e | 377 | char c = fgetc(fp); |
cyberjoey | 11:94a7379c0db8 | 378 | |
cyberjoey | 10:148da21c297e | 379 | while (!feof(fp)) |
cyberjoey | 10:148da21c297e | 380 | { |
cyberjoey | 10:148da21c297e | 381 | pc.printf("%c", c); |
cyberjoey | 10:148da21c297e | 382 | c = fgetc(fp); |
cyberjoey | 10:148da21c297e | 383 | } |
cyberjoey | 10:148da21c297e | 384 | |
cyberjoey | 10:148da21c297e | 385 | pc.printf("*\r\n"); |
cyberjoey | 10:148da21c297e | 386 | |
cyberjoey | 10:148da21c297e | 387 | fclose(fp); |
cyberjoey | 10:148da21c297e | 388 | } |
cyberjoey | 10:148da21c297e | 389 | else |
cyberjoey | 10:148da21c297e | 390 | { |
cyberjoey | 10:148da21c297e | 391 | return false; |
cyberjoey | 10:148da21c297e | 392 | } |
cyberjoey | 10:148da21c297e | 393 | return true; |
cyberjoey | 10:148da21c297e | 394 | } |
cyberjoey | 10:148da21c297e | 395 | |
cyberjoey | 10:148da21c297e | 396 | bool print_log_file() |
cyberjoey | 10:148da21c297e | 397 | { |
cyberjoey | 10:148da21c297e | 398 | FILE *fp = fopen(log_file, "r"); |
cyberjoey | 10:148da21c297e | 399 | if (fp != NULL) |
cyberjoey | 10:148da21c297e | 400 | { |
cyberjoey | 10:148da21c297e | 401 | pc.printf("*\r\n"); |
cyberjoey | 10:148da21c297e | 402 | |
cyberjoey | 10:148da21c297e | 403 | // Read contents from file |
cyberjoey | 10:148da21c297e | 404 | char c = fgetc(fp); |
cyberjoey | 10:148da21c297e | 405 | while (!feof(fp)) |
cyberjoey | 10:148da21c297e | 406 | { |
cyberjoey | 10:148da21c297e | 407 | pc.printf("%c", c); |
cyberjoey | 10:148da21c297e | 408 | c = fgetc(fp); |
cyberjoey | 10:148da21c297e | 409 | } |
cyberjoey | 10:148da21c297e | 410 | |
cyberjoey | 10:148da21c297e | 411 | pc.printf("*\r\n"); |
cyberjoey | 10:148da21c297e | 412 | |
cyberjoey | 10:148da21c297e | 413 | fclose(fp); |
cyberjoey | 10:148da21c297e | 414 | } |
cyberjoey | 10:148da21c297e | 415 | else |
cyberjoey | 10:148da21c297e | 416 | { |
cyberjoey | 10:148da21c297e | 417 | return false; |
cyberjoey | 10:148da21c297e | 418 | } |
cyberjoey | 10:148da21c297e | 419 | return true; |
cyberjoey | 10:148da21c297e | 420 | } |
cyberjoey | 10:148da21c297e | 421 | |
cyberjoey | 11:94a7379c0db8 | 422 | int getline(char **lineptr, int *n, FILE *stream) { |
cyberjoey | 11:94a7379c0db8 | 423 | char *bufptr = NULL; |
cyberjoey | 11:94a7379c0db8 | 424 | char *p = bufptr; |
cyberjoey | 11:94a7379c0db8 | 425 | size_t size; |
cyberjoey | 11:94a7379c0db8 | 426 | int c; |
cyberjoey | 11:94a7379c0db8 | 427 | |
cyberjoey | 11:94a7379c0db8 | 428 | if (lineptr == NULL) { |
cyberjoey | 11:94a7379c0db8 | 429 | return -1; |
cyberjoey | 11:94a7379c0db8 | 430 | } |
cyberjoey | 11:94a7379c0db8 | 431 | if (stream == NULL) { |
cyberjoey | 11:94a7379c0db8 | 432 | return -1; |
cyberjoey | 11:94a7379c0db8 | 433 | } |
cyberjoey | 11:94a7379c0db8 | 434 | if (n == NULL) { |
cyberjoey | 11:94a7379c0db8 | 435 | return -1; |
cyberjoey | 11:94a7379c0db8 | 436 | } |
cyberjoey | 11:94a7379c0db8 | 437 | bufptr = *lineptr; |
cyberjoey | 11:94a7379c0db8 | 438 | size = *n; |
cyberjoey | 11:94a7379c0db8 | 439 | |
cyberjoey | 11:94a7379c0db8 | 440 | c = fgetc(stream); |
cyberjoey | 11:94a7379c0db8 | 441 | if (c == EOF) { |
cyberjoey | 11:94a7379c0db8 | 442 | return -1; |
cyberjoey | 11:94a7379c0db8 | 443 | } |
cyberjoey | 11:94a7379c0db8 | 444 | if (bufptr == NULL) { |
cyberjoey | 11:94a7379c0db8 | 445 | bufptr = (char *)malloc(128); |
cyberjoey | 11:94a7379c0db8 | 446 | if (bufptr == NULL) { |
cyberjoey | 11:94a7379c0db8 | 447 | return -1; |
cyberjoey | 11:94a7379c0db8 | 448 | } |
cyberjoey | 11:94a7379c0db8 | 449 | size = 128; |
cyberjoey | 11:94a7379c0db8 | 450 | } |
cyberjoey | 11:94a7379c0db8 | 451 | p = bufptr; |
cyberjoey | 11:94a7379c0db8 | 452 | while(c != EOF) { |
cyberjoey | 11:94a7379c0db8 | 453 | if ((p - bufptr) > (size - 1)) { |
cyberjoey | 11:94a7379c0db8 | 454 | size = size + 128; |
cyberjoey | 11:94a7379c0db8 | 455 | bufptr = (char *)realloc(bufptr, size); |
cyberjoey | 11:94a7379c0db8 | 456 | if (bufptr == NULL) { |
cyberjoey | 11:94a7379c0db8 | 457 | return -1; |
cyberjoey | 11:94a7379c0db8 | 458 | } |
cyberjoey | 11:94a7379c0db8 | 459 | } |
cyberjoey | 11:94a7379c0db8 | 460 | *p++ = c; |
cyberjoey | 11:94a7379c0db8 | 461 | if (c == '\n') { |
cyberjoey | 11:94a7379c0db8 | 462 | break; |
cyberjoey | 11:94a7379c0db8 | 463 | } |
cyberjoey | 11:94a7379c0db8 | 464 | c = fgetc(stream); |
cyberjoey | 11:94a7379c0db8 | 465 | } |
cyberjoey | 11:94a7379c0db8 | 466 | |
cyberjoey | 11:94a7379c0db8 | 467 | *p++ = '\0'; |
cyberjoey | 11:94a7379c0db8 | 468 | *lineptr = bufptr; |
cyberjoey | 11:94a7379c0db8 | 469 | *n = size; |
cyberjoey | 11:94a7379c0db8 | 470 | |
cyberjoey | 11:94a7379c0db8 | 471 | return p - bufptr - 1; |
cyberjoey | 11:94a7379c0db8 | 472 | } |
cyberjoey | 11:94a7379c0db8 | 473 | |
cyberjoey | 14:480f2398fe6a | 474 | bool print_settings_file_2() |
cyberjoey | 14:480f2398fe6a | 475 | { |
cyberjoey | 14:480f2398fe6a | 476 | char * line = NULL; |
cyberjoey | 14:480f2398fe6a | 477 | int len = 0; |
cyberjoey | 14:480f2398fe6a | 478 | |
cyberjoey | 14:480f2398fe6a | 479 | FILE *fp = fopen(settings_file, "r"); |
cyberjoey | 14:480f2398fe6a | 480 | if (fp != NULL) |
cyberjoey | 14:480f2398fe6a | 481 | { |
cyberjoey | 14:480f2398fe6a | 482 | pc.printf("*\r\n"); |
cyberjoey | 14:480f2398fe6a | 483 | |
cyberjoey | 14:480f2398fe6a | 484 | // Read contents from file |
cyberjoey | 14:480f2398fe6a | 485 | while ((getline(&line, &len, fp)) != -1) |
cyberjoey | 14:480f2398fe6a | 486 | { |
cyberjoey | 14:480f2398fe6a | 487 | pc.printf("%s", line); |
cyberjoey | 14:480f2398fe6a | 488 | } |
cyberjoey | 14:480f2398fe6a | 489 | pc.printf("*\r\n"); |
cyberjoey | 14:480f2398fe6a | 490 | |
cyberjoey | 14:480f2398fe6a | 491 | fclose(fp); |
cyberjoey | 14:480f2398fe6a | 492 | } |
cyberjoey | 14:480f2398fe6a | 493 | else |
cyberjoey | 14:480f2398fe6a | 494 | { |
cyberjoey | 14:480f2398fe6a | 495 | return false; |
cyberjoey | 14:480f2398fe6a | 496 | } |
cyberjoey | 14:480f2398fe6a | 497 | return true; |
cyberjoey | 14:480f2398fe6a | 498 | } |
cyberjoey | 14:480f2398fe6a | 499 | |
cyberjoey | 11:94a7379c0db8 | 500 | //returns true if settings file exists and is in the proper format |
cyberjoey | 11:94a7379c0db8 | 501 | bool apply_settings_file(bool (&logged_devices)[MAX_DEVICES], int& interval) |
cyberjoey | 11:94a7379c0db8 | 502 | { |
cyberjoey | 11:94a7379c0db8 | 503 | |
cyberjoey | 11:94a7379c0db8 | 504 | char * line = NULL; |
cyberjoey | 11:94a7379c0db8 | 505 | int len = 0; |
cyberjoey | 11:94a7379c0db8 | 506 | int line_number = 0; |
cyberjoey | 11:94a7379c0db8 | 507 | |
cyberjoey | 11:94a7379c0db8 | 508 | |
cyberjoey | 11:94a7379c0db8 | 509 | FILE *fp = fopen("/sd/settings.txt", "r"); |
cyberjoey | 11:94a7379c0db8 | 510 | if (fp != NULL) |
cyberjoey | 11:94a7379c0db8 | 511 | { |
cyberjoey | 11:94a7379c0db8 | 512 | |
cyberjoey | 11:94a7379c0db8 | 513 | //initialize devices to all false; |
cyberjoey | 11:94a7379c0db8 | 514 | for(int i = 0; i < MAX_DEVICES; i++) |
cyberjoey | 11:94a7379c0db8 | 515 | { |
cyberjoey | 11:94a7379c0db8 | 516 | logged_devices[i] = false; |
cyberjoey | 11:94a7379c0db8 | 517 | } |
cyberjoey | 11:94a7379c0db8 | 518 | |
cyberjoey | 11:94a7379c0db8 | 519 | // Read contents from file |
cyberjoey | 11:94a7379c0db8 | 520 | while ((getline(&line, &len, fp)) != -1) |
cyberjoey | 11:94a7379c0db8 | 521 | { |
cyberjoey | 11:94a7379c0db8 | 522 | line_number++; |
cyberjoey | 11:94a7379c0db8 | 523 | |
cyberjoey | 11:94a7379c0db8 | 524 | char i = 0; |
cyberjoey | 11:94a7379c0db8 | 525 | char c = line[i]; |
cyberjoey | 11:94a7379c0db8 | 526 | while(c != '\0') |
cyberjoey | 11:94a7379c0db8 | 527 | { |
cyberjoey | 11:94a7379c0db8 | 528 | int number; |
cyberjoey | 11:94a7379c0db8 | 529 | int n; |
cyberjoey | 11:94a7379c0db8 | 530 | sscanf((line+i), "%d%n", &number, &n); |
cyberjoey | 11:94a7379c0db8 | 531 | if(isdigit(c)) |
cyberjoey | 11:94a7379c0db8 | 532 | { |
cyberjoey | 11:94a7379c0db8 | 533 | if(line_number == 1) |
cyberjoey | 11:94a7379c0db8 | 534 | { |
cyberjoey | 11:94a7379c0db8 | 535 | interval = number; |
cyberjoey | 11:94a7379c0db8 | 536 | } |
cyberjoey | 11:94a7379c0db8 | 537 | else if(line_number == 2) |
cyberjoey | 11:94a7379c0db8 | 538 | { |
cyberjoey | 11:94a7379c0db8 | 539 | logged_devices[number] = true; |
cyberjoey | 11:94a7379c0db8 | 540 | } |
cyberjoey | 11:94a7379c0db8 | 541 | if(n > 1) |
cyberjoey | 11:94a7379c0db8 | 542 | i = i + (n - 1); |
cyberjoey | 11:94a7379c0db8 | 543 | } |
cyberjoey | 11:94a7379c0db8 | 544 | i++; |
cyberjoey | 11:94a7379c0db8 | 545 | c = line[i]; |
cyberjoey | 11:94a7379c0db8 | 546 | } |
cyberjoey | 11:94a7379c0db8 | 547 | } |
cyberjoey | 11:94a7379c0db8 | 548 | |
cyberjoey | 11:94a7379c0db8 | 549 | fclose(fp); |
cyberjoey | 11:94a7379c0db8 | 550 | } |
cyberjoey | 11:94a7379c0db8 | 551 | else |
cyberjoey | 11:94a7379c0db8 | 552 | { |
cyberjoey | 11:94a7379c0db8 | 553 | return false; |
cyberjoey | 11:94a7379c0db8 | 554 | } |
cyberjoey | 11:94a7379c0db8 | 555 | |
cyberjoey | 11:94a7379c0db8 | 556 | return true; |
cyberjoey | 11:94a7379c0db8 | 557 | |
cyberjoey | 11:94a7379c0db8 | 558 | } |
cyberjoey | 11:94a7379c0db8 | 559 | |
cyberjoey | 9:36ba3626aab7 | 560 | //****************************************************************************** |
cyberjoey | 9:36ba3626aab7 | 561 | // main() |
cyberjoey | 9:36ba3626aab7 | 562 | //****************************************************************************** |
cyberjoey | 9:36ba3626aab7 | 563 | |
cyberjoey | 9:36ba3626aab7 | 564 | |
switches | 0:60a522ae2e35 | 565 | int main() |
switches | 0:60a522ae2e35 | 566 | { |
cyberjoey | 17:401a5bb8d403 | 567 | OT07_struct OT07[4]; // structure that holds I2C address and ROM_ID |
cyberjoey | 9:36ba3626aab7 | 568 | char data[130]; |
cyberjoey | 9:36ba3626aab7 | 569 | int device_count = 0; // number of OW devices found by search_rom() |
cyberjoey | 9:36ba3626aab7 | 570 | int i; |
cyberjoey | 9:36ba3626aab7 | 571 | int j; |
cyberjoey | 9:36ba3626aab7 | 572 | int k; |
cyberjoey | 9:36ba3626aab7 | 573 | |
cyberjoey | 9:36ba3626aab7 | 574 | |
cyberjoey | 9:36ba3626aab7 | 575 | char device_id[ID_LENGTH]; //8 byte rom id of current slected device |
cyberjoey | 9:36ba3626aab7 | 576 | char rom_id_list[MAX_DEVICES][ID_LENGTH]; //List of rom id for each device on OW bus |
cyberjoey | 9:36ba3626aab7 | 577 | |
cyberjoey | 24:454dc350bb17 | 578 | TempResponse T[MAX_DEVICES]; |
cyberjoey | 9:36ba3626aab7 | 579 | |
cyberjoey | 10:148da21c297e | 580 | bool device_logged[MAX_DEVICES]; |
cyberjoey | 11:94a7379c0db8 | 581 | //initialize device_logged to all false; |
cyberjoey | 11:94a7379c0db8 | 582 | for(int i = 0; i < MAX_DEVICES; i++) |
cyberjoey | 11:94a7379c0db8 | 583 | { |
cyberjoey | 11:94a7379c0db8 | 584 | device_logged[i] = false; |
cyberjoey | 11:94a7379c0db8 | 585 | } |
cyberjoey | 10:148da21c297e | 586 | |
cyberjoey | 9:36ba3626aab7 | 587 | // i/o variables |
cyberjoey | 9:36ba3626aab7 | 588 | char rx_buff[128]; // comport input buffer |
cyberjoey | 9:36ba3626aab7 | 589 | int rx_index; // rx_buffer pointer |
cyberjoey | 9:36ba3626aab7 | 590 | char c; // command type character |
cyberjoey | 9:36ba3626aab7 | 591 | int n; // argument count |
cyberjoey | 9:36ba3626aab7 | 592 | int arg1; // argumnet 1 |
cyberjoey | 9:36ba3626aab7 | 593 | int arg2; // argument 2 |
cyberjoey | 9:36ba3626aab7 | 594 | int device; // device argument |
cyberjoey | 9:36ba3626aab7 | 595 | int num_bytes; |
cyberjoey | 9:36ba3626aab7 | 596 | |
cyberjoey | 9:36ba3626aab7 | 597 | int time_count; |
cyberjoey | 9:36ba3626aab7 | 598 | int log_interval = PERIOD; |
cyberjoey | 9:36ba3626aab7 | 599 | int time_to_sample; |
cyberjoey | 9:36ba3626aab7 | 600 | |
cyberjoey | 11:94a7379c0db8 | 601 | apply_settings_file(device_logged, log_interval); |
cyberjoey | 11:94a7379c0db8 | 602 | |
cyberjoey | 9:36ba3626aab7 | 603 | //************* init ticker timer callbacks **************** |
cyberjoey | 12:aa9fff0aec91 | 604 | timer_1.attach(&LED_blink_callback,0.5); //start ticker, once per sec. |
cyberjoey | 9:36ba3626aab7 | 605 | |
cyberjoey | 9:36ba3626aab7 | 606 | |
cyberjoey | 12:aa9fff0aec91 | 607 | i2c.frequency(400000); //set I2C clock to 400kHz |
cyberjoey | 9:36ba3626aab7 | 608 | |
cyberjoey | 9:36ba3626aab7 | 609 | rLED = LED_OFF; |
switches | 6:96d8d823e292 | 610 | gLED = LED_ON; |
cyberjoey | 9:36ba3626aab7 | 611 | bLED = LED_ON; |
cyberjoey | 26:9ae9d097253a | 612 | |
cyberjoey | 26:9ae9d097253a | 613 | // ****************** search for all devices on bus ***************** |
cyberjoey | 9:36ba3626aab7 | 614 | |
cyberjoey | 17:401a5bb8d403 | 615 | device_count = search_I2C_bus(OT07); |
cyberjoey | 9:36ba3626aab7 | 616 | |
cyberjoey | 9:36ba3626aab7 | 617 | rx_index = 0; //character buffer index for input from PC |
cyberjoey | 9:36ba3626aab7 | 618 | |
cyberjoey | 9:36ba3626aab7 | 619 | button.fall(&btn_pressed); |
cyberjoey | 11:94a7379c0db8 | 620 | SDInsert.fall(&sd_insert); |
cyberjoey | 11:94a7379c0db8 | 621 | |
cyberjoey | 11:94a7379c0db8 | 622 | sd.disk_initialize(); //initialize sd card |
cyberjoey | 9:36ba3626aab7 | 623 | |
cyberjoey | 9:36ba3626aab7 | 624 | while(1) { // start main loop, take data if logging, check for input, repeat |
cyberjoey | 9:36ba3626aab7 | 625 | |
cyberjoey | 11:94a7379c0db8 | 626 | if(sd_insert_flag == true) |
cyberjoey | 11:94a7379c0db8 | 627 | { |
cyberjoey | 11:94a7379c0db8 | 628 | sd.disk_initialize(); |
cyberjoey | 11:94a7379c0db8 | 629 | sd_insert_flag = false; |
cyberjoey | 11:94a7379c0db8 | 630 | } |
cyberjoey | 11:94a7379c0db8 | 631 | |
cyberjoey | 9:36ba3626aab7 | 632 | if(button_flag == true) |
cyberjoey | 9:36ba3626aab7 | 633 | { |
cyberjoey | 9:36ba3626aab7 | 634 | if(log_flag == false){ //start logging |
cyberjoey | 9:36ba3626aab7 | 635 | |
cyberjoey | 9:36ba3626aab7 | 636 | if(SDDetect)//if SD card not detected |
cyberjoey | 9:36ba3626aab7 | 637 | { |
cyberjoey | 9:36ba3626aab7 | 638 | error_flag = true; |
cyberjoey | 9:36ba3626aab7 | 639 | error_ticks = 6; |
cyberjoey | 9:36ba3626aab7 | 640 | log_flag = false; |
cyberjoey | 9:36ba3626aab7 | 641 | } |
cyberjoey | 9:36ba3626aab7 | 642 | else |
cyberjoey | 9:36ba3626aab7 | 643 | { |
cyberjoey | 11:94a7379c0db8 | 644 | apply_settings_file(device_logged, log_interval); |
cyberjoey | 11:94a7379c0db8 | 645 | |
cyberjoey | 10:148da21c297e | 646 | FILE *fp = fopen(log_file, "a"); |
cyberjoey | 9:36ba3626aab7 | 647 | if (fp != NULL) |
cyberjoey | 9:36ba3626aab7 | 648 | { |
cyberjoey | 17:401a5bb8d403 | 649 | fprintf(fp,"Device List\r\n"); |
cyberjoey | 27:87f7adba07d7 | 650 | fprintf(fp,"Device Number,Address,Unique ID\r\n"); |
cyberjoey | 17:401a5bb8d403 | 651 | for(j=0;j<device_count;j++) |
cyberjoey | 17:401a5bb8d403 | 652 | { |
cyberjoey | 17:401a5bb8d403 | 653 | if(device_logged[j]) |
cyberjoey | 17:401a5bb8d403 | 654 | { |
cyberjoey | 23:aa2407a3b30d | 655 | fprintf(fp,"%d,0x%02X,0x",j, OT07[j].I2C_address); |
cyberjoey | 26:9ae9d097253a | 656 | for(i=ID_LENGTH-1;i>=0;i--) |
cyberjoey | 17:401a5bb8d403 | 657 | { |
cyberjoey | 21:0f358a702561 | 658 | fprintf(fp,"%02X",OT07[j].rom_id[i]); |
cyberjoey | 17:401a5bb8d403 | 659 | } |
cyberjoey | 17:401a5bb8d403 | 660 | fprintf(fp,"\r\n"); |
cyberjoey | 17:401a5bb8d403 | 661 | } |
cyberjoey | 17:401a5bb8d403 | 662 | } |
cyberjoey | 23:aa2407a3b30d | 663 | fprintf(fp, "Temperature Log\r\n"); |
cyberjoey | 23:aa2407a3b30d | 664 | fprintf(fp, "Time (s)"); |
cyberjoey | 9:36ba3626aab7 | 665 | |
cyberjoey | 9:36ba3626aab7 | 666 | for(j=0;j<device_count;j++) |
cyberjoey | 9:36ba3626aab7 | 667 | { |
cyberjoey | 11:94a7379c0db8 | 668 | if(device_logged[j]) |
cyberjoey | 16:2f373d3c8214 | 669 | fprintf(fp,",Device %d Temperature (C)",j); |
cyberjoey | 9:36ba3626aab7 | 670 | } |
cyberjoey | 9:36ba3626aab7 | 671 | fprintf(fp,"\r\n"); |
cyberjoey | 9:36ba3626aab7 | 672 | |
cyberjoey | 9:36ba3626aab7 | 673 | fclose(fp); |
cyberjoey | 9:36ba3626aab7 | 674 | } |
cyberjoey | 9:36ba3626aab7 | 675 | |
cyberjoey | 9:36ba3626aab7 | 676 | time_count = 0; |
cyberjoey | 9:36ba3626aab7 | 677 | |
cyberjoey | 9:36ba3626aab7 | 678 | time_to_sample = 0; // force sample at time = 0; |
cyberjoey | 9:36ba3626aab7 | 679 | tick_flag = true; // force sample at time = 0; |
cyberjoey | 9:36ba3626aab7 | 680 | log_flag = true; |
cyberjoey | 9:36ba3626aab7 | 681 | } |
cyberjoey | 9:36ba3626aab7 | 682 | }else{ |
cyberjoey | 9:36ba3626aab7 | 683 | log_flag = false; |
cyberjoey | 9:36ba3626aab7 | 684 | } |
cyberjoey | 9:36ba3626aab7 | 685 | button_flag = false; |
cyberjoey | 9:36ba3626aab7 | 686 | } |
switches | 1:90313362ec11 | 687 | |
cyberjoey | 9:36ba3626aab7 | 688 | while(tick_flag == false){ //check for input while waiting for next tick |
cyberjoey | 9:36ba3626aab7 | 689 | |
cyberjoey | 9:36ba3626aab7 | 690 | // ---------------------------------------------------------------------------- |
cyberjoey | 9:36ba3626aab7 | 691 | // test for charater input for USB com port |
cyberjoey | 9:36ba3626aab7 | 692 | // ---------------------------------------------------------------------------- |
cyberjoey | 9:36ba3626aab7 | 693 | |
cyberjoey | 9:36ba3626aab7 | 694 | //test if PC sent some charaters |
cyberjoey | 9:36ba3626aab7 | 695 | while(pc.readable()){ //characters in buffer, get them |
cyberjoey | 9:36ba3626aab7 | 696 | rx_buff[rx_index] = pc.getc(); |
cyberjoey | 9:36ba3626aab7 | 697 | |
cyberjoey | 9:36ba3626aab7 | 698 | if(rx_buff[rx_index] == CR){ |
cyberjoey | 9:36ba3626aab7 | 699 | rx_buff[++rx_index] = 0; |
cyberjoey | 9:36ba3626aab7 | 700 | rx_index = -1; // because i++ at end of while give i=0 on next loop |
cyberjoey | 9:36ba3626aab7 | 701 | device = 0; |
cyberjoey | 9:36ba3626aab7 | 702 | arg1 = 0; |
cyberjoey | 9:36ba3626aab7 | 703 | arg2 = 0; |
cyberjoey | 9:36ba3626aab7 | 704 | |
cyberjoey | 9:36ba3626aab7 | 705 | n = sscanf(rx_buff, " %c %d %x %x", &c, & device, &arg1, &arg2); |
cyberjoey | 9:36ba3626aab7 | 706 | //process input |
cyberjoey | 9:36ba3626aab7 | 707 | if(n > 0){//got input so process it |
cyberjoey | 9:36ba3626aab7 | 708 | switch(c){ |
cyberjoey | 9:36ba3626aab7 | 709 | case 'a': |
cyberjoey | 26:9ae9d097253a | 710 | case 'A': |
cyberjoey | 9:36ba3626aab7 | 711 | break; |
cyberjoey | 11:94a7379c0db8 | 712 | case 'c': |
cyberjoey | 11:94a7379c0db8 | 713 | case 'C': |
cyberjoey | 11:94a7379c0db8 | 714 | if(!SDDetect) |
cyberjoey | 11:94a7379c0db8 | 715 | pc.printf("y\r\n"); |
cyberjoey | 11:94a7379c0db8 | 716 | else |
cyberjoey | 11:94a7379c0db8 | 717 | pc.printf("n\r\n"); |
cyberjoey | 11:94a7379c0db8 | 718 | break; |
cyberjoey | 10:148da21c297e | 719 | case 'd': |
cyberjoey | 10:148da21c297e | 720 | case 'D': |
cyberjoey | 10:148da21c297e | 721 | if(n==1) // if no device number is given |
cyberjoey | 10:148da21c297e | 722 | { |
cyberjoey | 10:148da21c297e | 723 | //clear device_logged array |
cyberjoey | 10:148da21c297e | 724 | for(int i = 0; i < MAX_DEVICES; i++) |
cyberjoey | 10:148da21c297e | 725 | { |
cyberjoey | 10:148da21c297e | 726 | device_logged[i] = false; |
cyberjoey | 10:148da21c297e | 727 | } |
cyberjoey | 10:148da21c297e | 728 | |
cyberjoey | 10:148da21c297e | 729 | write_settings_file(log_interval, device_logged); |
cyberjoey | 10:148da21c297e | 730 | } |
cyberjoey | 10:148da21c297e | 731 | if(n == 2) |
cyberjoey | 10:148da21c297e | 732 | { |
cyberjoey | 10:148da21c297e | 733 | device_logged[device] = true; |
cyberjoey | 10:148da21c297e | 734 | write_settings_file(log_interval, device_logged); |
cyberjoey | 10:148da21c297e | 735 | } |
cyberjoey | 10:148da21c297e | 736 | break; |
cyberjoey | 9:36ba3626aab7 | 737 | case 'f': |
cyberjoey | 9:36ba3626aab7 | 738 | case 'F': //f is for "flash" for microSD |
cyberjoey | 12:aa9fff0aec91 | 739 | if(!SDDetect) |
cyberjoey | 9:36ba3626aab7 | 740 | pc.printf("y\r\n"); |
cyberjoey | 9:36ba3626aab7 | 741 | else |
cyberjoey | 12:aa9fff0aec91 | 742 | pc.printf("n\r\n"); |
cyberjoey | 9:36ba3626aab7 | 743 | break; |
cyberjoey | 10:148da21c297e | 744 | case 'g': |
cyberjoey | 10:148da21c297e | 745 | case 'G': |
cyberjoey | 10:148da21c297e | 746 | if(n == 2) |
cyberjoey | 10:148da21c297e | 747 | { |
cyberjoey | 21:0f358a702561 | 748 | //0 means get config, 1 means get log, 2 means clear log |
cyberjoey | 10:148da21c297e | 749 | bool fileExists = false; |
cyberjoey | 10:148da21c297e | 750 | if(device == 0)//get config |
cyberjoey | 10:148da21c297e | 751 | { |
cyberjoey | 10:148da21c297e | 752 | fileExists = print_settings_file(); |
cyberjoey | 10:148da21c297e | 753 | } |
cyberjoey | 10:148da21c297e | 754 | if(device == 1)//get log |
cyberjoey | 10:148da21c297e | 755 | { |
cyberjoey | 10:148da21c297e | 756 | fileExists = print_log_file(); |
cyberjoey | 10:148da21c297e | 757 | } |
cyberjoey | 21:0f358a702561 | 758 | if(device == 2)//get log |
cyberjoey | 21:0f358a702561 | 759 | { |
cyberjoey | 21:0f358a702561 | 760 | clear_log_file(); |
cyberjoey | 21:0f358a702561 | 761 | fileExists = true; |
cyberjoey | 21:0f358a702561 | 762 | } |
cyberjoey | 10:148da21c297e | 763 | if(!fileExists) |
cyberjoey | 10:148da21c297e | 764 | { |
cyberjoey | 10:148da21c297e | 765 | pc.printf("no_file\r\n"); |
cyberjoey | 10:148da21c297e | 766 | } |
cyberjoey | 10:148da21c297e | 767 | } |
cyberjoey | 20:ba06efe24970 | 768 | // read all registers command |
cyberjoey | 20:ba06efe24970 | 769 | //i.e. ("g 0 0" means readAll from device 0 "g 1 0" means readAll from device 1) |
cyberjoey | 20:ba06efe24970 | 770 | if(n == 3) |
cyberjoey | 20:ba06efe24970 | 771 | { |
cyberjoey | 20:ba06efe24970 | 772 | //device holds device number 0 means readAll command |
cyberjoey | 20:ba06efe24970 | 773 | OT07_read_register(OT07[device].I2C_address,0x00,data,1); |
cyberjoey | 29:88f36da6a119 | 774 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x00,data[0]); |
cyberjoey | 20:ba06efe24970 | 775 | OT07_read_register(OT07[device].I2C_address,0x01,data,1); |
cyberjoey | 29:88f36da6a119 | 776 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x01,data[0]); |
cyberjoey | 20:ba06efe24970 | 777 | OT07_read_register(OT07[device].I2C_address,0x04,data,1); |
cyberjoey | 29:88f36da6a119 | 778 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x04,data[0]); |
cyberjoey | 20:ba06efe24970 | 779 | OT07_read_register(OT07[device].I2C_address,0x05,data,1); |
cyberjoey | 29:88f36da6a119 | 780 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x05,data[0]); |
cyberjoey | 20:ba06efe24970 | 781 | OT07_read_register(OT07[device].I2C_address,0x06,data,1); |
cyberjoey | 29:88f36da6a119 | 782 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x06,data[0]); |
cyberjoey | 20:ba06efe24970 | 783 | OT07_read_register(OT07[device].I2C_address,0x07,data,1); |
cyberjoey | 29:88f36da6a119 | 784 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x07,data[0]); |
cyberjoey | 20:ba06efe24970 | 785 | OT07_read_register(OT07[device].I2C_address,0x08,data,1); |
cyberjoey | 29:88f36da6a119 | 786 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x08,data[0]); |
cyberjoey | 20:ba06efe24970 | 787 | OT07_read_register(OT07[device].I2C_address,0x09,data,1); |
cyberjoey | 29:88f36da6a119 | 788 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x09,data[0]); |
cyberjoey | 20:ba06efe24970 | 789 | OT07_read_register(OT07[device].I2C_address,0x0A,data,1); |
cyberjoey | 29:88f36da6a119 | 790 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x0A,data[0]); |
cyberjoey | 20:ba06efe24970 | 791 | OT07_read_register(OT07[device].I2C_address,0x0C,data,1); |
cyberjoey | 29:88f36da6a119 | 792 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x0C,data[0]); |
cyberjoey | 20:ba06efe24970 | 793 | OT07_read_register(OT07[device].I2C_address,0x10,data,1); |
cyberjoey | 29:88f36da6a119 | 794 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x10,data[0]); |
cyberjoey | 20:ba06efe24970 | 795 | OT07_read_register(OT07[device].I2C_address,0x11,data,1); |
cyberjoey | 29:88f36da6a119 | 796 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x11,data[0]); |
cyberjoey | 20:ba06efe24970 | 797 | OT07_read_register(OT07[device].I2C_address,0x12,data,1); |
cyberjoey | 29:88f36da6a119 | 798 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x12,data[0]); |
cyberjoey | 20:ba06efe24970 | 799 | OT07_read_register(OT07[device].I2C_address,0x13,data,1); |
cyberjoey | 29:88f36da6a119 | 800 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x13,data[0]); |
cyberjoey | 20:ba06efe24970 | 801 | OT07_read_register(OT07[device].I2C_address,0x14,data,1); |
cyberjoey | 29:88f36da6a119 | 802 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x14,data[0]); |
cyberjoey | 20:ba06efe24970 | 803 | OT07_read_register(OT07[device].I2C_address,0x20,data,1); |
cyberjoey | 29:88f36da6a119 | 804 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x20,data[0]); |
cyberjoey | 20:ba06efe24970 | 805 | OT07_read_register(OT07[device].I2C_address,0x21,data,1); |
cyberjoey | 29:88f36da6a119 | 806 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x21,data[0]); |
cyberjoey | 27:87f7adba07d7 | 807 | //OT07_read_register(OT07[device].I2C_address,0x30,data,1); |
cyberjoey | 29:88f36da6a119 | 808 | //pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x30,data[0]); |
cyberjoey | 20:ba06efe24970 | 809 | OT07_read_register(OT07[device].I2C_address,0x31,data,1); |
cyberjoey | 29:88f36da6a119 | 810 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x31,data[0]); |
cyberjoey | 20:ba06efe24970 | 811 | OT07_read_register(OT07[device].I2C_address,0x32,data,1); |
cyberjoey | 29:88f36da6a119 | 812 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x32,data[0]); |
cyberjoey | 20:ba06efe24970 | 813 | OT07_read_register(OT07[device].I2C_address,0x33,data,1); |
cyberjoey | 29:88f36da6a119 | 814 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x33,data[0]); |
cyberjoey | 20:ba06efe24970 | 815 | OT07_read_register(OT07[device].I2C_address,0x34,data,1); |
cyberjoey | 29:88f36da6a119 | 816 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x34,data[0]); |
cyberjoey | 20:ba06efe24970 | 817 | OT07_read_register(OT07[device].I2C_address,0x35,data,1); |
cyberjoey | 29:88f36da6a119 | 818 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x35,data[0]); |
cyberjoey | 20:ba06efe24970 | 819 | OT07_read_register(OT07[device].I2C_address,0x36,data,1); |
cyberjoey | 29:88f36da6a119 | 820 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x36,data[0]); |
cyberjoey | 27:87f7adba07d7 | 821 | //OT07_read_register(OT07[device].I2C_address,0x37,data,1); |
cyberjoey | 29:88f36da6a119 | 822 | //pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0x37,data[0]); |
cyberjoey | 20:ba06efe24970 | 823 | OT07_read_register(OT07[device].I2C_address,0xFF,data,1); |
cyberjoey | 29:88f36da6a119 | 824 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,0xFF,data[0]); |
cyberjoey | 20:ba06efe24970 | 825 | } |
cyberjoey | 10:148da21c297e | 826 | break; |
cyberjoey | 9:36ba3626aab7 | 827 | case 'i': |
cyberjoey | 9:36ba3626aab7 | 828 | case 'I': //Set Logging sample intreval in seconds |
cyberjoey | 9:36ba3626aab7 | 829 | |
cyberjoey | 9:36ba3626aab7 | 830 | log_interval = device; |
cyberjoey | 9:36ba3626aab7 | 831 | if(log_interval < 1)log_interval = 1; |
cyberjoey | 10:148da21c297e | 832 | if(log_interval > 60)log_interval = 60; |
cyberjoey | 10:148da21c297e | 833 | write_settings_file(log_interval, device_logged); |
cyberjoey | 9:36ba3626aab7 | 834 | break; |
cyberjoey | 9:36ba3626aab7 | 835 | case 'l': |
cyberjoey | 28:fe9385ce2d00 | 836 | case 'L': |
cyberjoey | 17:401a5bb8d403 | 837 | /* |
cyberjoey | 17:401a5bb8d403 | 838 | // Toggle logging |
cyberjoey | 9:36ba3626aab7 | 839 | |
cyberjoey | 9:36ba3626aab7 | 840 | if(log_flag == false){ //start logging |
cyberjoey | 9:36ba3626aab7 | 841 | if(SDDetect)//if SD card not detected |
cyberjoey | 9:36ba3626aab7 | 842 | { |
cyberjoey | 9:36ba3626aab7 | 843 | error_flag = true; |
cyberjoey | 9:36ba3626aab7 | 844 | error_ticks = 6; |
cyberjoey | 9:36ba3626aab7 | 845 | log_flag = false; |
cyberjoey | 9:36ba3626aab7 | 846 | } |
cyberjoey | 9:36ba3626aab7 | 847 | else |
cyberjoey | 9:36ba3626aab7 | 848 | { |
cyberjoey | 10:148da21c297e | 849 | FILE *fp = fopen(log_file, "a"); |
cyberjoey | 9:36ba3626aab7 | 850 | if (fp != NULL) |
cyberjoey | 9:36ba3626aab7 | 851 | { |
cyberjoey | 9:36ba3626aab7 | 852 | fprintf(fp, "Time(s)"); |
cyberjoey | 9:36ba3626aab7 | 853 | |
cyberjoey | 9:36ba3626aab7 | 854 | for(j=0;j<device_count;j++) |
cyberjoey | 9:36ba3626aab7 | 855 | { |
cyberjoey | 16:2f373d3c8214 | 856 | fprintf(fp,",Device %d Temperature (C)",j); |
cyberjoey | 9:36ba3626aab7 | 857 | } |
cyberjoey | 9:36ba3626aab7 | 858 | fprintf(fp,"\r\n"); |
cyberjoey | 9:36ba3626aab7 | 859 | |
cyberjoey | 9:36ba3626aab7 | 860 | fclose(fp); |
cyberjoey | 9:36ba3626aab7 | 861 | } |
cyberjoey | 9:36ba3626aab7 | 862 | |
cyberjoey | 9:36ba3626aab7 | 863 | time_count = 0; |
cyberjoey | 9:36ba3626aab7 | 864 | |
cyberjoey | 9:36ba3626aab7 | 865 | time_to_sample = 0; // force sample at time = 0; |
cyberjoey | 9:36ba3626aab7 | 866 | tick_flag = true; // force sample at time = 0; |
cyberjoey | 9:36ba3626aab7 | 867 | log_flag = true; |
cyberjoey | 9:36ba3626aab7 | 868 | } |
cyberjoey | 9:36ba3626aab7 | 869 | }else{ |
cyberjoey | 9:36ba3626aab7 | 870 | //pc.printf("<stop logging>\r\n"); |
cyberjoey | 9:36ba3626aab7 | 871 | log_flag = false; |
cyberjoey | 17:401a5bb8d403 | 872 | }*/ |
cyberjoey | 9:36ba3626aab7 | 873 | break; |
cyberjoey | 9:36ba3626aab7 | 874 | case 'r': |
cyberjoey | 17:401a5bb8d403 | 875 | case 'R': |
cyberjoey | 17:401a5bb8d403 | 876 | //read register "r device radd.start radd.end" |
cyberjoey | 9:36ba3626aab7 | 877 | if(n==3){ //read single register from selected device |
cyberjoey | 17:401a5bb8d403 | 878 | OT07_read_register(OT07[device].I2C_address,arg1,data,1); |
cyberjoey | 29:88f36da6a119 | 879 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,arg1,data[0]); |
cyberjoey | 9:36ba3626aab7 | 880 | } |
cyberjoey | 9:36ba3626aab7 | 881 | if(n==4){ //read a range of regesters from selected device |
cyberjoey | 9:36ba3626aab7 | 882 | num_bytes = arg2-arg1 + 1; // calculate number of bytes to read |
cyberjoey | 9:36ba3626aab7 | 883 | if (num_bytes < 1) num_bytes = 1; // if arg2 <= arg 1 just read arg1 address. |
cyberjoey | 17:401a5bb8d403 | 884 | OT07_read_register(OT07[device].I2C_address,arg1,data,num_bytes); |
cyberjoey | 9:36ba3626aab7 | 885 | for(i=0;i<num_bytes;i++){ |
cyberjoey | 20:ba06efe24970 | 886 | pc.printf("device[%02X] add[%02X] data[%02X]\r\n",device,arg1+i,data[i]); |
cyberjoey | 17:401a5bb8d403 | 887 | } |
cyberjoey | 9:36ba3626aab7 | 888 | } |
cyberjoey | 9:36ba3626aab7 | 889 | break; |
cyberjoey | 9:36ba3626aab7 | 890 | |
cyberjoey | 9:36ba3626aab7 | 891 | case 's': |
cyberjoey | 28:fe9385ce2d00 | 892 | case 'S': |
cyberjoey | 17:401a5bb8d403 | 893 | // ****************** search for I2C devices on bus ***************** |
cyberjoey | 17:401a5bb8d403 | 894 | device_count = search_I2C_bus(OT07); |
cyberjoey | 9:36ba3626aab7 | 895 | pc.printf("%d devices:\r\n", device_count); |
cyberjoey | 9:36ba3626aab7 | 896 | for(j=0;j<device_count;j++){ |
cyberjoey | 27:87f7adba07d7 | 897 | pc.printf("device[%02X] addr[%02X] unique id[",j, OT07[j].I2C_address); |
cyberjoey | 26:9ae9d097253a | 898 | for(i=ID_LENGTH-1;i>=0;i--){ |
cyberjoey | 17:401a5bb8d403 | 899 | pc.printf("%02X",OT07[j].rom_id[i]); |
cyberjoey | 9:36ba3626aab7 | 900 | } |
cyberjoey | 9:36ba3626aab7 | 901 | pc.printf("]\r\n"); |
cyberjoey | 13:674c647d12dd | 902 | } |
cyberjoey | 17:401a5bb8d403 | 903 | |
cyberjoey | 9:36ba3626aab7 | 904 | break; |
cyberjoey | 9:36ba3626aab7 | 905 | |
cyberjoey | 9:36ba3626aab7 | 906 | case 'T': |
cyberjoey | 9:36ba3626aab7 | 907 | case 't': |
cyberjoey | 17:401a5bb8d403 | 908 | |
cyberjoey | 17:401a5bb8d403 | 909 | if(n == 2){//get temperatures from selected device |
cyberjoey | 17:401a5bb8d403 | 910 | |
cyberjoey | 17:401a5bb8d403 | 911 | convert_temperature(OT07[device].I2C_address); //send OW convert selected device |
cyberjoey | 30:3a1d9c2b49a5 | 912 | wait_ms(CONVERT_T_DELAY); //wait 20 ms for convert temperature to complete |
cyberjoey | 17:401a5bb8d403 | 913 | T[device] = get_temperature(OT07[device].I2C_address); |
cyberjoey | 24:454dc350bb17 | 914 | pc.printf("device[%02X] temperature[%.3f] status[%d]\r\n",device,T[device].tempC, T[device].status); |
cyberjoey | 17:401a5bb8d403 | 915 | } |
cyberjoey | 17:401a5bb8d403 | 916 | |
cyberjoey | 17:401a5bb8d403 | 917 | if(n == 3){ // "t 1 3" get temperature for devices 1 thru 3 |
cyberjoey | 17:401a5bb8d403 | 918 | //sprintf(str,"%x",arg1); //convert arg1 input as hex to decimal i.e. 0x10 becomes 10 dec |
cyberjoey | 17:401a5bb8d403 | 919 | //sscanf(str,"%d",&arg1); |
cyberjoey | 17:401a5bb8d403 | 920 | //IS THIS NEEDED? ^^^ |
cyberjoey | 17:401a5bb8d403 | 921 | |
cyberjoey | 17:401a5bb8d403 | 922 | for(j=device;j<=arg1;j++){ |
cyberjoey | 17:401a5bb8d403 | 923 | convert_temperature(OT07[j].I2C_address); //send convert to all devices |
cyberjoey | 30:3a1d9c2b49a5 | 924 | wait_ms(CONVERT_T_DELAY); //wait 20ms for convert temperature to complete |
cyberjoey | 17:401a5bb8d403 | 925 | T[j] = get_temperature(OT07[j].I2C_address); |
cyberjoey | 17:401a5bb8d403 | 926 | } |
cyberjoey | 17:401a5bb8d403 | 927 | |
cyberjoey | 17:401a5bb8d403 | 928 | for(j=device;j<=arg1;j++){ |
cyberjoey | 24:454dc350bb17 | 929 | pc.printf("device[%02X] temperature[%.3f] status[%d]\r\n",j,T[j].tempC, T[j].status); |
cyberjoey | 17:401a5bb8d403 | 930 | } |
cyberjoey | 17:401a5bb8d403 | 931 | } |
cyberjoey | 17:401a5bb8d403 | 932 | |
cyberjoey | 9:36ba3626aab7 | 933 | break; |
cyberjoey | 9:36ba3626aab7 | 934 | |
cyberjoey | 9:36ba3626aab7 | 935 | case 'w': |
cyberjoey | 17:401a5bb8d403 | 936 | case 'W': //write register "w device w.addr data" |
cyberjoey | 9:36ba3626aab7 | 937 | |
cyberjoey | 17:401a5bb8d403 | 938 | OT07_write_register(OT07[device].I2C_address,arg1, arg2); |
cyberjoey | 29:88f36da6a119 | 939 | pc.printf("write -- device[%02X] add[%02X] data[%02X]\r\n",device,arg1,arg2); |
cyberjoey | 9:36ba3626aab7 | 940 | break; |
cyberjoey | 9:36ba3626aab7 | 941 | |
cyberjoey | 9:36ba3626aab7 | 942 | case 'x': |
cyberjoey | 9:36ba3626aab7 | 943 | case 'X': // experimental modes |
cyberjoey | 9:36ba3626aab7 | 944 | break; |
cyberjoey | 9:36ba3626aab7 | 945 | |
cyberjoey | 9:36ba3626aab7 | 946 | |
cyberjoey | 9:36ba3626aab7 | 947 | }//end switch(c) |
cyberjoey | 9:36ba3626aab7 | 948 | }//if(n>0) |
cyberjoey | 9:36ba3626aab7 | 949 | }//end if(CR) |
cyberjoey | 9:36ba3626aab7 | 950 | if(rx_buff[rx_index] == BS){//backspace received, back up buffer pointer |
cyberjoey | 9:36ba3626aab7 | 951 | if(rx_index>0)rx_index--;//remove last char from buffer if not at start. |
cyberjoey | 9:36ba3626aab7 | 952 | }else rx_index++; |
cyberjoey | 9:36ba3626aab7 | 953 | }//end while(pc.redable()) |
cyberjoey | 30:3a1d9c2b49a5 | 954 | wait_ms(100); // slow down polling |
switches | 0:60a522ae2e35 | 955 | |
cyberjoey | 9:36ba3626aab7 | 956 | }// end (while tick == false) |
cyberjoey | 9:36ba3626aab7 | 957 | tick_flag = false; // set to false for next time |
cyberjoey | 9:36ba3626aab7 | 958 | |
cyberjoey | 9:36ba3626aab7 | 959 | // only reached when tick_flag = true otherwise stuck in pc.readable() loop |
cyberjoey | 13:674c647d12dd | 960 | if(log_flag == true){ |
cyberjoey | 9:36ba3626aab7 | 961 | if(time_count >= time_to_sample){ //take next sample |
cyberjoey | 9:36ba3626aab7 | 962 | time_to_sample += log_interval; // calculate time for next sample |
cyberjoey | 18:d913ec9dd9c2 | 963 | |
cyberjoey | 18:d913ec9dd9c2 | 964 | for(j=0;j<device_count;j++){ |
cyberjoey | 18:d913ec9dd9c2 | 965 | convert_temperature(OT07[j].I2C_address); |
cyberjoey | 30:3a1d9c2b49a5 | 966 | wait_ms(CONVERT_T_DELAY); //wait 20ms for convert temperature to complete |
cyberjoey | 18:d913ec9dd9c2 | 967 | T[j] = get_temperature(OT07[j].I2C_address); |
cyberjoey | 9:36ba3626aab7 | 968 | } |
cyberjoey | 18:d913ec9dd9c2 | 969 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
cyberjoey | 13:674c647d12dd | 970 | |
cyberjoey | 13:674c647d12dd | 971 | //open file for microSD logging |
cyberjoey | 10:148da21c297e | 972 | FILE *fp = fopen(log_file, "a"); |
cyberjoey | 9:36ba3626aab7 | 973 | if (fp != NULL) |
cyberjoey | 9:36ba3626aab7 | 974 | { |
cyberjoey | 9:36ba3626aab7 | 975 | //fprintf(fp, "\n"); |
cyberjoey | 10:148da21c297e | 976 | fprintf(fp, "%d",time_count); |
cyberjoey | 9:36ba3626aab7 | 977 | for(j=0;j<device_count;j++) |
cyberjoey | 9:36ba3626aab7 | 978 | { |
cyberjoey | 11:94a7379c0db8 | 979 | if(device_logged[j]) |
cyberjoey | 11:94a7379c0db8 | 980 | { |
cyberjoey | 25:08b220797bd9 | 981 | if(T[j].status == DEVICE_ACK) |
cyberjoey | 25:08b220797bd9 | 982 | fprintf(fp,",%.3f",T[j].tempC); |
cyberjoey | 25:08b220797bd9 | 983 | else |
cyberjoey | 25:08b220797bd9 | 984 | fprintf(fp,",---.---"); |
cyberjoey | 11:94a7379c0db8 | 985 | } |
cyberjoey | 11:94a7379c0db8 | 986 | |
cyberjoey | 9:36ba3626aab7 | 987 | } |
cyberjoey | 9:36ba3626aab7 | 988 | fprintf(fp,"\r\n"); |
cyberjoey | 9:36ba3626aab7 | 989 | |
cyberjoey | 9:36ba3626aab7 | 990 | fclose(fp); |
cyberjoey | 9:36ba3626aab7 | 991 | } |
cyberjoey | 9:36ba3626aab7 | 992 | }// end if(time_count >= time_to_sample) |
cyberjoey | 9:36ba3626aab7 | 993 | time_count ++; // |
cyberjoey | 9:36ba3626aab7 | 994 | |
cyberjoey | 9:36ba3626aab7 | 995 | }// end if(log_flag) |
cyberjoey | 9:36ba3626aab7 | 996 | |
cyberjoey | 9:36ba3626aab7 | 997 | }//end while(1) |
switches | 0:60a522ae2e35 | 998 | } |