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