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@11:94a7379c0db8, 2018-07-03 (annotated)
- Committer:
- cyberjoey
- Date:
- Tue Jul 03 15:59:51 2018 +0000
- Revision:
- 11:94a7379c0db8
- Parent:
- 10:148da21c297e
- Child:
- 12:aa9fff0aec91
fixed bugs involving infinite settings/log printing after live insertion of sd card
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 | |
| cyberjoey | 9:36ba3626aab7 | 8 | #define I2C_ADDRESS 0x30 // DS2484 I2C address |
| cyberjoey | 9:36ba3626aab7 | 9 | #define DS2484_ADD 0x30 // DS2484 I2C write address |
| cyberjoey | 9:36ba3626aab7 | 10 | |
| cyberjoey | 9:36ba3626aab7 | 11 | |
| cyberjoey | 9:36ba3626aab7 | 12 | #define PERIOD 10 // Logging period in seconds |
| cyberjoey | 9:36ba3626aab7 | 13 | |
| cyberjoey | 9:36ba3626aab7 | 14 | //DS2484 Status register bits |
| cyberjoey | 9:36ba3626aab7 | 15 | #define OWB 0x01 // status reg, One Wire Busy |
| cyberjoey | 9:36ba3626aab7 | 16 | #define PPD 0x02 // status reg, Presence Pulse Detected |
| cyberjoey | 9:36ba3626aab7 | 17 | #define SD 0x04 // status reg, Short Detected |
| cyberjoey | 9:36ba3626aab7 | 18 | #define LL 0x08 // status reg, Logic Level |
| cyberjoey | 9:36ba3626aab7 | 19 | #define RST 0x10 // status reg, Device Reset (DS2484 Reset) |
| cyberjoey | 9:36ba3626aab7 | 20 | #define SBR 0x20 // status reg, Single Bit Result |
| cyberjoey | 9:36ba3626aab7 | 21 | #define TSB 0x40 // status reg, Triplet Second Bit |
| cyberjoey | 9:36ba3626aab7 | 22 | #define DIR 0x80 // status reg, Branch Direction Taken |
| cyberjoey | 9:36ba3626aab7 | 23 | //DS2484 Command bytes |
| cyberjoey | 9:36ba3626aab7 | 24 | #define RESET 0xF0 // DS2484 Reset |
| cyberjoey | 9:36ba3626aab7 | 25 | #define SRP 0xE1 // DS2484 Set Read Pointer |
| cyberjoey | 9:36ba3626aab7 | 26 | #define WDC 0xD2 // DS2484 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 27 | #define ACP 0xC3 // DS2484 Adjust one wire Control Port |
| cyberjoey | 9:36ba3626aab7 | 28 | #define OW_RESET 0xB4 // DS2484 One Wire Reset |
| cyberjoey | 9:36ba3626aab7 | 29 | #define OW_SB 0x87 // One Wire Single Bit (write or read) |
| cyberjoey | 9:36ba3626aab7 | 30 | #define OWWB 0xA5 // One Wire Write Byte |
| cyberjoey | 9:36ba3626aab7 | 31 | #define OWRB 0x96 // One Wire Read Byte |
| cyberjoey | 9:36ba3626aab7 | 32 | #define OWT 0x78 // One Wire Triplet (read 2 bits, write 1 bit) |
| cyberjoey | 9:36ba3626aab7 | 33 | //One Wire Commands |
| cyberjoey | 9:36ba3626aab7 | 34 | #define OW_SEARCH 0xF0 // Search Rom |
| cyberjoey | 9:36ba3626aab7 | 35 | #define OW_READ 0x33 // Read Rom |
| cyberjoey | 9:36ba3626aab7 | 36 | #define OW_MATCH 0x55 // Match Rom |
| cyberjoey | 9:36ba3626aab7 | 37 | #define OW_SKIP 0xCC // Skip Rom |
| cyberjoey | 9:36ba3626aab7 | 38 | #define OW_OD_MATCH 0x69 // Match Rom |
| cyberjoey | 9:36ba3626aab7 | 39 | #define OW_OD_SKIP 0x3C // Skip Rom |
| cyberjoey | 9:36ba3626aab7 | 40 | #define OW_ALARM 0xEC // Alarm Search |
| cyberjoey | 9:36ba3626aab7 | 41 | // OT07 OW commands |
| cyberjoey | 9:36ba3626aab7 | 42 | #define OT07_OW_CONVERT 0x44 //OT07 OW convert temperature command |
| cyberjoey | 9:36ba3626aab7 | 43 | #define OT07_OW_WRITE 0xCC // OT07 OW Write Register command |
| cyberjoey | 9:36ba3626aab7 | 44 | #define OT07_OW_READ 0x33 // OT07 OW Read Register command |
| cyberjoey | 9:36ba3626aab7 | 45 | #define OT07_OW_RESET 0x82 // OT07 OW Soft Reset command |
| switches | 3:247c750f9f3d | 46 | |
| cyberjoey | 9:36ba3626aab7 | 47 | //OT07 Registers |
| cyberjoey | 9:36ba3626aab7 | 48 | #define OT07_STATUS 0x00 // OT07 status regiter |
| cyberjoey | 9:36ba3626aab7 | 49 | #define OT07_INT_EN 0x01 // OT07 Interrupt Enable |
| cyberjoey | 9:36ba3626aab7 | 50 | #define OT07_FIFO_W 0x04 // OT07 FIFO Write Pointer |
| cyberjoey | 9:36ba3626aab7 | 51 | #define OT07_FIFO_R 0x05 // OT07 FIFO Read Pointer |
| cyberjoey | 9:36ba3626aab7 | 52 | #define OT07_FIFO_OF 0x06 // OT07 FIFO Overflow Counter |
| cyberjoey | 9:36ba3626aab7 | 53 | #define OT07_FIFO_COUNT 0x07 // OT07 FIFO Data Count |
| cyberjoey | 9:36ba3626aab7 | 54 | #define OT07_FIFO_DATA 0x08 // OT07 FIFO Data |
| cyberjoey | 9:36ba3626aab7 | 55 | #define OT07_FIFO_CNFG1 0x09 // OT07 FIFO Configuration 1 (FIFO_A_FULL) |
| cyberjoey | 9:36ba3626aab7 | 56 | #define OT07_FIFO_CNFG2 0x0A // OT07 FIFO Configuration 2 |
| cyberjoey | 9:36ba3626aab7 | 57 | #define OT07_SYS 0x0C // OT07 System Configuration |
| cyberjoey | 9:36ba3626aab7 | 58 | #define OT07_ALARM_HIGH_MSB 0x10 // OT07 Alarm High MSB |
| cyberjoey | 9:36ba3626aab7 | 59 | #define OT07_ALARM_HIGH_LSB 0x11 // OT07 Alarm High LSB |
| cyberjoey | 9:36ba3626aab7 | 60 | #define OT07_ALARM_LOW_MSB 0x12 // OT07 Alarm Low MSB |
| cyberjoey | 9:36ba3626aab7 | 61 | #define OT07_ALARM_LOW_LSB 0x13 // OT07 Alarm LOW LSB |
| cyberjoey | 9:36ba3626aab7 | 62 | #define OT07_ADC_BITS 0x14 // OT07 Temp Seneor Setup (ADC_RES[7:6]) |
| cyberjoey | 9:36ba3626aab7 | 63 | #define OT07_GPIO_SETUP 0x20 // OT07 GPIO Setup, sets GPIO modes |
| cyberjoey | 9:36ba3626aab7 | 64 | #define Ot07_GPIO_CTRL 0x21 // OT07 GPIO control |
| cyberjoey | 9:36ba3626aab7 | 65 | #define OT07_ROM_ID 0x30 // OT07 ROM_ID address of LSB? |
| cyberjoey | 9:36ba3626aab7 | 66 | |
| cyberjoey | 9:36ba3626aab7 | 67 | |
| cyberjoey | 9:36ba3626aab7 | 68 | #define MAX_DEVICES 64 // Maximum number of rom devices allowed |
| cyberjoey | 9:36ba3626aab7 | 69 | #define ID_LENGTH 8 // Rom ID length in bytes |
| cyberjoey | 9:36ba3626aab7 | 70 | |
| cyberjoey | 9:36ba3626aab7 | 71 | |
| cyberjoey | 9:36ba3626aab7 | 72 | #define BS 8 // ASCII Back Space |
| cyberjoey | 9:36ba3626aab7 | 73 | #define CR 13 // ASCII Carriage Return |
| cyberjoey | 9:36ba3626aab7 | 74 | |
| cyberjoey | 10:148da21c297e | 75 | const char* settings_file = "/sd/settings.txt"; |
| cyberjoey | 10:148da21c297e | 76 | const char* log_file = "/sd/MAX30207Log.csv"; |
| cyberjoey | 10:148da21c297e | 77 | |
| cyberjoey | 9:36ba3626aab7 | 78 | //global variable |
| cyberjoey | 9:36ba3626aab7 | 79 | |
| cyberjoey | 9:36ba3626aab7 | 80 | //******************** init Feather Boared Hardware *********************** |
| cyberjoey | 9:36ba3626aab7 | 81 | |
| cyberjoey | 9:36ba3626aab7 | 82 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
| cyberjoey | 9:36ba3626aab7 | 83 | |
| cyberjoey | 9:36ba3626aab7 | 84 | //microSD logging file system setup |
| cyberjoey | 9:36ba3626aab7 | 85 | SDFileSystem sd(P0_5, P0_6, P0_4, P0_7, "sd"); // mosi, miso, sclk, cs |
| cyberjoey | 9:36ba3626aab7 | 86 | |
| cyberjoey | 9:36ba3626aab7 | 87 | //SD card insertion detection pin |
| cyberjoey | 9:36ba3626aab7 | 88 | DigitalIn SDDetect(P2_2, PullUp); |
| cyberjoey | 11:94a7379c0db8 | 89 | InterruptIn SDInsert(P2_2); |
| cyberjoey | 9:36ba3626aab7 | 90 | |
| cyberjoey | 9:36ba3626aab7 | 91 | |
| cyberjoey | 9:36ba3626aab7 | 92 | // Virtual serial port over USB |
| cyberjoey | 9:36ba3626aab7 | 93 | //USBSerial pc; |
| cyberjoey | 9:36ba3626aab7 | 94 | USBSerial pc(0x0B6A, 0x0042, 0x0001, true); |
| cyberjoey | 9:36ba3626aab7 | 95 | |
| cyberjoey | 9:36ba3626aab7 | 96 | // I2C setup |
| cyberjoey | 9:36ba3626aab7 | 97 | I2C i2c(P3_4,P3_5); // P3_4 -> I2C1_SDA, P3_5-> I2C1_SCL |
| cyberjoey | 9:36ba3626aab7 | 98 | |
| cyberjoey | 9:36ba3626aab7 | 99 | //Timer setup |
| cyberjoey | 9:36ba3626aab7 | 100 | Ticker timer_1; // timer for blinking led heartbeat |
| cyberjoey | 9:36ba3626aab7 | 101 | |
| cyberjoey | 9:36ba3626aab7 | 102 | bool tick_flag; // used for counting seconds |
| cyberjoey | 9:36ba3626aab7 | 103 | bool log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 104 | bool led_toggle_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 105 | bool button_flag = false; |
| cyberjoey | 11:94a7379c0db8 | 106 | bool sd_insert_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 107 | bool error_flag; |
| cyberjoey | 9:36ba3626aab7 | 108 | int error_ticks; |
| cyberjoey | 9:36ba3626aab7 | 109 | |
| cyberjoey | 9:36ba3626aab7 | 110 | //LED blink setup |
| switches | 1:90313362ec11 | 111 | DigitalOut rLED(LED1); |
| switches | 1:90313362ec11 | 112 | DigitalOut gLED(LED2); |
| cyberjoey | 9:36ba3626aab7 | 113 | DigitalOut bLED(LED3); |
| switches | 0:60a522ae2e35 | 114 | |
| cyberjoey | 9:36ba3626aab7 | 115 | InterruptIn button(SW1); |
| cyberjoey | 9:36ba3626aab7 | 116 | |
| cyberjoey | 9:36ba3626aab7 | 117 | |
| cyberjoey | 9:36ba3626aab7 | 118 | void LED_blink_callback(){ // LED Heart beat |
| cyberjoey | 9:36ba3626aab7 | 119 | |
| cyberjoey | 9:36ba3626aab7 | 120 | led_toggle_flag = !led_toggle_flag; |
| cyberjoey | 9:36ba3626aab7 | 121 | |
| cyberjoey | 9:36ba3626aab7 | 122 | if(log_flag) //if logging |
| cyberjoey | 9:36ba3626aab7 | 123 | { |
| cyberjoey | 9:36ba3626aab7 | 124 | |
| cyberjoey | 9:36ba3626aab7 | 125 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 126 | { |
| cyberjoey | 9:36ba3626aab7 | 127 | //toggle red led |
| cyberjoey | 9:36ba3626aab7 | 128 | rLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 129 | } |
| cyberjoey | 9:36ba3626aab7 | 130 | else |
| cyberjoey | 9:36ba3626aab7 | 131 | { |
| cyberjoey | 9:36ba3626aab7 | 132 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 133 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 134 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 135 | } |
| cyberjoey | 9:36ba3626aab7 | 136 | } |
| cyberjoey | 9:36ba3626aab7 | 137 | else if(error_flag) //if error (no sd card) |
| cyberjoey | 9:36ba3626aab7 | 138 | { |
| cyberjoey | 9:36ba3626aab7 | 139 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 140 | { |
| cyberjoey | 9:36ba3626aab7 | 141 | //toggle red led |
| cyberjoey | 9:36ba3626aab7 | 142 | rLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 143 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 144 | } |
| cyberjoey | 9:36ba3626aab7 | 145 | else |
| cyberjoey | 9:36ba3626aab7 | 146 | { |
| cyberjoey | 9:36ba3626aab7 | 147 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 148 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 149 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 150 | } |
| cyberjoey | 9:36ba3626aab7 | 151 | error_ticks--; |
| cyberjoey | 9:36ba3626aab7 | 152 | if(error_ticks <= 0) |
| cyberjoey | 9:36ba3626aab7 | 153 | error_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 154 | } |
| cyberjoey | 9:36ba3626aab7 | 155 | else |
| cyberjoey | 9:36ba3626aab7 | 156 | { |
| cyberjoey | 9:36ba3626aab7 | 157 | if(led_toggle_flag) |
| cyberjoey | 9:36ba3626aab7 | 158 | { |
| cyberjoey | 9:36ba3626aab7 | 159 | //toggle teal leds |
| cyberjoey | 9:36ba3626aab7 | 160 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 161 | bLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 162 | } |
| cyberjoey | 9:36ba3626aab7 | 163 | else |
| cyberjoey | 9:36ba3626aab7 | 164 | { |
| cyberjoey | 9:36ba3626aab7 | 165 | rLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 166 | gLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 167 | bLED = LED_OFF; |
| cyberjoey | 9:36ba3626aab7 | 168 | } |
| cyberjoey | 9:36ba3626aab7 | 169 | } |
| cyberjoey | 9:36ba3626aab7 | 170 | |
| cyberjoey | 9:36ba3626aab7 | 171 | tick_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 172 | } |
| cyberjoey | 9:36ba3626aab7 | 173 | |
| cyberjoey | 9:36ba3626aab7 | 174 | void btn_pressed() //button pressed isr |
| cyberjoey | 9:36ba3626aab7 | 175 | { |
| cyberjoey | 9:36ba3626aab7 | 176 | button_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 177 | } |
| cyberjoey | 9:36ba3626aab7 | 178 | |
| cyberjoey | 11:94a7379c0db8 | 179 | void sd_insert() //sd_insert pressed isr |
| cyberjoey | 11:94a7379c0db8 | 180 | { |
| cyberjoey | 11:94a7379c0db8 | 181 | sd_insert_flag = true; |
| cyberjoey | 11:94a7379c0db8 | 182 | } |
| cyberjoey | 11:94a7379c0db8 | 183 | |
| cyberjoey | 9:36ba3626aab7 | 184 | /* |
| cyberjoey | 9:36ba3626aab7 | 185 | void take_data_flag_callback(){ // set data sample time flag |
| cyberjoey | 9:36ba3626aab7 | 186 | |
| cyberjoey | 9:36ba3626aab7 | 187 | take_data_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 188 | } |
| cyberjoey | 9:36ba3626aab7 | 189 | */ |
| cyberjoey | 9:36ba3626aab7 | 190 | |
| cyberjoey | 9:36ba3626aab7 | 191 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 192 | // Define CRC-8 Table |
| cyberjoey | 9:36ba3626aab7 | 193 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 194 | |
| cyberjoey | 9:36ba3626aab7 | 195 | static unsigned char crc_table[] = { |
| cyberjoey | 9:36ba3626aab7 | 196 | 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, |
| cyberjoey | 9:36ba3626aab7 | 197 | 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, |
| cyberjoey | 9:36ba3626aab7 | 198 | 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, |
| cyberjoey | 9:36ba3626aab7 | 199 | 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, |
| cyberjoey | 9:36ba3626aab7 | 200 | 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, |
| cyberjoey | 9:36ba3626aab7 | 201 | 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, |
| cyberjoey | 9:36ba3626aab7 | 202 | 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, |
| cyberjoey | 9:36ba3626aab7 | 203 | 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, |
| cyberjoey | 9:36ba3626aab7 | 204 | 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, |
| cyberjoey | 9:36ba3626aab7 | 205 | 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, |
| cyberjoey | 9:36ba3626aab7 | 206 | 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, |
| cyberjoey | 9:36ba3626aab7 | 207 | 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, |
| cyberjoey | 9:36ba3626aab7 | 208 | 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, |
| cyberjoey | 9:36ba3626aab7 | 209 | 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, |
| cyberjoey | 9:36ba3626aab7 | 210 | 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, |
| cyberjoey | 9:36ba3626aab7 | 211 | 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; |
| cyberjoey | 9:36ba3626aab7 | 212 | |
| cyberjoey | 9:36ba3626aab7 | 213 | |
| cyberjoey | 9:36ba3626aab7 | 214 | |
| cyberjoey | 9:36ba3626aab7 | 215 | // ----------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 216 | // Calculate the CRC8 of the byte value provided with the current total |
| cyberjoey | 9:36ba3626aab7 | 217 | // calc_crc8(unsigned char, unsigned char) crc8 total, byte to add |
| cyberjoey | 9:36ba3626aab7 | 218 | // pass crc8 total = 0 to start new crc sum. |
| cyberjoey | 9:36ba3626aab7 | 219 | |
| cyberjoey | 9:36ba3626aab7 | 220 | // Returns new crc8 total |
| cyberjoey | 9:36ba3626aab7 | 221 | // ----------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 222 | |
| cyberjoey | 9:36ba3626aab7 | 223 | unsigned char calc_crc8(unsigned char crc8, unsigned char value){ |
| cyberjoey | 9:36ba3626aab7 | 224 | |
| cyberjoey | 9:36ba3626aab7 | 225 | return crc_table[crc8 ^ value]; |
| cyberjoey | 9:36ba3626aab7 | 226 | } |
| cyberjoey | 9:36ba3626aab7 | 227 | |
| cyberjoey | 9:36ba3626aab7 | 228 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 229 | // OW_reset() |
| cyberjoey | 9:36ba3626aab7 | 230 | // returns 0 no device present, 1 device present |
| cyberjoey | 9:36ba3626aab7 | 231 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 232 | |
| cyberjoey | 9:36ba3626aab7 | 233 | int OW_reset(){ |
| cyberjoey | 9:36ba3626aab7 | 234 | char data[2]; // define as minimun array size so 'data' is a pointer |
| cyberjoey | 9:36ba3626aab7 | 235 | data[0] = OW_RESET; // One Wire Reset 0xB4 |
| cyberjoey | 9:36ba3626aab7 | 236 | i2c.write(DS2484_ADD,data,1,0); // write reset command to DS2484 |
| cyberjoey | 9:36ba3626aab7 | 237 | wait_ms(1); |
| cyberjoey | 9:36ba3626aab7 | 238 | do{ // poll OW-busy |
| cyberjoey | 9:36ba3626aab7 | 239 | i2c.read(DS2484_ADD,data,1); // read back status byte from DS2484 |
| cyberjoey | 9:36ba3626aab7 | 240 | }while(OWB & data[0]); |
| cyberjoey | 9:36ba3626aab7 | 241 | if(PPD & data[0])return 1; // Presence Pulse Detected bit true? |
| cyberjoey | 9:36ba3626aab7 | 242 | return 0; // else |
| cyberjoey | 9:36ba3626aab7 | 243 | } // end OW_reset() |
| cyberjoey | 9:36ba3626aab7 | 244 | |
| cyberjoey | 9:36ba3626aab7 | 245 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 246 | // OW_write_byte(char) takes char to send over one wire |
| cyberjoey | 9:36ba3626aab7 | 247 | // returns DS2484 status byte |
| cyberjoey | 9:36ba3626aab7 | 248 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 249 | |
| cyberjoey | 9:36ba3626aab7 | 250 | char OW_write_byte(char byte){ |
| cyberjoey | 9:36ba3626aab7 | 251 | |
| cyberjoey | 9:36ba3626aab7 | 252 | char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 253 | data[0] = 0xA5; |
| cyberjoey | 9:36ba3626aab7 | 254 | data[1] = byte; |
| cyberjoey | 9:36ba3626aab7 | 255 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 256 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 257 | |
| cyberjoey | 9:36ba3626aab7 | 258 | // db.printf("[%02X]",byte); |
| cyberjoey | 9:36ba3626aab7 | 259 | wait_us(700); // assumes normal speed OW write |
| cyberjoey | 9:36ba3626aab7 | 260 | return data[0]; // return DS2484 Status byte |
| cyberjoey | 9:36ba3626aab7 | 261 | |
| cyberjoey | 9:36ba3626aab7 | 262 | }// end OW_write_byte() |
| cyberjoey | 9:36ba3626aab7 | 263 | |
| cyberjoey | 9:36ba3626aab7 | 264 | |
| cyberjoey | 9:36ba3626aab7 | 265 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 266 | // OW_read_byte(char *, int) char* (save data pointer) |
| cyberjoey | 9:36ba3626aab7 | 267 | // int (number of bytes to read) |
| cyberjoey | 9:36ba3626aab7 | 268 | // returns 0x00 |
| cyberjoey | 9:36ba3626aab7 | 269 | // |
| cyberjoey | 9:36ba3626aab7 | 270 | // assumes OW_reset, OW_match rom, and OW device register read address already sent. |
| cyberjoey | 9:36ba3626aab7 | 271 | // |
| cyberjoey | 9:36ba3626aab7 | 272 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 273 | |
| cyberjoey | 9:36ba3626aab7 | 274 | char OW_read_byte(char* a, int n){ |
| cyberjoey | 9:36ba3626aab7 | 275 | |
| cyberjoey | 9:36ba3626aab7 | 276 | char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 277 | int i; |
| cyberjoey | 9:36ba3626aab7 | 278 | |
| cyberjoey | 9:36ba3626aab7 | 279 | //read n bytes of data |
| cyberjoey | 9:36ba3626aab7 | 280 | i=0; |
| cyberjoey | 9:36ba3626aab7 | 281 | do{ |
| cyberjoey | 9:36ba3626aab7 | 282 | data[0] = OWRB; // send 0x96; DS2484 command, OW read byte |
| cyberjoey | 9:36ba3626aab7 | 283 | i2c.write(DS2484_ADD,data,1,0); |
| cyberjoey | 9:36ba3626aab7 | 284 | wait_us(600); |
| cyberjoey | 9:36ba3626aab7 | 285 | do{ |
| cyberjoey | 9:36ba3626aab7 | 286 | i2c.read(DS2484_ADD,data,1); // read status byte and test OW-busy? |
| cyberjoey | 9:36ba3626aab7 | 287 | }while(data[0]&0x01 == 1); // status bit[0] set --> OW busy |
| cyberjoey | 9:36ba3626aab7 | 288 | |
| cyberjoey | 9:36ba3626aab7 | 289 | //get byte from DS2484 |
| cyberjoey | 9:36ba3626aab7 | 290 | data[0] = SRP; // send 0xE1, set read pointer |
| cyberjoey | 9:36ba3626aab7 | 291 | data[1] = 0xE1; // set read pointer address 0xE1 read OW data |
| cyberjoey | 9:36ba3626aab7 | 292 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 293 | i2c.read(DS2484_ADD,data,1); // read bytes from OW register |
| cyberjoey | 9:36ba3626aab7 | 294 | |
| cyberjoey | 9:36ba3626aab7 | 295 | a[i] = data[0]; |
| cyberjoey | 9:36ba3626aab7 | 296 | |
| cyberjoey | 9:36ba3626aab7 | 297 | i++; |
| cyberjoey | 9:36ba3626aab7 | 298 | //pc.printf("[%02X] ",data[0]); |
| cyberjoey | 9:36ba3626aab7 | 299 | }while(i<n); |
| cyberjoey | 9:36ba3626aab7 | 300 | //for(i=0;i<n;i++) db.printf("OW_read_byte - [%2d][%02X]\r\n",i,a[i]); |
| cyberjoey | 9:36ba3626aab7 | 301 | return 0x00; |
| cyberjoey | 9:36ba3626aab7 | 302 | }// end OW_read_byte() |
| cyberjoey | 9:36ba3626aab7 | 303 | |
| cyberjoey | 9:36ba3626aab7 | 304 | |
| cyberjoey | 9:36ba3626aab7 | 305 | |
| cyberjoey | 9:36ba3626aab7 | 306 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 307 | // OW_match_rom(char *) pointer to device_id byte array |
| cyberjoey | 9:36ba3626aab7 | 308 | // returns 0x00 |
| cyberjoey | 9:36ba3626aab7 | 309 | // assumes OW_reset alread sent |
| cyberjoey | 9:36ba3626aab7 | 310 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 311 | |
| cyberjoey | 9:36ba3626aab7 | 312 | char OW_match_rom(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 313 | |
| cyberjoey | 9:36ba3626aab7 | 314 | int i; |
| cyberjoey | 9:36ba3626aab7 | 315 | //Match rom |
| cyberjoey | 9:36ba3626aab7 | 316 | OW_write_byte(OW_MATCH); // send 0x55, match rom command |
| cyberjoey | 9:36ba3626aab7 | 317 | //send rom code for device |
| cyberjoey | 9:36ba3626aab7 | 318 | for(i=0;i<8;i++){ |
| cyberjoey | 9:36ba3626aab7 | 319 | OW_write_byte(device_id[i]); //I2C write ow byte |
| cyberjoey | 9:36ba3626aab7 | 320 | } |
| cyberjoey | 9:36ba3626aab7 | 321 | return 0x00; |
| cyberjoey | 9:36ba3626aab7 | 322 | |
| cyberjoey | 9:36ba3626aab7 | 323 | }// end OW_macth_rom |
| cyberjoey | 9:36ba3626aab7 | 324 | |
| cyberjoey | 9:36ba3626aab7 | 325 | |
| cyberjoey | 9:36ba3626aab7 | 326 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 327 | // search_rom(char *) pointer to start of 2D array rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 328 | // returns number of devices found |
| cyberjoey | 9:36ba3626aab7 | 329 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 330 | |
| cyberjoey | 9:36ba3626aab7 | 331 | |
| cyberjoey | 9:36ba3626aab7 | 332 | int search_rom(char rom_id_list[MAX_DEVICES][ID_LENGTH]){ // searches for all device on OW bus, returns # found |
| cyberjoey | 9:36ba3626aab7 | 333 | |
| cyberjoey | 9:36ba3626aab7 | 334 | int bit_num; |
| cyberjoey | 9:36ba3626aab7 | 335 | int byte_num; |
| cyberjoey | 9:36ba3626aab7 | 336 | int last_zero; |
| cyberjoey | 9:36ba3626aab7 | 337 | int last_discrep; |
| cyberjoey | 9:36ba3626aab7 | 338 | int search_dir; |
| cyberjoey | 9:36ba3626aab7 | 339 | int rom_count; |
| cyberjoey | 9:36ba3626aab7 | 340 | char byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 341 | int last_device; |
| cyberjoey | 9:36ba3626aab7 | 342 | int i; |
| cyberjoey | 9:36ba3626aab7 | 343 | |
| cyberjoey | 9:36ba3626aab7 | 344 | char rom_id[8]; //used in rom search |
| cyberjoey | 9:36ba3626aab7 | 345 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 346 | char crc; |
| cyberjoey | 9:36ba3626aab7 | 347 | |
| cyberjoey | 9:36ba3626aab7 | 348 | //init for first search only |
| cyberjoey | 9:36ba3626aab7 | 349 | last_device = 0; |
| cyberjoey | 9:36ba3626aab7 | 350 | last_discrep = 0; |
| cyberjoey | 9:36ba3626aab7 | 351 | rom_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 352 | |
| cyberjoey | 9:36ba3626aab7 | 353 | for(i=0;i<8;i++)rom_id[i] = 0x00; //clear rom_id |
| cyberjoey | 9:36ba3626aab7 | 354 | |
| cyberjoey | 9:36ba3626aab7 | 355 | do{ // loop for each rom search (end when last_device = 1) |
| cyberjoey | 9:36ba3626aab7 | 356 | //db.printf("\r\nsearch next rom [%2d]",rom_count); |
| cyberjoey | 9:36ba3626aab7 | 357 | |
| cyberjoey | 9:36ba3626aab7 | 358 | //init variable for each search |
| cyberjoey | 9:36ba3626aab7 | 359 | bit_num = 1; |
| cyberjoey | 9:36ba3626aab7 | 360 | byte_num = 0; |
| cyberjoey | 9:36ba3626aab7 | 361 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 362 | last_zero = 0; |
| cyberjoey | 9:36ba3626aab7 | 363 | |
| cyberjoey | 9:36ba3626aab7 | 364 | |
| cyberjoey | 9:36ba3626aab7 | 365 | //OW reset |
| cyberjoey | 9:36ba3626aab7 | 366 | if(OW_reset()){ //returns 1 if at least one device present |
| cyberjoey | 9:36ba3626aab7 | 367 | //test if last device found |
| cyberjoey | 9:36ba3626aab7 | 368 | if(last_device!=1){ // more devices to find |
| cyberjoey | 9:36ba3626aab7 | 369 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 370 | OW_write_byte(OW_SEARCH); //send 0xF0 over one wire to init search rom |
| cyberjoey | 9:36ba3626aab7 | 371 | |
| cyberjoey | 9:36ba3626aab7 | 372 | do{ |
| cyberjoey | 9:36ba3626aab7 | 373 | //determine search direction |
| cyberjoey | 9:36ba3626aab7 | 374 | if(bit_num < last_discrep){ //before last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 375 | if((rom_id[byte_num] & byte_mask) > 0){// use last rom path |
| cyberjoey | 9:36ba3626aab7 | 376 | search_dir = 1; // last path was 1 |
| cyberjoey | 9:36ba3626aab7 | 377 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 378 | search_dir = 0; // last path was 0 |
| cyberjoey | 9:36ba3626aab7 | 379 | } |
| cyberjoey | 9:36ba3626aab7 | 380 | }else{ // at or past last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 381 | if(bit_num == last_discrep){ // at last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 382 | search_dir = 1; //been here before so use 1 |
| cyberjoey | 9:36ba3626aab7 | 383 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 384 | search_dir = 0; //past last discrepancy so use 0 |
| cyberjoey | 9:36ba3626aab7 | 385 | } |
| cyberjoey | 9:36ba3626aab7 | 386 | } |
| cyberjoey | 9:36ba3626aab7 | 387 | |
| cyberjoey | 9:36ba3626aab7 | 388 | //write direction bit and get reply |
| cyberjoey | 9:36ba3626aab7 | 389 | data[0] = 0x78; |
| cyberjoey | 9:36ba3626aab7 | 390 | data[1] = search_dir << 7; //sets bit 7 of status reg to search_dir |
| cyberjoey | 9:36ba3626aab7 | 391 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 392 | |
| cyberjoey | 9:36ba3626aab7 | 393 | wait_us(250); |
| cyberjoey | 9:36ba3626aab7 | 394 | |
| cyberjoey | 9:36ba3626aab7 | 395 | //read in rom_id bits |
| cyberjoey | 9:36ba3626aab7 | 396 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 397 | |
| cyberjoey | 9:36ba3626aab7 | 398 | //(todo) check for no device error here |
| cyberjoey | 9:36ba3626aab7 | 399 | |
| cyberjoey | 9:36ba3626aab7 | 400 | //get search direction used by triplet command |
| cyberjoey | 9:36ba3626aab7 | 401 | |
| cyberjoey | 9:36ba3626aab7 | 402 | if(data[0]&0x80){ //true --> DIR bit = 1 |
| cyberjoey | 9:36ba3626aab7 | 403 | search_dir = 1; |
| cyberjoey | 9:36ba3626aab7 | 404 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 405 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 406 | } |
| cyberjoey | 9:36ba3626aab7 | 407 | |
| cyberjoey | 9:36ba3626aab7 | 408 | //test for discrepancy (both 0 and 1 present at current bit number)(TSB = 0 (1's present) and SBR = 0 (0's present)) |
| cyberjoey | 9:36ba3626aab7 | 409 | if(!(data[0] & 0x60)){ // true --> discrepancy exist, both TSB and SBR = 0 |
| cyberjoey | 9:36ba3626aab7 | 410 | if(search_dir == 0)last_zero = bit_num; |
| cyberjoey | 9:36ba3626aab7 | 411 | } |
| cyberjoey | 9:36ba3626aab7 | 412 | if(search_dir == 1){ // write search dir to rom bit |
| cyberjoey | 9:36ba3626aab7 | 413 | rom_id[byte_num] |= byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 414 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 415 | rom_id[byte_num] &= ~byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 416 | } |
| cyberjoey | 9:36ba3626aab7 | 417 | |
| cyberjoey | 9:36ba3626aab7 | 418 | //increment bit_num and byte_num if needed |
| cyberjoey | 9:36ba3626aab7 | 419 | bit_num++; |
| cyberjoey | 9:36ba3626aab7 | 420 | byte_mask <<= 1; //rotate 1 bit left |
| cyberjoey | 9:36ba3626aab7 | 421 | if(byte_mask == 0){ //if bit shifts out of byte set byte to 1 |
| cyberjoey | 9:36ba3626aab7 | 422 | byte_num++; //also increnent byt num |
| cyberjoey | 9:36ba3626aab7 | 423 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 424 | } |
| cyberjoey | 9:36ba3626aab7 | 425 | // pc.printf("sdx[%d] lz[%2d] byte[%d] mask[%02X] bn[%d] ROM[%02X %02X %02X]\n\r",search_dir,last_zero,byte_num,byte_mask, byte_num, rom_id[2], rom_id[1], rom_id[0]); |
| cyberjoey | 9:36ba3626aab7 | 426 | |
| cyberjoey | 9:36ba3626aab7 | 427 | |
| cyberjoey | 9:36ba3626aab7 | 428 | }while(bit_num<65); //bit nun started at 1 so end after 64 |
| cyberjoey | 9:36ba3626aab7 | 429 | last_discrep = last_zero; |
| cyberjoey | 9:36ba3626aab7 | 430 | if(last_discrep == 0)last_device = 1; |
| cyberjoey | 9:36ba3626aab7 | 431 | //copy rom_id into rom_id_list and calc crc of first 7 bytes |
| cyberjoey | 9:36ba3626aab7 | 432 | crc = 0x00; // reset crc8 total to 0 |
| cyberjoey | 9:36ba3626aab7 | 433 | //db.printf(" -- rom id[ "); |
| cyberjoey | 9:36ba3626aab7 | 434 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 435 | rom_id_list[rom_count][i] = rom_id[i] ; |
| cyberjoey | 9:36ba3626aab7 | 436 | //db.printf("%02X ",rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 437 | } |
| cyberjoey | 9:36ba3626aab7 | 438 | //db.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 439 | //db.printf("last_device[%2d]\r\n",last_device); |
| cyberjoey | 9:36ba3626aab7 | 440 | //db.printf("last_zero[%2d]\r\n",last_zero); |
| cyberjoey | 9:36ba3626aab7 | 441 | //db.printf("last_discrep[%2d]\r\n",last_discrep); |
| cyberjoey | 9:36ba3626aab7 | 442 | |
| cyberjoey | 9:36ba3626aab7 | 443 | //clac_crc of rom ID |
| cyberjoey | 9:36ba3626aab7 | 444 | for (i=0;i<7;i++){ |
| cyberjoey | 9:36ba3626aab7 | 445 | crc = calc_crc8(crc, rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 446 | } |
| cyberjoey | 9:36ba3626aab7 | 447 | //db.printf("crc8 = [%02X]\r\n",crc); |
| cyberjoey | 9:36ba3626aab7 | 448 | |
| cyberjoey | 9:36ba3626aab7 | 449 | |
| cyberjoey | 9:36ba3626aab7 | 450 | rom_count++; |
| cyberjoey | 9:36ba3626aab7 | 451 | }//if(last_device..) |
| cyberjoey | 9:36ba3626aab7 | 452 | }else{//if prescent -- if no device present rom count is 0 |
| cyberjoey | 9:36ba3626aab7 | 453 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 454 | } |
| cyberjoey | 9:36ba3626aab7 | 455 | }while(last_device == 0); |
| cyberjoey | 9:36ba3626aab7 | 456 | return rom_count; |
| cyberjoey | 9:36ba3626aab7 | 457 | }// end search_rom() |
| cyberjoey | 9:36ba3626aab7 | 458 | |
| cyberjoey | 9:36ba3626aab7 | 459 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 460 | // alarm_search(char *) pointer to start of 2D array rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 461 | // returns number of devices found with alarm triggered |
| cyberjoey | 9:36ba3626aab7 | 462 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 463 | |
| cyberjoey | 9:36ba3626aab7 | 464 | int alarm_search(char rom_id_list[MAX_DEVICES][ID_LENGTH]){ // searches for all devices with alarm triggered on OW bus, returns # found |
| switches | 0:60a522ae2e35 | 465 | |
| cyberjoey | 9:36ba3626aab7 | 466 | int bit_num; |
| cyberjoey | 9:36ba3626aab7 | 467 | int byte_num; |
| cyberjoey | 9:36ba3626aab7 | 468 | int last_zero; |
| cyberjoey | 9:36ba3626aab7 | 469 | int last_discrep; |
| cyberjoey | 9:36ba3626aab7 | 470 | int search_dir; |
| cyberjoey | 9:36ba3626aab7 | 471 | int rom_count; |
| cyberjoey | 9:36ba3626aab7 | 472 | char byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 473 | int last_device; |
| cyberjoey | 9:36ba3626aab7 | 474 | int i; |
| cyberjoey | 9:36ba3626aab7 | 475 | |
| cyberjoey | 9:36ba3626aab7 | 476 | char rom_id[8]; //used in rom search |
| cyberjoey | 9:36ba3626aab7 | 477 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 478 | char crc; |
| cyberjoey | 9:36ba3626aab7 | 479 | |
| cyberjoey | 9:36ba3626aab7 | 480 | //init for first search only |
| cyberjoey | 9:36ba3626aab7 | 481 | last_device = 0; |
| cyberjoey | 9:36ba3626aab7 | 482 | last_discrep = 0; |
| cyberjoey | 9:36ba3626aab7 | 483 | rom_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 484 | |
| cyberjoey | 9:36ba3626aab7 | 485 | for(i=0;i<8;i++)rom_id[i] = 0x00; //clear rom_id |
| cyberjoey | 9:36ba3626aab7 | 486 | |
| cyberjoey | 9:36ba3626aab7 | 487 | do{ // loop for each rom search (end when last_device = 1) |
| cyberjoey | 9:36ba3626aab7 | 488 | |
| cyberjoey | 9:36ba3626aab7 | 489 | //init variable for each search |
| cyberjoey | 9:36ba3626aab7 | 490 | bit_num = 1; |
| cyberjoey | 9:36ba3626aab7 | 491 | byte_num = 0; |
| cyberjoey | 9:36ba3626aab7 | 492 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 493 | last_zero = 0; |
| cyberjoey | 9:36ba3626aab7 | 494 | |
| cyberjoey | 9:36ba3626aab7 | 495 | |
| cyberjoey | 9:36ba3626aab7 | 496 | //OW reset |
| cyberjoey | 9:36ba3626aab7 | 497 | if(OW_reset()){ //returns 1 if at least one device present |
| cyberjoey | 9:36ba3626aab7 | 498 | //test if last device found |
| cyberjoey | 9:36ba3626aab7 | 499 | if(last_device!=1){ // more devices to find |
| cyberjoey | 9:36ba3626aab7 | 500 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 501 | OW_write_byte(OW_ALARM); //send 0xEC over one wire to init alarm search |
| cyberjoey | 9:36ba3626aab7 | 502 | |
| cyberjoey | 9:36ba3626aab7 | 503 | do{ |
| cyberjoey | 9:36ba3626aab7 | 504 | //determine search direction |
| cyberjoey | 9:36ba3626aab7 | 505 | if(bit_num < last_discrep){ //before last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 506 | if((rom_id[byte_num] & byte_mask) > 0){// use last rom path |
| cyberjoey | 9:36ba3626aab7 | 507 | search_dir = 1; // last path was 1 |
| cyberjoey | 9:36ba3626aab7 | 508 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 509 | search_dir = 0; // last path was 0 |
| cyberjoey | 9:36ba3626aab7 | 510 | } |
| cyberjoey | 9:36ba3626aab7 | 511 | }else{ // at or past last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 512 | if(bit_num == last_discrep){ // at last discrepancy |
| cyberjoey | 9:36ba3626aab7 | 513 | search_dir = 1; //been here before so use 1 |
| cyberjoey | 9:36ba3626aab7 | 514 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 515 | search_dir = 0; //past last discrepancy so use 0 |
| cyberjoey | 9:36ba3626aab7 | 516 | } |
| cyberjoey | 9:36ba3626aab7 | 517 | } |
| cyberjoey | 9:36ba3626aab7 | 518 | |
| cyberjoey | 9:36ba3626aab7 | 519 | //write direction bit and get reply |
| cyberjoey | 9:36ba3626aab7 | 520 | data[0] = 0x78; |
| cyberjoey | 9:36ba3626aab7 | 521 | data[1] = search_dir << 7; //sets bit 7 of status reg to search_dir |
| cyberjoey | 9:36ba3626aab7 | 522 | i2c.write(DS2484_ADD,data,2,1); |
| cyberjoey | 9:36ba3626aab7 | 523 | |
| cyberjoey | 9:36ba3626aab7 | 524 | wait_us(250); |
| cyberjoey | 9:36ba3626aab7 | 525 | |
| cyberjoey | 9:36ba3626aab7 | 526 | //read in rom_id bits |
| cyberjoey | 9:36ba3626aab7 | 527 | i2c.read(DS2484_ADD,data,1); // read status byte |
| cyberjoey | 9:36ba3626aab7 | 528 | |
| cyberjoey | 9:36ba3626aab7 | 529 | //(todo) check for no device error here |
| cyberjoey | 9:36ba3626aab7 | 530 | |
| cyberjoey | 9:36ba3626aab7 | 531 | //get search direction used by triplet command |
| cyberjoey | 9:36ba3626aab7 | 532 | |
| cyberjoey | 9:36ba3626aab7 | 533 | if(data[0]&0x80){ //true --> DIR bit = 1 |
| cyberjoey | 9:36ba3626aab7 | 534 | search_dir = 1; |
| cyberjoey | 9:36ba3626aab7 | 535 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 536 | search_dir = 0; |
| cyberjoey | 9:36ba3626aab7 | 537 | } |
| cyberjoey | 9:36ba3626aab7 | 538 | |
| cyberjoey | 9:36ba3626aab7 | 539 | //test for discrepancy (both 0 and 1 present at current bit number)(TSB = 0 (1's present) and SBR = 0 (0's present)) |
| cyberjoey | 9:36ba3626aab7 | 540 | if(!(data[0] & 0x60)){ // true --> discrepancy exist, both TSB and SBR = 0 |
| cyberjoey | 9:36ba3626aab7 | 541 | if(search_dir == 0)last_zero = bit_num; |
| cyberjoey | 9:36ba3626aab7 | 542 | } |
| cyberjoey | 9:36ba3626aab7 | 543 | if(search_dir == 1){ // write search dir to rom bit |
| cyberjoey | 9:36ba3626aab7 | 544 | rom_id[byte_num] |= byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 545 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 546 | rom_id[byte_num] &= ~byte_mask; |
| cyberjoey | 9:36ba3626aab7 | 547 | } |
| cyberjoey | 9:36ba3626aab7 | 548 | |
| cyberjoey | 9:36ba3626aab7 | 549 | //increment bit_num and byte_num if needed |
| cyberjoey | 9:36ba3626aab7 | 550 | bit_num++; |
| cyberjoey | 9:36ba3626aab7 | 551 | byte_mask <<= 1; //rotate 1 bit left |
| cyberjoey | 9:36ba3626aab7 | 552 | if(byte_mask == 0){ //if bit shifts out of byte set byte to 1 |
| cyberjoey | 9:36ba3626aab7 | 553 | byte_num++; //also increnent byt num |
| cyberjoey | 9:36ba3626aab7 | 554 | byte_mask = 1; |
| cyberjoey | 9:36ba3626aab7 | 555 | } |
| cyberjoey | 9:36ba3626aab7 | 556 | |
| cyberjoey | 9:36ba3626aab7 | 557 | |
| cyberjoey | 9:36ba3626aab7 | 558 | }while(bit_num<65); //bit nun started at 1 so end after 64 |
| cyberjoey | 9:36ba3626aab7 | 559 | last_discrep = last_zero; |
| cyberjoey | 9:36ba3626aab7 | 560 | if(last_discrep == 0)last_device = 1; |
| cyberjoey | 9:36ba3626aab7 | 561 | //copy rom_id into rom_id_list and calc crc of first 7 bytes |
| cyberjoey | 9:36ba3626aab7 | 562 | crc = 0x00; // reset crc8 total to 0 |
| cyberjoey | 9:36ba3626aab7 | 563 | //db.printf(" -- rom id[ "); |
| cyberjoey | 9:36ba3626aab7 | 564 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 565 | rom_id_list[rom_count][i] = rom_id[i]; |
| cyberjoey | 9:36ba3626aab7 | 566 | } |
| cyberjoey | 9:36ba3626aab7 | 567 | |
| cyberjoey | 9:36ba3626aab7 | 568 | //clac_crc of rom ID |
| cyberjoey | 9:36ba3626aab7 | 569 | for (i=0;i<7;i++){ |
| cyberjoey | 9:36ba3626aab7 | 570 | crc = calc_crc8(crc, rom_id[i]); |
| cyberjoey | 9:36ba3626aab7 | 571 | } |
| cyberjoey | 9:36ba3626aab7 | 572 | |
| cyberjoey | 9:36ba3626aab7 | 573 | rom_count++; |
| cyberjoey | 9:36ba3626aab7 | 574 | }//if(last_device..) |
| cyberjoey | 9:36ba3626aab7 | 575 | }else{//if prescent -- if no device present rom count is 0 |
| cyberjoey | 9:36ba3626aab7 | 576 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 577 | } |
| cyberjoey | 9:36ba3626aab7 | 578 | }while(last_device == 0); |
| cyberjoey | 9:36ba3626aab7 | 579 | |
| cyberjoey | 9:36ba3626aab7 | 580 | //check if rom_id from alarm search is all 1s (this means there was a presence pulse but no alarm is triggered) |
| cyberjoey | 9:36ba3626aab7 | 581 | bool bad_rom_id = true; |
| cyberjoey | 9:36ba3626aab7 | 582 | for(int i = 0; i < 7; i++) |
| cyberjoey | 9:36ba3626aab7 | 583 | { |
| cyberjoey | 9:36ba3626aab7 | 584 | if(rom_id_list[0][i] != 0xFF) |
| cyberjoey | 9:36ba3626aab7 | 585 | bad_rom_id = false; //found a byte in the rom id that isn't all 1s |
| cyberjoey | 9:36ba3626aab7 | 586 | } |
| cyberjoey | 9:36ba3626aab7 | 587 | if(bad_rom_id) |
| cyberjoey | 9:36ba3626aab7 | 588 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 589 | |
| cyberjoey | 9:36ba3626aab7 | 590 | return rom_count; |
| cyberjoey | 9:36ba3626aab7 | 591 | }// end alarm_search() |
| cyberjoey | 9:36ba3626aab7 | 592 | |
| cyberjoey | 9:36ba3626aab7 | 593 | |
| cyberjoey | 9:36ba3626aab7 | 594 | // ******************** write_OW_register() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 595 | // write_OW_register(char*, int, int, char*) |
| cyberjoey | 9:36ba3626aab7 | 596 | // char* pointer to rom_id |
| cyberjoey | 9:36ba3626aab7 | 597 | // int start address |
| cyberjoey | 9:36ba3626aab7 | 598 | // int number of bytes to write |
| cyberjoey | 9:36ba3626aab7 | 599 | // char* pointer to write data location |
| cyberjoey | 9:36ba3626aab7 | 600 | // returns 0 if CRC match data |
| cyberjoey | 9:36ba3626aab7 | 601 | // returns -1 if CRC does not match data |
| cyberjoey | 9:36ba3626aab7 | 602 | // |
| cyberjoey | 9:36ba3626aab7 | 603 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 604 | |
| cyberjoey | 9:36ba3626aab7 | 605 | |
| cyberjoey | 9:36ba3626aab7 | 606 | |
| cyberjoey | 9:36ba3626aab7 | 607 | int write_OW_register(char *device_ID, int start_add, int n, char *data){ |
| cyberjoey | 9:36ba3626aab7 | 608 | int i; |
| cyberjoey | 9:36ba3626aab7 | 609 | //select device |
| cyberjoey | 9:36ba3626aab7 | 610 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 611 | OW_match_rom(device_ID); |
| cyberjoey | 9:36ba3626aab7 | 612 | //set start address |
| cyberjoey | 9:36ba3626aab7 | 613 | OW_write_byte(OT07_OW_WRITE); // send 0xCC, OW write register command for OT07 |
| cyberjoey | 9:36ba3626aab7 | 614 | OW_write_byte(start_add); // send write register start address |
| cyberjoey | 9:36ba3626aab7 | 615 | OW_write_byte(n-1); // send length of bytes to write (00-> one byte) |
| cyberjoey | 9:36ba3626aab7 | 616 | for(i=0;i<n;i++){ // send n bytes of data |
| cyberjoey | 9:36ba3626aab7 | 617 | OW_write_byte(data[i]); |
| cyberjoey | 9:36ba3626aab7 | 618 | } |
| cyberjoey | 9:36ba3626aab7 | 619 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 620 | |
| cyberjoey | 9:36ba3626aab7 | 621 | // --- todo ---- |
| cyberjoey | 9:36ba3626aab7 | 622 | // calculate CRC of |
| cyberjoey | 9:36ba3626aab7 | 623 | // Command + length + n bytes sent |
| cyberjoey | 9:36ba3626aab7 | 624 | // compare with 2 bytes read |
| cyberjoey | 9:36ba3626aab7 | 625 | |
| cyberjoey | 9:36ba3626aab7 | 626 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 627 | |
| cyberjoey | 9:36ba3626aab7 | 628 | }//end write_OW_register |
| cyberjoey | 9:36ba3626aab7 | 629 | |
| cyberjoey | 9:36ba3626aab7 | 630 | |
| cyberjoey | 9:36ba3626aab7 | 631 | |
| cyberjoey | 9:36ba3626aab7 | 632 | |
| cyberjoey | 9:36ba3626aab7 | 633 | // ******************** read_OW_register() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 634 | // read_OW_register(char*, int, int, char*) |
| cyberjoey | 9:36ba3626aab7 | 635 | // char* pointer to rom_id |
| cyberjoey | 9:36ba3626aab7 | 636 | // int start address |
| cyberjoey | 9:36ba3626aab7 | 637 | // int number of bytes to read, not including CRC bytes |
| cyberjoey | 9:36ba3626aab7 | 638 | // char* pointer to save data location |
| cyberjoey | 9:36ba3626aab7 | 639 | // returns 0 if CRC match data |
| cyberjoey | 9:36ba3626aab7 | 640 | // returns -1 if CRC does not match data |
| cyberjoey | 9:36ba3626aab7 | 641 | // |
| cyberjoey | 9:36ba3626aab7 | 642 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 643 | |
| cyberjoey | 9:36ba3626aab7 | 644 | |
| cyberjoey | 9:36ba3626aab7 | 645 | |
| cyberjoey | 9:36ba3626aab7 | 646 | int read_OW_register(char *device_ID, int start_add, int n, char *data){ |
| cyberjoey | 9:36ba3626aab7 | 647 | |
| cyberjoey | 9:36ba3626aab7 | 648 | //int i; |
| cyberjoey | 9:36ba3626aab7 | 649 | //select device |
| cyberjoey | 9:36ba3626aab7 | 650 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 651 | OW_match_rom(device_ID); |
| cyberjoey | 9:36ba3626aab7 | 652 | //set start address |
| cyberjoey | 9:36ba3626aab7 | 653 | OW_write_byte(OT07_OW_READ); // send 0x33, OW read register command for OT07 |
| cyberjoey | 9:36ba3626aab7 | 654 | OW_write_byte(start_add); // send read register start address |
| cyberjoey | 9:36ba3626aab7 | 655 | OW_write_byte(n-1); // send length of bytes to read (0 -> 1 byte) |
| cyberjoey | 9:36ba3626aab7 | 656 | OW_read_byte(data,n+2); // read n bytes plus 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 657 | |
| cyberjoey | 9:36ba3626aab7 | 658 | // debug output |
| cyberjoey | 9:36ba3626aab7 | 659 | //for(i=0;i<n+2;i++) db.printf("read_OW_register - [%2d][%02X]\r\n",i,data[i]); |
| cyberjoey | 9:36ba3626aab7 | 660 | |
| cyberjoey | 9:36ba3626aab7 | 661 | |
| cyberjoey | 9:36ba3626aab7 | 662 | // --- todo ---- |
| cyberjoey | 9:36ba3626aab7 | 663 | // calculate CRC of |
| cyberjoey | 9:36ba3626aab7 | 664 | // Command + length + n bytes |
| cyberjoey | 9:36ba3626aab7 | 665 | // compare with last 2 bytes read |
| cyberjoey | 9:36ba3626aab7 | 666 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 667 | |
| cyberjoey | 9:36ba3626aab7 | 668 | }//end read_OW_register |
| cyberjoey | 9:36ba3626aab7 | 669 | |
| cyberjoey | 9:36ba3626aab7 | 670 | int set_test_mode(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 671 | char data[4]; |
| cyberjoey | 9:36ba3626aab7 | 672 | |
| cyberjoey | 9:36ba3626aab7 | 673 | // enter test mode |
| cyberjoey | 9:36ba3626aab7 | 674 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 675 | OW_match_rom(device_id); // match ROM |
| cyberjoey | 9:36ba3626aab7 | 676 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 677 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 678 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 679 | OW_write_byte(0x54); // first half of test mode pass code |
| cyberjoey | 9:36ba3626aab7 | 680 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 681 | |
| cyberjoey | 9:36ba3626aab7 | 682 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 683 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 684 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 685 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 686 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 687 | OW_write_byte(0x4D); // second half of test mode pass code |
| cyberjoey | 9:36ba3626aab7 | 688 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 689 | |
| cyberjoey | 9:36ba3626aab7 | 690 | // set ADC_ENABLED |
| cyberjoey | 9:36ba3626aab7 | 691 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 692 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 693 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 694 | OW_write_byte(0x81); // device register address |
| cyberjoey | 9:36ba3626aab7 | 695 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 696 | OW_write_byte(0x04); // ADC_ENABLED code |
| cyberjoey | 9:36ba3626aab7 | 697 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 698 | |
| cyberjoey | 9:36ba3626aab7 | 699 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 700 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 701 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 702 | OW_write_byte(0x98); // device register address |
| cyberjoey | 9:36ba3626aab7 | 703 | OW_write_byte(0x05); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 704 | OW_write_byte(0x40); // add 98 data 40 |
| cyberjoey | 9:36ba3626aab7 | 705 | OW_write_byte(0xD4); // add 99 data D4 |
| cyberjoey | 9:36ba3626aab7 | 706 | OW_write_byte(0xE0); // add 9A data E0 |
| cyberjoey | 9:36ba3626aab7 | 707 | OW_write_byte(0xB3); // add 9B data B3 |
| cyberjoey | 9:36ba3626aab7 | 708 | OW_write_byte(0x09); // add 9C data 09 |
| cyberjoey | 9:36ba3626aab7 | 709 | OW_write_byte(0xBA); // add 9D data BA |
| cyberjoey | 9:36ba3626aab7 | 710 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 711 | |
| cyberjoey | 9:36ba3626aab7 | 712 | |
| cyberjoey | 9:36ba3626aab7 | 713 | //OTP copy |
| cyberjoey | 9:36ba3626aab7 | 714 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 715 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 716 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 717 | OW_write_byte(0x80); // device register address |
| cyberjoey | 9:36ba3626aab7 | 718 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 719 | OW_write_byte(0x82); // |
| cyberjoey | 9:36ba3626aab7 | 720 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 721 | |
| cyberjoey | 9:36ba3626aab7 | 722 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 723 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 724 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 725 | OW_write_byte(0x80); // device register address |
| cyberjoey | 9:36ba3626aab7 | 726 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 727 | OW_write_byte(0x02); // |
| cyberjoey | 9:36ba3626aab7 | 728 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 729 | |
| cyberjoey | 9:36ba3626aab7 | 730 | |
| cyberjoey | 9:36ba3626aab7 | 731 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 732 | OW_match_rom(device_id); |
| cyberjoey | 9:36ba3626aab7 | 733 | OW_write_byte(OT07_OW_WRITE); // device write register command |
| cyberjoey | 9:36ba3626aab7 | 734 | OW_write_byte(0xFF); // device register address |
| cyberjoey | 9:36ba3626aab7 | 735 | OW_write_byte(0x00); // number of bytes to write -1 |
| cyberjoey | 9:36ba3626aab7 | 736 | OW_write_byte(0x00); // exit Test mode |
| cyberjoey | 9:36ba3626aab7 | 737 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 738 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 739 | }// end set_test_mode() |
| cyberjoey | 9:36ba3626aab7 | 740 | |
| cyberjoey | 9:36ba3626aab7 | 741 | |
| cyberjoey | 9:36ba3626aab7 | 742 | |
| cyberjoey | 9:36ba3626aab7 | 743 | // ******************** convert_temperature() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 744 | // convert_temperature() |
| cyberjoey | 9:36ba3626aab7 | 745 | // returns 0 |
| cyberjoey | 9:36ba3626aab7 | 746 | // dose not block for 0.75 seconds. |
| cyberjoey | 9:36ba3626aab7 | 747 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 748 | |
| cyberjoey | 9:36ba3626aab7 | 749 | int convert_temperature(){ // convert sent to all OW devices |
| cyberjoey | 9:36ba3626aab7 | 750 | |
| cyberjoey | 9:36ba3626aab7 | 751 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 752 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 753 | |
| cyberjoey | 9:36ba3626aab7 | 754 | OW_write_byte(OW_SKIP); // send 0xCC, skip rom command |
| cyberjoey | 9:36ba3626aab7 | 755 | |
| cyberjoey | 9:36ba3626aab7 | 756 | // activate strong pullup |
| cyberjoey | 9:36ba3626aab7 | 757 | data[0] = WDC; // send 0xD2 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 758 | data[1] = 0xA5; // 1010 0101 b strong and active pullup on |
| cyberjoey | 9:36ba3626aab7 | 759 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 760 | |
| cyberjoey | 9:36ba3626aab7 | 761 | //convert command |
| cyberjoey | 9:36ba3626aab7 | 762 | OW_write_byte(OT07_OW_CONVERT); // send 0x44, Convert Temperature |
| cyberjoey | 9:36ba3626aab7 | 763 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 764 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 765 | } |
| cyberjoey | 9:36ba3626aab7 | 766 | |
| cyberjoey | 9:36ba3626aab7 | 767 | |
| cyberjoey | 9:36ba3626aab7 | 768 | // ******************** convert_temperature() ******************************** |
| cyberjoey | 9:36ba3626aab7 | 769 | // convert_temperature(char *) takes 8 byte rom_id as input |
| cyberjoey | 9:36ba3626aab7 | 770 | // returns 0 |
| cyberjoey | 9:36ba3626aab7 | 771 | // dose not block for 0.75 seconds. |
| cyberjoey | 9:36ba3626aab7 | 772 | // ***************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 773 | |
| cyberjoey | 9:36ba3626aab7 | 774 | int convert_temperature(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 775 | |
| cyberjoey | 9:36ba3626aab7 | 776 | char data[8]; |
| cyberjoey | 9:36ba3626aab7 | 777 | OW_reset(); |
| cyberjoey | 9:36ba3626aab7 | 778 | OW_match_rom(device_id); // send device id |
| cyberjoey | 9:36ba3626aab7 | 779 | |
| cyberjoey | 9:36ba3626aab7 | 780 | // activate strong pullup |
| cyberjoey | 9:36ba3626aab7 | 781 | data[0] = WDC; // send 0xD2 Write Device Configuration |
| cyberjoey | 9:36ba3626aab7 | 782 | data[1] = 0xA5; // 1010 0101 b strong and active pullup on |
| cyberjoey | 9:36ba3626aab7 | 783 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 784 | |
| cyberjoey | 9:36ba3626aab7 | 785 | //convert command |
| cyberjoey | 9:36ba3626aab7 | 786 | OW_write_byte(OT07_OW_CONVERT); // send 0x44, Convert Temperature |
| cyberjoey | 9:36ba3626aab7 | 787 | OW_read_byte(data,2); // read 2 byte CRC |
| cyberjoey | 9:36ba3626aab7 | 788 | return 0; |
| cyberjoey | 9:36ba3626aab7 | 789 | } |
| cyberjoey | 9:36ba3626aab7 | 790 | |
| cyberjoey | 9:36ba3626aab7 | 791 | //************************ get_temperature() ********************************* |
| cyberjoey | 9:36ba3626aab7 | 792 | // get_temperature(char *) takes 8 byte rom_id as input |
| cyberjoey | 9:36ba3626aab7 | 793 | // returns double temperature in oC |
| cyberjoey | 9:36ba3626aab7 | 794 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 795 | |
| cyberjoey | 9:36ba3626aab7 | 796 | double get_temperature(char *device_id){ |
| cyberjoey | 9:36ba3626aab7 | 797 | |
| cyberjoey | 9:36ba3626aab7 | 798 | char t[4]; |
| cyberjoey | 9:36ba3626aab7 | 799 | double T; |
| cyberjoey | 9:36ba3626aab7 | 800 | int count; |
| cyberjoey | 9:36ba3626aab7 | 801 | // char data[2]; |
| cyberjoey | 9:36ba3626aab7 | 802 | |
| cyberjoey | 9:36ba3626aab7 | 803 | read_OW_register(device_id,OT07_FIFO_DATA,0x02,t); // Read temperature from FIFO, 2 bytes |
| cyberjoey | 9:36ba3626aab7 | 804 | //db.printf("get_temperature -- FIFO[%02X %02X] CRC[%02X %02X]\r\n",t[0],t[1],t[2],t[3]); |
| cyberjoey | 9:36ba3626aab7 | 805 | //calculate temperture from data |
| cyberjoey | 9:36ba3626aab7 | 806 | count = (int)(t[0]*256 + t[1]); |
| cyberjoey | 9:36ba3626aab7 | 807 | if (count >= 32768)count = count - 65536; // 2s comp |
| cyberjoey | 9:36ba3626aab7 | 808 | T = (double)count*0.005; |
| cyberjoey | 9:36ba3626aab7 | 809 | |
| cyberjoey | 9:36ba3626aab7 | 810 | //db.printf("T = %f count[%5d]\r\n", T,count); |
| cyberjoey | 9:36ba3626aab7 | 811 | return T; |
| cyberjoey | 9:36ba3626aab7 | 812 | }// end get_temperature() |
| cyberjoey | 9:36ba3626aab7 | 813 | |
| cyberjoey | 9:36ba3626aab7 | 814 | |
| cyberjoey | 10:148da21c297e | 815 | void write_settings_file(int interval, bool device_logged[MAX_DEVICES]) |
| cyberjoey | 10:148da21c297e | 816 | { |
| cyberjoey | 10:148da21c297e | 817 | FILE *fp = fopen(settings_file, "w"); |
| cyberjoey | 10:148da21c297e | 818 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 819 | { |
| cyberjoey | 10:148da21c297e | 820 | fprintf(fp, "i %d\r\n", interval); |
| cyberjoey | 10:148da21c297e | 821 | |
| cyberjoey | 10:148da21c297e | 822 | fprintf(fp, "d"); |
| cyberjoey | 10:148da21c297e | 823 | |
| cyberjoey | 10:148da21c297e | 824 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 10:148da21c297e | 825 | { |
| cyberjoey | 10:148da21c297e | 826 | if(device_logged[i] == true) |
| cyberjoey | 10:148da21c297e | 827 | { |
| cyberjoey | 10:148da21c297e | 828 | fprintf(fp," %d", i); |
| cyberjoey | 10:148da21c297e | 829 | } |
| cyberjoey | 10:148da21c297e | 830 | } |
| cyberjoey | 10:148da21c297e | 831 | fprintf(fp,"\r\n"); |
| cyberjoey | 10:148da21c297e | 832 | |
| cyberjoey | 10:148da21c297e | 833 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 834 | } |
| cyberjoey | 10:148da21c297e | 835 | return; |
| cyberjoey | 10:148da21c297e | 836 | } |
| cyberjoey | 10:148da21c297e | 837 | |
| cyberjoey | 10:148da21c297e | 838 | bool print_settings_file() |
| cyberjoey | 10:148da21c297e | 839 | { |
| cyberjoey | 10:148da21c297e | 840 | FILE *fp = fopen(settings_file, "r"); |
| cyberjoey | 10:148da21c297e | 841 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 842 | { |
| cyberjoey | 10:148da21c297e | 843 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 844 | |
| cyberjoey | 10:148da21c297e | 845 | // Read contents from file |
| cyberjoey | 10:148da21c297e | 846 | char c = fgetc(fp); |
| cyberjoey | 11:94a7379c0db8 | 847 | |
| cyberjoey | 10:148da21c297e | 848 | while (!feof(fp)) |
| cyberjoey | 10:148da21c297e | 849 | { |
| cyberjoey | 10:148da21c297e | 850 | pc.printf("%c", c); |
| cyberjoey | 10:148da21c297e | 851 | c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 852 | } |
| cyberjoey | 10:148da21c297e | 853 | |
| cyberjoey | 10:148da21c297e | 854 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 855 | |
| cyberjoey | 10:148da21c297e | 856 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 857 | } |
| cyberjoey | 10:148da21c297e | 858 | else |
| cyberjoey | 10:148da21c297e | 859 | { |
| cyberjoey | 10:148da21c297e | 860 | return false; |
| cyberjoey | 10:148da21c297e | 861 | } |
| cyberjoey | 10:148da21c297e | 862 | return true; |
| cyberjoey | 10:148da21c297e | 863 | } |
| cyberjoey | 10:148da21c297e | 864 | |
| cyberjoey | 10:148da21c297e | 865 | bool print_log_file() |
| cyberjoey | 10:148da21c297e | 866 | { |
| cyberjoey | 10:148da21c297e | 867 | FILE *fp = fopen(log_file, "r"); |
| cyberjoey | 10:148da21c297e | 868 | if (fp != NULL) |
| cyberjoey | 10:148da21c297e | 869 | { |
| cyberjoey | 10:148da21c297e | 870 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 871 | |
| cyberjoey | 10:148da21c297e | 872 | // Read contents from file |
| cyberjoey | 10:148da21c297e | 873 | char c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 874 | while (!feof(fp)) |
| cyberjoey | 10:148da21c297e | 875 | { |
| cyberjoey | 10:148da21c297e | 876 | pc.printf("%c", c); |
| cyberjoey | 10:148da21c297e | 877 | c = fgetc(fp); |
| cyberjoey | 10:148da21c297e | 878 | } |
| cyberjoey | 10:148da21c297e | 879 | |
| cyberjoey | 10:148da21c297e | 880 | pc.printf("*\r\n"); |
| cyberjoey | 10:148da21c297e | 881 | |
| cyberjoey | 10:148da21c297e | 882 | fclose(fp); |
| cyberjoey | 10:148da21c297e | 883 | } |
| cyberjoey | 10:148da21c297e | 884 | else |
| cyberjoey | 10:148da21c297e | 885 | { |
| cyberjoey | 10:148da21c297e | 886 | return false; |
| cyberjoey | 10:148da21c297e | 887 | } |
| cyberjoey | 10:148da21c297e | 888 | return true; |
| cyberjoey | 10:148da21c297e | 889 | } |
| cyberjoey | 10:148da21c297e | 890 | |
| cyberjoey | 11:94a7379c0db8 | 891 | int getline(char **lineptr, int *n, FILE *stream) { |
| cyberjoey | 11:94a7379c0db8 | 892 | char *bufptr = NULL; |
| cyberjoey | 11:94a7379c0db8 | 893 | char *p = bufptr; |
| cyberjoey | 11:94a7379c0db8 | 894 | size_t size; |
| cyberjoey | 11:94a7379c0db8 | 895 | int c; |
| cyberjoey | 11:94a7379c0db8 | 896 | |
| cyberjoey | 11:94a7379c0db8 | 897 | if (lineptr == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 898 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 899 | } |
| cyberjoey | 11:94a7379c0db8 | 900 | if (stream == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 901 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 902 | } |
| cyberjoey | 11:94a7379c0db8 | 903 | if (n == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 904 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 905 | } |
| cyberjoey | 11:94a7379c0db8 | 906 | bufptr = *lineptr; |
| cyberjoey | 11:94a7379c0db8 | 907 | size = *n; |
| cyberjoey | 11:94a7379c0db8 | 908 | |
| cyberjoey | 11:94a7379c0db8 | 909 | c = fgetc(stream); |
| cyberjoey | 11:94a7379c0db8 | 910 | if (c == EOF) { |
| cyberjoey | 11:94a7379c0db8 | 911 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 912 | } |
| cyberjoey | 11:94a7379c0db8 | 913 | if (bufptr == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 914 | bufptr = (char *)malloc(128); |
| cyberjoey | 11:94a7379c0db8 | 915 | if (bufptr == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 916 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 917 | } |
| cyberjoey | 11:94a7379c0db8 | 918 | size = 128; |
| cyberjoey | 11:94a7379c0db8 | 919 | } |
| cyberjoey | 11:94a7379c0db8 | 920 | p = bufptr; |
| cyberjoey | 11:94a7379c0db8 | 921 | while(c != EOF) { |
| cyberjoey | 11:94a7379c0db8 | 922 | if ((p - bufptr) > (size - 1)) { |
| cyberjoey | 11:94a7379c0db8 | 923 | size = size + 128; |
| cyberjoey | 11:94a7379c0db8 | 924 | bufptr = (char *)realloc(bufptr, size); |
| cyberjoey | 11:94a7379c0db8 | 925 | if (bufptr == NULL) { |
| cyberjoey | 11:94a7379c0db8 | 926 | return -1; |
| cyberjoey | 11:94a7379c0db8 | 927 | } |
| cyberjoey | 11:94a7379c0db8 | 928 | } |
| cyberjoey | 11:94a7379c0db8 | 929 | *p++ = c; |
| cyberjoey | 11:94a7379c0db8 | 930 | if (c == '\n') { |
| cyberjoey | 11:94a7379c0db8 | 931 | break; |
| cyberjoey | 11:94a7379c0db8 | 932 | } |
| cyberjoey | 11:94a7379c0db8 | 933 | c = fgetc(stream); |
| cyberjoey | 11:94a7379c0db8 | 934 | } |
| cyberjoey | 11:94a7379c0db8 | 935 | |
| cyberjoey | 11:94a7379c0db8 | 936 | *p++ = '\0'; |
| cyberjoey | 11:94a7379c0db8 | 937 | *lineptr = bufptr; |
| cyberjoey | 11:94a7379c0db8 | 938 | *n = size; |
| cyberjoey | 11:94a7379c0db8 | 939 | |
| cyberjoey | 11:94a7379c0db8 | 940 | return p - bufptr - 1; |
| cyberjoey | 11:94a7379c0db8 | 941 | } |
| cyberjoey | 11:94a7379c0db8 | 942 | |
| cyberjoey | 11:94a7379c0db8 | 943 | //returns true if settings file exists and is in the proper format |
| cyberjoey | 11:94a7379c0db8 | 944 | bool apply_settings_file(bool (&logged_devices)[MAX_DEVICES], int& interval) |
| cyberjoey | 11:94a7379c0db8 | 945 | { |
| cyberjoey | 11:94a7379c0db8 | 946 | //int log_interval = 10; |
| cyberjoey | 11:94a7379c0db8 | 947 | //bool devices[MAX_DEVICES]; |
| cyberjoey | 11:94a7379c0db8 | 948 | |
| cyberjoey | 11:94a7379c0db8 | 949 | char * line = NULL; |
| cyberjoey | 11:94a7379c0db8 | 950 | int len = 0; |
| cyberjoey | 11:94a7379c0db8 | 951 | int line_number = 0; |
| cyberjoey | 11:94a7379c0db8 | 952 | |
| cyberjoey | 11:94a7379c0db8 | 953 | |
| cyberjoey | 11:94a7379c0db8 | 954 | FILE *fp = fopen("/sd/settings.txt", "r"); |
| cyberjoey | 11:94a7379c0db8 | 955 | if (fp != NULL) |
| cyberjoey | 11:94a7379c0db8 | 956 | { |
| cyberjoey | 11:94a7379c0db8 | 957 | |
| cyberjoey | 11:94a7379c0db8 | 958 | //initialize devices to all false; |
| cyberjoey | 11:94a7379c0db8 | 959 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 11:94a7379c0db8 | 960 | { |
| cyberjoey | 11:94a7379c0db8 | 961 | logged_devices[i] = false; |
| cyberjoey | 11:94a7379c0db8 | 962 | } |
| cyberjoey | 11:94a7379c0db8 | 963 | |
| cyberjoey | 11:94a7379c0db8 | 964 | |
| cyberjoey | 11:94a7379c0db8 | 965 | // Read contents from file |
| cyberjoey | 11:94a7379c0db8 | 966 | while ((getline(&line, &len, fp)) != -1) |
| cyberjoey | 11:94a7379c0db8 | 967 | { |
| cyberjoey | 11:94a7379c0db8 | 968 | line_number++; |
| cyberjoey | 11:94a7379c0db8 | 969 | |
| cyberjoey | 11:94a7379c0db8 | 970 | char i = 0; |
| cyberjoey | 11:94a7379c0db8 | 971 | char c = line[i]; |
| cyberjoey | 11:94a7379c0db8 | 972 | while(c != '\0') |
| cyberjoey | 11:94a7379c0db8 | 973 | { |
| cyberjoey | 11:94a7379c0db8 | 974 | int number; |
| cyberjoey | 11:94a7379c0db8 | 975 | int n; |
| cyberjoey | 11:94a7379c0db8 | 976 | sscanf((line+i), "%d%n", &number, &n); |
| cyberjoey | 11:94a7379c0db8 | 977 | if(isdigit(c)) |
| cyberjoey | 11:94a7379c0db8 | 978 | { |
| cyberjoey | 11:94a7379c0db8 | 979 | if(line_number == 1) |
| cyberjoey | 11:94a7379c0db8 | 980 | { |
| cyberjoey | 11:94a7379c0db8 | 981 | interval = number; |
| cyberjoey | 11:94a7379c0db8 | 982 | } |
| cyberjoey | 11:94a7379c0db8 | 983 | else if(line_number == 2) |
| cyberjoey | 11:94a7379c0db8 | 984 | { |
| cyberjoey | 11:94a7379c0db8 | 985 | logged_devices[number] = true; |
| cyberjoey | 11:94a7379c0db8 | 986 | } |
| cyberjoey | 11:94a7379c0db8 | 987 | if(n > 1) |
| cyberjoey | 11:94a7379c0db8 | 988 | i = i + (n - 1); |
| cyberjoey | 11:94a7379c0db8 | 989 | } |
| cyberjoey | 11:94a7379c0db8 | 990 | i++; |
| cyberjoey | 11:94a7379c0db8 | 991 | c = line[i]; |
| cyberjoey | 11:94a7379c0db8 | 992 | } |
| cyberjoey | 11:94a7379c0db8 | 993 | } |
| cyberjoey | 11:94a7379c0db8 | 994 | |
| cyberjoey | 11:94a7379c0db8 | 995 | fclose(fp); |
| cyberjoey | 11:94a7379c0db8 | 996 | } |
| cyberjoey | 11:94a7379c0db8 | 997 | else |
| cyberjoey | 11:94a7379c0db8 | 998 | { |
| cyberjoey | 11:94a7379c0db8 | 999 | return false; |
| cyberjoey | 11:94a7379c0db8 | 1000 | } |
| cyberjoey | 11:94a7379c0db8 | 1001 | |
| cyberjoey | 11:94a7379c0db8 | 1002 | return true; |
| cyberjoey | 11:94a7379c0db8 | 1003 | |
| cyberjoey | 11:94a7379c0db8 | 1004 | } |
| cyberjoey | 11:94a7379c0db8 | 1005 | |
| cyberjoey | 11:94a7379c0db8 | 1006 | |
| cyberjoey | 11:94a7379c0db8 | 1007 | |
| cyberjoey | 10:148da21c297e | 1008 | |
| cyberjoey | 9:36ba3626aab7 | 1009 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 1010 | // main() |
| cyberjoey | 9:36ba3626aab7 | 1011 | //****************************************************************************** |
| cyberjoey | 9:36ba3626aab7 | 1012 | |
| cyberjoey | 9:36ba3626aab7 | 1013 | |
| switches | 0:60a522ae2e35 | 1014 | int main() |
| switches | 0:60a522ae2e35 | 1015 | { |
| cyberjoey | 9:36ba3626aab7 | 1016 | |
| cyberjoey | 9:36ba3626aab7 | 1017 | char data[130]; |
| cyberjoey | 9:36ba3626aab7 | 1018 | int device_count = 0; // number of OW devices found by search_rom() |
| cyberjoey | 9:36ba3626aab7 | 1019 | int i; |
| cyberjoey | 9:36ba3626aab7 | 1020 | int j; |
| cyberjoey | 9:36ba3626aab7 | 1021 | int k; |
| cyberjoey | 9:36ba3626aab7 | 1022 | |
| cyberjoey | 9:36ba3626aab7 | 1023 | |
| cyberjoey | 9:36ba3626aab7 | 1024 | char device_id[ID_LENGTH]; //8 byte rom id of current slected device |
| cyberjoey | 9:36ba3626aab7 | 1025 | char rom_id_list[MAX_DEVICES][ID_LENGTH]; //List of rom id for each device on OW bus |
| cyberjoey | 9:36ba3626aab7 | 1026 | |
| cyberjoey | 9:36ba3626aab7 | 1027 | double T[MAX_DEVICES]; |
| cyberjoey | 9:36ba3626aab7 | 1028 | |
| cyberjoey | 10:148da21c297e | 1029 | bool device_logged[MAX_DEVICES]; |
| cyberjoey | 11:94a7379c0db8 | 1030 | //initialize device_logged to all false; |
| cyberjoey | 11:94a7379c0db8 | 1031 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 11:94a7379c0db8 | 1032 | { |
| cyberjoey | 11:94a7379c0db8 | 1033 | device_logged[i] = false; |
| cyberjoey | 11:94a7379c0db8 | 1034 | } |
| cyberjoey | 10:148da21c297e | 1035 | |
| cyberjoey | 9:36ba3626aab7 | 1036 | // i/o variables |
| cyberjoey | 9:36ba3626aab7 | 1037 | char rx_buff[128]; // comport input buffer |
| cyberjoey | 9:36ba3626aab7 | 1038 | int rx_index; // rx_buffer pointer |
| cyberjoey | 9:36ba3626aab7 | 1039 | char c; // command type character |
| cyberjoey | 9:36ba3626aab7 | 1040 | int n; // argument count |
| cyberjoey | 9:36ba3626aab7 | 1041 | int arg1; // argumnet 1 |
| cyberjoey | 9:36ba3626aab7 | 1042 | int arg2; // argument 2 |
| cyberjoey | 9:36ba3626aab7 | 1043 | int device; // device argument |
| cyberjoey | 9:36ba3626aab7 | 1044 | int num_bytes; |
| cyberjoey | 9:36ba3626aab7 | 1045 | |
| cyberjoey | 9:36ba3626aab7 | 1046 | int time_count; |
| cyberjoey | 9:36ba3626aab7 | 1047 | int log_interval = PERIOD; |
| cyberjoey | 9:36ba3626aab7 | 1048 | int time_to_sample; |
| cyberjoey | 9:36ba3626aab7 | 1049 | |
| cyberjoey | 11:94a7379c0db8 | 1050 | apply_settings_file(device_logged, log_interval); |
| cyberjoey | 11:94a7379c0db8 | 1051 | |
| cyberjoey | 9:36ba3626aab7 | 1052 | //************* init ticker timer callbacks **************** |
| cyberjoey | 9:36ba3626aab7 | 1053 | timer_1.attach(&LED_blink_callback,1.0); //start ticker, once per sec. |
| cyberjoey | 9:36ba3626aab7 | 1054 | |
| cyberjoey | 9:36ba3626aab7 | 1055 | |
| cyberjoey | 9:36ba3626aab7 | 1056 | i2c.frequency(400000); //set I2C clock to 100kHz |
| cyberjoey | 9:36ba3626aab7 | 1057 | |
| cyberjoey | 9:36ba3626aab7 | 1058 | //wait(1); //wait 1 sec for USB serial port to init. |
| cyberjoey | 9:36ba3626aab7 | 1059 | |
| cyberjoey | 9:36ba3626aab7 | 1060 | //db.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\ndaplink serial port\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1061 | //pc.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\nmicro USB serial port\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1062 | |
| cyberjoey | 9:36ba3626aab7 | 1063 | rLED = LED_OFF; |
| switches | 6:96d8d823e292 | 1064 | gLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 1065 | bLED = LED_ON; |
| cyberjoey | 9:36ba3626aab7 | 1066 | |
| cyberjoey | 9:36ba3626aab7 | 1067 | |
| cyberjoey | 9:36ba3626aab7 | 1068 | // reset DS2484 |
| cyberjoey | 9:36ba3626aab7 | 1069 | data[0] = 0xE1; |
| cyberjoey | 9:36ba3626aab7 | 1070 | data[1] = 0xF0; |
| cyberjoey | 9:36ba3626aab7 | 1071 | i2c.write(DS2484_ADD,data,1,1); |
| cyberjoey | 9:36ba3626aab7 | 1072 | i2c.read(DS2484_ADD,data,1); |
| cyberjoey | 9:36ba3626aab7 | 1073 | |
| cyberjoey | 9:36ba3626aab7 | 1074 | //db.printf("DS2484 Reset --> status[%02X]\r\n",data[0]); |
| cyberjoey | 9:36ba3626aab7 | 1075 | |
| cyberjoey | 9:36ba3626aab7 | 1076 | // ****************** search for all OW devices on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 1077 | |
| cyberjoey | 9:36ba3626aab7 | 1078 | device_count = search_rom(rom_id_list); |
| cyberjoey | 10:148da21c297e | 1079 | |
| cyberjoey | 9:36ba3626aab7 | 1080 | |
| cyberjoey | 9:36ba3626aab7 | 1081 | // print out rom codes found |
| cyberjoey | 9:36ba3626aab7 | 1082 | /*pc.printf("\r\nsearch rom\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1083 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1084 | //pc.printf("device[%02X] rom id[ ",j); |
| cyberjoey | 9:36ba3626aab7 | 1085 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 1086 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 1087 | } |
| cyberjoey | 9:36ba3626aab7 | 1088 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1089 | } |
| cyberjoey | 9:36ba3626aab7 | 1090 | pc.printf("\r\n");*/ |
| cyberjoey | 9:36ba3626aab7 | 1091 | |
| cyberjoey | 9:36ba3626aab7 | 1092 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1093 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1094 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1095 | } |
| cyberjoey | 9:36ba3626aab7 | 1096 | set_test_mode(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1097 | //db.printf("device[%2X] in test mode\r\n",j); |
| cyberjoey | 9:36ba3626aab7 | 1098 | } |
| cyberjoey | 9:36ba3626aab7 | 1099 | |
| cyberjoey | 9:36ba3626aab7 | 1100 | |
| cyberjoey | 9:36ba3626aab7 | 1101 | rx_index = 0; //character buffer index for input from PC |
| cyberjoey | 9:36ba3626aab7 | 1102 | |
| cyberjoey | 9:36ba3626aab7 | 1103 | button.fall(&btn_pressed); |
| cyberjoey | 11:94a7379c0db8 | 1104 | SDInsert.fall(&sd_insert); |
| cyberjoey | 11:94a7379c0db8 | 1105 | |
| cyberjoey | 11:94a7379c0db8 | 1106 | sd.disk_initialize(); //initialize sd card |
| cyberjoey | 9:36ba3626aab7 | 1107 | |
| cyberjoey | 9:36ba3626aab7 | 1108 | //db.printf("start Main loop\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1109 | //db.printf("tf[%d]\r\n",tick_flag); |
| cyberjoey | 9:36ba3626aab7 | 1110 | while(1) { // start main loop, take data if logging, check for input, repeat |
| cyberjoey | 9:36ba3626aab7 | 1111 | |
| cyberjoey | 11:94a7379c0db8 | 1112 | if(sd_insert_flag == true) |
| cyberjoey | 11:94a7379c0db8 | 1113 | { |
| cyberjoey | 11:94a7379c0db8 | 1114 | sd.disk_initialize(); |
| cyberjoey | 11:94a7379c0db8 | 1115 | sd_insert_flag = false; |
| cyberjoey | 11:94a7379c0db8 | 1116 | } |
| cyberjoey | 11:94a7379c0db8 | 1117 | |
| cyberjoey | 9:36ba3626aab7 | 1118 | if(button_flag == true) |
| cyberjoey | 9:36ba3626aab7 | 1119 | { |
| cyberjoey | 9:36ba3626aab7 | 1120 | if(log_flag == false){ //start logging |
| cyberjoey | 9:36ba3626aab7 | 1121 | |
| cyberjoey | 9:36ba3626aab7 | 1122 | if(SDDetect)//if SD card not detected |
| cyberjoey | 9:36ba3626aab7 | 1123 | { |
| cyberjoey | 9:36ba3626aab7 | 1124 | error_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1125 | error_ticks = 6; |
| cyberjoey | 9:36ba3626aab7 | 1126 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1127 | } |
| cyberjoey | 9:36ba3626aab7 | 1128 | else |
| cyberjoey | 9:36ba3626aab7 | 1129 | { |
| cyberjoey | 11:94a7379c0db8 | 1130 | apply_settings_file(device_logged, log_interval); |
| cyberjoey | 11:94a7379c0db8 | 1131 | |
| cyberjoey | 10:148da21c297e | 1132 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 1133 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 1134 | { |
| cyberjoey | 9:36ba3626aab7 | 1135 | fprintf(fp, "Time(s)"); |
| cyberjoey | 9:36ba3626aab7 | 1136 | |
| cyberjoey | 9:36ba3626aab7 | 1137 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 1138 | { |
| cyberjoey | 11:94a7379c0db8 | 1139 | if(device_logged[j]) |
| cyberjoey | 11:94a7379c0db8 | 1140 | fprintf(fp,", Device %d Temperature (C)",j); |
| cyberjoey | 9:36ba3626aab7 | 1141 | } |
| cyberjoey | 9:36ba3626aab7 | 1142 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1143 | |
| cyberjoey | 9:36ba3626aab7 | 1144 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1145 | } |
| cyberjoey | 9:36ba3626aab7 | 1146 | |
| cyberjoey | 9:36ba3626aab7 | 1147 | time_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 1148 | |
| cyberjoey | 9:36ba3626aab7 | 1149 | time_to_sample = 0; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1150 | tick_flag = true; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1151 | log_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1152 | } |
| cyberjoey | 9:36ba3626aab7 | 1153 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 1154 | //pc.printf("<stop logging>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1155 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1156 | } |
| cyberjoey | 9:36ba3626aab7 | 1157 | button_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1158 | } |
| switches | 1:90313362ec11 | 1159 | |
| cyberjoey | 9:36ba3626aab7 | 1160 | while(tick_flag == false){ //check for input while waiting for next tick |
| cyberjoey | 9:36ba3626aab7 | 1161 | |
| cyberjoey | 9:36ba3626aab7 | 1162 | // ---------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 1163 | // test for charater input for USB com port |
| cyberjoey | 9:36ba3626aab7 | 1164 | // ---------------------------------------------------------------------------- |
| cyberjoey | 9:36ba3626aab7 | 1165 | |
| cyberjoey | 9:36ba3626aab7 | 1166 | //test if PC sent some charaters |
| cyberjoey | 9:36ba3626aab7 | 1167 | while(pc.readable()){ //characters in buffer, get them |
| cyberjoey | 9:36ba3626aab7 | 1168 | rx_buff[rx_index] = pc.getc(); |
| cyberjoey | 9:36ba3626aab7 | 1169 | //pc.putc(rx_buff[rx_index]); //echo character |
| cyberjoey | 9:36ba3626aab7 | 1170 | //pc.printf("<[%02x] %c i[%d]>",rx_buff[i],rx_buff[i],i); //echo charater |
| cyberjoey | 9:36ba3626aab7 | 1171 | |
| cyberjoey | 9:36ba3626aab7 | 1172 | |
| cyberjoey | 9:36ba3626aab7 | 1173 | if(rx_buff[rx_index] == CR){ |
| cyberjoey | 9:36ba3626aab7 | 1174 | //db.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1175 | pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1176 | rx_buff[++rx_index] = 0; |
| cyberjoey | 9:36ba3626aab7 | 1177 | //db.printf("%s\r\n",rx_buff); |
| cyberjoey | 9:36ba3626aab7 | 1178 | //pc.printf("%s\r\n",rx_buff); |
| cyberjoey | 9:36ba3626aab7 | 1179 | rx_index = -1; // because i++ at end of while give i=0 on next loop |
| cyberjoey | 9:36ba3626aab7 | 1180 | device = 0; |
| cyberjoey | 9:36ba3626aab7 | 1181 | arg1 = 0; |
| cyberjoey | 9:36ba3626aab7 | 1182 | arg2 = 0; |
| cyberjoey | 9:36ba3626aab7 | 1183 | |
| cyberjoey | 9:36ba3626aab7 | 1184 | n = sscanf(rx_buff, " %c %d %x %x", &c, & device, &arg1, &arg2); |
| cyberjoey | 9:36ba3626aab7 | 1185 | |
| cyberjoey | 9:36ba3626aab7 | 1186 | //db.printf("c[%c] d[%d] a1[%x] a2[%x] n[%d]\r\n",c,device,arg1,arg2,n); //echo values read in |
| cyberjoey | 9:36ba3626aab7 | 1187 | //process input |
| cyberjoey | 9:36ba3626aab7 | 1188 | if(n > 0){//got input so process it |
| cyberjoey | 9:36ba3626aab7 | 1189 | switch(c){ |
| cyberjoey | 9:36ba3626aab7 | 1190 | case 'a': |
| cyberjoey | 9:36ba3626aab7 | 1191 | case 'A': // alarm search |
| cyberjoey | 9:36ba3626aab7 | 1192 | // ****************** search for all OW devices with alarm triggered on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 1193 | |
| cyberjoey | 9:36ba3626aab7 | 1194 | device_count = alarm_search(rom_id_list); |
| cyberjoey | 9:36ba3626aab7 | 1195 | |
| cyberjoey | 9:36ba3626aab7 | 1196 | pc.printf("%d devices:\r\n", device_count); |
| cyberjoey | 9:36ba3626aab7 | 1197 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1198 | pc.printf("device[%02X] rom id[",j); |
| cyberjoey | 9:36ba3626aab7 | 1199 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 1200 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 1201 | } |
| cyberjoey | 9:36ba3626aab7 | 1202 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1203 | } |
| cyberjoey | 9:36ba3626aab7 | 1204 | break; |
| cyberjoey | 11:94a7379c0db8 | 1205 | case 'c': |
| cyberjoey | 11:94a7379c0db8 | 1206 | case 'C': |
| cyberjoey | 11:94a7379c0db8 | 1207 | if(!SDDetect) |
| cyberjoey | 11:94a7379c0db8 | 1208 | pc.printf("y\r\n"); |
| cyberjoey | 11:94a7379c0db8 | 1209 | else |
| cyberjoey | 11:94a7379c0db8 | 1210 | pc.printf("n\r\n"); |
| cyberjoey | 11:94a7379c0db8 | 1211 | break; |
| cyberjoey | 10:148da21c297e | 1212 | case 'd': |
| cyberjoey | 10:148da21c297e | 1213 | case 'D': |
| cyberjoey | 10:148da21c297e | 1214 | if(n==1) // if no device number is given |
| cyberjoey | 10:148da21c297e | 1215 | { |
| cyberjoey | 10:148da21c297e | 1216 | //clear device_logged array |
| cyberjoey | 10:148da21c297e | 1217 | for(int i = 0; i < MAX_DEVICES; i++) |
| cyberjoey | 10:148da21c297e | 1218 | { |
| cyberjoey | 10:148da21c297e | 1219 | device_logged[i] = false; |
| cyberjoey | 10:148da21c297e | 1220 | } |
| cyberjoey | 10:148da21c297e | 1221 | |
| cyberjoey | 10:148da21c297e | 1222 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 10:148da21c297e | 1223 | } |
| cyberjoey | 10:148da21c297e | 1224 | if(n == 2) |
| cyberjoey | 10:148da21c297e | 1225 | { |
| cyberjoey | 10:148da21c297e | 1226 | device_logged[device] = true; |
| cyberjoey | 10:148da21c297e | 1227 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 10:148da21c297e | 1228 | } |
| cyberjoey | 10:148da21c297e | 1229 | break; |
| cyberjoey | 9:36ba3626aab7 | 1230 | case 'f': |
| cyberjoey | 9:36ba3626aab7 | 1231 | case 'F': //f is for "flash" for microSD |
| cyberjoey | 11:94a7379c0db8 | 1232 | /*if(!SDDetect) |
| cyberjoey | 9:36ba3626aab7 | 1233 | pc.printf("y\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1234 | else |
| cyberjoey | 11:94a7379c0db8 | 1235 | pc.printf("n\r\n");*/ |
| cyberjoey | 11:94a7379c0db8 | 1236 | apply_settings_file(device_logged, log_interval); |
| cyberjoey | 11:94a7379c0db8 | 1237 | pc.printf("Interval: %d\r\n", log_interval); |
| cyberjoey | 11:94a7379c0db8 | 1238 | bool no_devices = true; |
| cyberjoey | 11:94a7379c0db8 | 1239 | for(i = 0; i < MAX_DEVICES; i ++) |
| cyberjoey | 11:94a7379c0db8 | 1240 | { |
| cyberjoey | 11:94a7379c0db8 | 1241 | if(device_logged[i]) |
| cyberjoey | 11:94a7379c0db8 | 1242 | { |
| cyberjoey | 11:94a7379c0db8 | 1243 | pc.printf("Device %d is being logged.\r\n", i); |
| cyberjoey | 11:94a7379c0db8 | 1244 | no_devices = false; |
| cyberjoey | 11:94a7379c0db8 | 1245 | } |
| cyberjoey | 11:94a7379c0db8 | 1246 | |
| cyberjoey | 11:94a7379c0db8 | 1247 | } |
| cyberjoey | 11:94a7379c0db8 | 1248 | if(no_devices) |
| cyberjoey | 11:94a7379c0db8 | 1249 | pc.printf("No devices being logged.\r\n", i); |
| cyberjoey | 9:36ba3626aab7 | 1250 | break; |
| cyberjoey | 10:148da21c297e | 1251 | case 'g': |
| cyberjoey | 10:148da21c297e | 1252 | case 'G': |
| cyberjoey | 10:148da21c297e | 1253 | //0 means get config, 1 means get log |
| cyberjoey | 10:148da21c297e | 1254 | if(n == 2) |
| cyberjoey | 10:148da21c297e | 1255 | { |
| cyberjoey | 10:148da21c297e | 1256 | bool fileExists = false; |
| cyberjoey | 10:148da21c297e | 1257 | if(device == 0)//get config |
| cyberjoey | 10:148da21c297e | 1258 | { |
| cyberjoey | 10:148da21c297e | 1259 | fileExists = print_settings_file(); |
| cyberjoey | 10:148da21c297e | 1260 | } |
| cyberjoey | 10:148da21c297e | 1261 | if(device == 1)//get log |
| cyberjoey | 10:148da21c297e | 1262 | { |
| cyberjoey | 10:148da21c297e | 1263 | fileExists = print_log_file(); |
| cyberjoey | 10:148da21c297e | 1264 | } |
| cyberjoey | 10:148da21c297e | 1265 | if(!fileExists) |
| cyberjoey | 10:148da21c297e | 1266 | { |
| cyberjoey | 10:148da21c297e | 1267 | pc.printf("no_file\r\n"); |
| cyberjoey | 10:148da21c297e | 1268 | } |
| cyberjoey | 10:148da21c297e | 1269 | } |
| cyberjoey | 10:148da21c297e | 1270 | break; |
| cyberjoey | 9:36ba3626aab7 | 1271 | case 'i': |
| cyberjoey | 9:36ba3626aab7 | 1272 | case 'I': //Set Logging sample intreval in seconds |
| cyberjoey | 9:36ba3626aab7 | 1273 | |
| cyberjoey | 9:36ba3626aab7 | 1274 | log_interval = device; |
| cyberjoey | 9:36ba3626aab7 | 1275 | if(log_interval < 1)log_interval = 1; |
| cyberjoey | 10:148da21c297e | 1276 | if(log_interval > 60)log_interval = 60; |
| cyberjoey | 10:148da21c297e | 1277 | write_settings_file(log_interval, device_logged); |
| cyberjoey | 9:36ba3626aab7 | 1278 | break; |
| cyberjoey | 9:36ba3626aab7 | 1279 | case 'l': |
| cyberjoey | 9:36ba3626aab7 | 1280 | case 'L': // Toggle logging |
| cyberjoey | 9:36ba3626aab7 | 1281 | |
| cyberjoey | 9:36ba3626aab7 | 1282 | if(log_flag == false){ //start logging |
| cyberjoey | 9:36ba3626aab7 | 1283 | if(SDDetect)//if SD card not detected |
| cyberjoey | 9:36ba3626aab7 | 1284 | { |
| cyberjoey | 9:36ba3626aab7 | 1285 | error_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1286 | error_ticks = 6; |
| cyberjoey | 9:36ba3626aab7 | 1287 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1288 | } |
| cyberjoey | 9:36ba3626aab7 | 1289 | else |
| cyberjoey | 9:36ba3626aab7 | 1290 | { |
| cyberjoey | 10:148da21c297e | 1291 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 1292 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 1293 | { |
| cyberjoey | 9:36ba3626aab7 | 1294 | fprintf(fp, "Time(s)"); |
| cyberjoey | 9:36ba3626aab7 | 1295 | |
| cyberjoey | 9:36ba3626aab7 | 1296 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 1297 | { |
| cyberjoey | 9:36ba3626aab7 | 1298 | fprintf(fp,", Device %d Temperature (C)",j); |
| cyberjoey | 9:36ba3626aab7 | 1299 | } |
| cyberjoey | 9:36ba3626aab7 | 1300 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1301 | |
| cyberjoey | 9:36ba3626aab7 | 1302 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1303 | } |
| cyberjoey | 9:36ba3626aab7 | 1304 | |
| cyberjoey | 9:36ba3626aab7 | 1305 | time_count = 0; |
| cyberjoey | 9:36ba3626aab7 | 1306 | |
| cyberjoey | 9:36ba3626aab7 | 1307 | time_to_sample = 0; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1308 | tick_flag = true; // force sample at time = 0; |
| cyberjoey | 9:36ba3626aab7 | 1309 | log_flag = true; |
| cyberjoey | 9:36ba3626aab7 | 1310 | } |
| cyberjoey | 9:36ba3626aab7 | 1311 | }else{ |
| cyberjoey | 9:36ba3626aab7 | 1312 | //pc.printf("<stop logging>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1313 | log_flag = false; |
| cyberjoey | 9:36ba3626aab7 | 1314 | } |
| cyberjoey | 9:36ba3626aab7 | 1315 | break; |
| cyberjoey | 9:36ba3626aab7 | 1316 | case 'P': |
| cyberjoey | 9:36ba3626aab7 | 1317 | case 'p': // power down One Wire buss |
| cyberjoey | 9:36ba3626aab7 | 1318 | if(n == 2){ |
| cyberjoey | 9:36ba3626aab7 | 1319 | if (device == 0){ // power down mode ON. |
| cyberjoey | 9:36ba3626aab7 | 1320 | data[0] = WDC; // 0xD2 Write Device Config |
| cyberjoey | 9:36ba3626aab7 | 1321 | data[1] = 0xD2; // 1101 0010 set 1WS = 0, SPU = 0, PDN = 1, APU = 0 |
| cyberjoey | 9:36ba3626aab7 | 1322 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 1323 | pc.printf("<Power down DQ>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1324 | }else{ // power down mode OFF |
| cyberjoey | 9:36ba3626aab7 | 1325 | data[0] = WDC; // 0xD2 Write Device Config |
| cyberjoey | 9:36ba3626aab7 | 1326 | data[1] = 0xE1; // 1110 0001 set 1WS = 0, SPU = 0, PDN = 0, APU = 1 |
| cyberjoey | 9:36ba3626aab7 | 1327 | i2c.write(DS2484_ADD,data,2,0); |
| cyberjoey | 9:36ba3626aab7 | 1328 | pc.printf("<Power up DQ>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1329 | } |
| cyberjoey | 9:36ba3626aab7 | 1330 | } |
| cyberjoey | 9:36ba3626aab7 | 1331 | break; |
| cyberjoey | 9:36ba3626aab7 | 1332 | case 'r': |
| cyberjoey | 9:36ba3626aab7 | 1333 | case 'R': //read register "r device radd.start radd.end" |
| cyberjoey | 9:36ba3626aab7 | 1334 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1335 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1336 | } |
| cyberjoey | 9:36ba3626aab7 | 1337 | if(n==3){ //read single register from selected device |
| cyberjoey | 9:36ba3626aab7 | 1338 | read_OW_register(device_id,arg1,1,data); |
| cyberjoey | 9:36ba3626aab7 | 1339 | pc.printf("device[%02X] add[%02X] data[%02X] crc[%02X %02X]\r\n",device,arg1,data[0],data[1],data[2]); |
| cyberjoey | 9:36ba3626aab7 | 1340 | } |
| cyberjoey | 9:36ba3626aab7 | 1341 | if(n==4){ //read a range of regesters from selected device |
| cyberjoey | 9:36ba3626aab7 | 1342 | num_bytes = arg2-arg1 + 1; // calculate number of bytes to read |
| cyberjoey | 9:36ba3626aab7 | 1343 | if (num_bytes < 1) num_bytes = 1; // if arg2 <= arg 1 just read arg1 address. |
| cyberjoey | 9:36ba3626aab7 | 1344 | read_OW_register(device_id,arg1,num_bytes,data); |
| cyberjoey | 9:36ba3626aab7 | 1345 | for(i=0;i<num_bytes;i++){ |
| cyberjoey | 9:36ba3626aab7 | 1346 | pc.printf("\r\ndevice[%02X] add[%02X] data[%02X]",device,arg1+i,data[i]); |
| cyberjoey | 9:36ba3626aab7 | 1347 | } |
| cyberjoey | 9:36ba3626aab7 | 1348 | pc.printf(" crc[%02X %02X]\r\n",data[i],data[i+1]); |
| cyberjoey | 9:36ba3626aab7 | 1349 | } |
| cyberjoey | 9:36ba3626aab7 | 1350 | break; |
| cyberjoey | 9:36ba3626aab7 | 1351 | |
| cyberjoey | 9:36ba3626aab7 | 1352 | case 's': |
| cyberjoey | 9:36ba3626aab7 | 1353 | case 'S': // search rom |
| cyberjoey | 9:36ba3626aab7 | 1354 | // ****************** search for all OW devices on bus ***************** |
| cyberjoey | 9:36ba3626aab7 | 1355 | |
| cyberjoey | 9:36ba3626aab7 | 1356 | device_count = search_rom(rom_id_list); |
| cyberjoey | 9:36ba3626aab7 | 1357 | |
| cyberjoey | 9:36ba3626aab7 | 1358 | // print out rom codes found |
| cyberjoey | 9:36ba3626aab7 | 1359 | //pc.printf("\r\nsearch rom\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1360 | pc.printf("%d devices:\r\n", device_count); |
| cyberjoey | 9:36ba3626aab7 | 1361 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1362 | pc.printf("device[%02X] rom id[",j); |
| cyberjoey | 9:36ba3626aab7 | 1363 | for(i=7;i>=0;i--){ |
| cyberjoey | 9:36ba3626aab7 | 1364 | pc.printf("%02X",rom_id_list[j][i]); |
| cyberjoey | 9:36ba3626aab7 | 1365 | } |
| cyberjoey | 9:36ba3626aab7 | 1366 | pc.printf("]\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1367 | } |
| cyberjoey | 9:36ba3626aab7 | 1368 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1369 | break; |
| cyberjoey | 9:36ba3626aab7 | 1370 | |
| cyberjoey | 9:36ba3626aab7 | 1371 | case 'T': |
| cyberjoey | 9:36ba3626aab7 | 1372 | case 't': |
| cyberjoey | 9:36ba3626aab7 | 1373 | if(n == 2){//get temperatures from selected device |
| cyberjoey | 9:36ba3626aab7 | 1374 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1375 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1376 | } |
| cyberjoey | 9:36ba3626aab7 | 1377 | convert_temperature(device_id); //send OW convert selected device |
| cyberjoey | 9:36ba3626aab7 | 1378 | wait(0.02); //wait 20 ms for convert temperature to complete |
| cyberjoey | 9:36ba3626aab7 | 1379 | T[0] = get_temperature(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1380 | pc.printf("device[%02X] temperature[%.3f]\r\n",device,T[0]); |
| cyberjoey | 9:36ba3626aab7 | 1381 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1382 | } |
| cyberjoey | 9:36ba3626aab7 | 1383 | if(n == 3){ // "t 1 5" get temperature for devices 1 thru 5 |
| cyberjoey | 9:36ba3626aab7 | 1384 | |
| cyberjoey | 9:36ba3626aab7 | 1385 | convert_temperature(); //send OW convert to all devices |
| cyberjoey | 9:36ba3626aab7 | 1386 | wait(0.02); //wait 750 ms for convert temperature to complete |
| cyberjoey | 9:36ba3626aab7 | 1387 | for(j=device;j<=arg1;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1388 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1389 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1390 | } |
| cyberjoey | 9:36ba3626aab7 | 1391 | pc.printf("device[%02X] temperature[%.3f]\r\n",j,get_temperature(device_id)); |
| cyberjoey | 9:36ba3626aab7 | 1392 | } |
| cyberjoey | 9:36ba3626aab7 | 1393 | //pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1394 | } |
| cyberjoey | 9:36ba3626aab7 | 1395 | break; |
| cyberjoey | 9:36ba3626aab7 | 1396 | |
| cyberjoey | 9:36ba3626aab7 | 1397 | case 'w': |
| cyberjoey | 9:36ba3626aab7 | 1398 | case 'W': //write register "w device w.addr data" |
| cyberjoey | 9:36ba3626aab7 | 1399 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1400 | device_id[k] = rom_id_list[device][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1401 | } |
| cyberjoey | 9:36ba3626aab7 | 1402 | data[0] = arg2; |
| cyberjoey | 9:36ba3626aab7 | 1403 | write_OW_register(device_id, arg1, 1, data); |
| cyberjoey | 9:36ba3626aab7 | 1404 | |
| cyberjoey | 9:36ba3626aab7 | 1405 | pc.printf("write -- device[%02X] add[%02X] data[%02X] ",device,arg1,arg2); |
| cyberjoey | 9:36ba3626aab7 | 1406 | |
| cyberjoey | 9:36ba3626aab7 | 1407 | // get CRC |
| cyberjoey | 9:36ba3626aab7 | 1408 | //OW_read_byte(data,2); |
| cyberjoey | 9:36ba3626aab7 | 1409 | pc.printf("crc[%02X %02X]\r\n",data[0],data[1]); |
| cyberjoey | 9:36ba3626aab7 | 1410 | |
| cyberjoey | 9:36ba3626aab7 | 1411 | break; |
| cyberjoey | 9:36ba3626aab7 | 1412 | |
| cyberjoey | 9:36ba3626aab7 | 1413 | case 'x': |
| cyberjoey | 9:36ba3626aab7 | 1414 | case 'X': // experimental modes |
| cyberjoey | 9:36ba3626aab7 | 1415 | // ****************** set up ADC enabled in test mode***************** |
| cyberjoey | 9:36ba3626aab7 | 1416 | pc.printf("<set ADC_ENABLED in test mode>\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1417 | |
| cyberjoey | 9:36ba3626aab7 | 1418 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1419 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1420 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1421 | } |
| cyberjoey | 9:36ba3626aab7 | 1422 | set_test_mode(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1423 | //db.printf("device[%2X] in test mode\r\n",j); |
| cyberjoey | 9:36ba3626aab7 | 1424 | } |
| cyberjoey | 9:36ba3626aab7 | 1425 | break; |
| cyberjoey | 9:36ba3626aab7 | 1426 | |
| cyberjoey | 9:36ba3626aab7 | 1427 | |
| cyberjoey | 9:36ba3626aab7 | 1428 | }//end switch(c) |
| cyberjoey | 9:36ba3626aab7 | 1429 | }//if(n>0) |
| cyberjoey | 9:36ba3626aab7 | 1430 | }//end if(CR) |
| cyberjoey | 9:36ba3626aab7 | 1431 | if(rx_buff[rx_index] == BS){//backspace received, back up buffer pointer |
| cyberjoey | 9:36ba3626aab7 | 1432 | if(rx_index>0)rx_index--;//remove last char from buffer if not at start. |
| cyberjoey | 9:36ba3626aab7 | 1433 | }else rx_index++; |
| cyberjoey | 9:36ba3626aab7 | 1434 | }//end while(pc.redable()) |
| cyberjoey | 9:36ba3626aab7 | 1435 | wait(0.1); // slow down polling |
| switches | 0:60a522ae2e35 | 1436 | |
| cyberjoey | 9:36ba3626aab7 | 1437 | }// end (while tick == false) |
| cyberjoey | 9:36ba3626aab7 | 1438 | tick_flag = false; // set to false for next time |
| cyberjoey | 9:36ba3626aab7 | 1439 | |
| cyberjoey | 9:36ba3626aab7 | 1440 | // only reached when tick_flag = true otherwise stuck in pc.readable() loop |
| cyberjoey | 9:36ba3626aab7 | 1441 | if(log_flag == true){ |
| cyberjoey | 9:36ba3626aab7 | 1442 | //pc.printf("tc{%5d] tts[%5d]\n\r",time_count,time_to_sample); |
| cyberjoey | 9:36ba3626aab7 | 1443 | if(time_count >= time_to_sample){ //take next sample |
| cyberjoey | 9:36ba3626aab7 | 1444 | time_to_sample += log_interval; // calculate time for next sample |
| cyberjoey | 9:36ba3626aab7 | 1445 | |
| cyberjoey | 9:36ba3626aab7 | 1446 | // start conversion |
| cyberjoey | 9:36ba3626aab7 | 1447 | convert_temperature(); |
| cyberjoey | 9:36ba3626aab7 | 1448 | wait(0.02); //wait 20ms for convert to complete |
| cyberjoey | 9:36ba3626aab7 | 1449 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1450 | for(k=0;k<8;k++){ |
| cyberjoey | 9:36ba3626aab7 | 1451 | device_id[k] = rom_id_list[j][k]; // get device_id from rom_id_list |
| cyberjoey | 9:36ba3626aab7 | 1452 | } |
| cyberjoey | 9:36ba3626aab7 | 1453 | T[j] = get_temperature(device_id); |
| cyberjoey | 9:36ba3626aab7 | 1454 | } |
| cyberjoey | 9:36ba3626aab7 | 1455 | /* |
| cyberjoey | 9:36ba3626aab7 | 1456 | // print out time count and temperatures |
| cyberjoey | 9:36ba3626aab7 | 1457 | pc.printf("%5d",time_count); |
| cyberjoey | 9:36ba3626aab7 | 1458 | for(j=0;j<device_count;j++){ |
| cyberjoey | 9:36ba3626aab7 | 1459 | pc.printf(", %7.3f",T[j]); |
| cyberjoey | 9:36ba3626aab7 | 1460 | } |
| cyberjoey | 9:36ba3626aab7 | 1461 | pc.printf("\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1462 | */ |
| cyberjoey | 10:148da21c297e | 1463 | FILE *fp = fopen(log_file, "a"); |
| cyberjoey | 9:36ba3626aab7 | 1464 | if (fp != NULL) |
| cyberjoey | 9:36ba3626aab7 | 1465 | { |
| cyberjoey | 9:36ba3626aab7 | 1466 | //fprintf(fp, "\n"); |
| cyberjoey | 10:148da21c297e | 1467 | fprintf(fp, "%d",time_count); |
| cyberjoey | 9:36ba3626aab7 | 1468 | for(j=0;j<device_count;j++) |
| cyberjoey | 9:36ba3626aab7 | 1469 | { |
| cyberjoey | 11:94a7379c0db8 | 1470 | if(device_logged[j]) |
| cyberjoey | 11:94a7379c0db8 | 1471 | { |
| cyberjoey | 11:94a7379c0db8 | 1472 | fprintf(fp,", %.3f",T[j]); |
| cyberjoey | 11:94a7379c0db8 | 1473 | } |
| cyberjoey | 11:94a7379c0db8 | 1474 | |
| cyberjoey | 9:36ba3626aab7 | 1475 | } |
| cyberjoey | 9:36ba3626aab7 | 1476 | fprintf(fp,"\r\n"); |
| cyberjoey | 9:36ba3626aab7 | 1477 | |
| cyberjoey | 9:36ba3626aab7 | 1478 | fclose(fp); |
| cyberjoey | 9:36ba3626aab7 | 1479 | } |
| cyberjoey | 9:36ba3626aab7 | 1480 | }// end if(time_count >= time_to_sample) |
| cyberjoey | 9:36ba3626aab7 | 1481 | time_count ++; // |
| cyberjoey | 9:36ba3626aab7 | 1482 | |
| cyberjoey | 9:36ba3626aab7 | 1483 | }// end if(log_flag) |
| cyberjoey | 9:36ba3626aab7 | 1484 | |
| cyberjoey | 9:36ba3626aab7 | 1485 | }//end while(1) |
| switches | 0:60a522ae2e35 | 1486 | } |
| switches | 0:60a522ae2e35 | 1487 |
