PICO I2C FW

Dependencies:   USBDevice

Revision:
13:674c647d12dd
Parent:
12:aa9fff0aec91
Child:
14:480f2398fe6a
--- a/main.cpp	Tue Jul 03 16:41:47 2018 +0000
+++ b/main.cpp	Tue Jul 03 16:48:14 2018 +0000
@@ -90,7 +90,6 @@
 
 
 // Virtual serial port over USB
-//USBSerial pc;
 USBSerial pc(0x0B6A, 0x0042, 0x0001, true);
 
 // I2C setup
@@ -181,12 +180,6 @@
     sd_insert_flag = true;
 }
 
-/*
-void take_data_flag_callback(){ // set data sample time flag
-    
-    take_data_flag = true; 
-}
-*/
 
 // *****************************************************************************
 //            Define CRC-8 Table
@@ -353,7 +346,6 @@
     for(i=0;i<8;i++)rom_id[i] = 0x00;  //clear rom_id    
     
     do{  // loop for each rom search (end when last_device = 1)
-        //db.printf("\r\nsearch next rom [%2d]",rom_count);
                         
         //init variable for each search
         bit_num = 1;
@@ -422,30 +414,21 @@
                         byte_num++;             //also increnent byt num
                         byte_mask = 1;     
                     }
- //                   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]);
                                     
                                 
                 }while(bit_num<65);  //bit nun started at 1 so end after 64 
                 last_discrep = last_zero;
                 if(last_discrep == 0)last_device = 1;
                 //copy rom_id into rom_id_list and calc crc of first 7 bytes
-                crc = 0x00;                 // reset crc8 total to 0                 
-                //db.printf(" -- rom id[ ");
+                crc = 0x00;                 // reset crc8 total to 0
                 for(i=7;i>=0;i--){
-                    rom_id_list[rom_count][i] = rom_id[i] ;
-                    //db.printf("%02X ",rom_id[i]); 
-                }    
-                //db.printf("]\r\n"); 
-                //db.printf("last_device[%2d]\r\n",last_device); 
-                //db.printf("last_zero[%2d]\r\n",last_zero); 
-                //db.printf("last_discrep[%2d]\r\n",last_discrep);                
+                    rom_id_list[rom_count][i] = rom_id[i];
+                }              
                 
                 //clac_crc of rom ID
                 for (i=0;i<7;i++){
                     crc = calc_crc8(crc, rom_id[i]);
                 }
-                //db.printf("crc8 = [%02X]\r\n",crc);
-                
                                                                                               
                 rom_count++;  
             }//if(last_device..)
@@ -559,8 +542,7 @@
                 last_discrep = last_zero;
                 if(last_discrep == 0)last_device = 1;
                 //copy rom_id into rom_id_list and calc crc of first 7 bytes
-                crc = 0x00;                 // reset crc8 total to 0                 
-                //db.printf(" -- rom id[ ");
+                crc = 0x00;                 // reset crc8 total to 0  
                 for(i=7;i>=0;i--){
                     rom_id_list[rom_count][i] = rom_id[i];
                 }                 
@@ -655,10 +637,6 @@
     OW_write_byte(n-1);                   // send length of bytes to read (0 -> 1 byte)
     OW_read_byte(data,n+2);             // read n bytes plus 2 byte CRC
     
-    // debug output
-    //for(i=0;i<n+2;i++) db.printf("read_OW_register - [%2d][%02X]\r\n",i,data[i]);
-    
-    
     // --- todo ----
     // calculate CRC of
     // Command + length + n bytes
@@ -801,13 +779,11 @@
  //   char data[2];  
     
     read_OW_register(device_id,OT07_FIFO_DATA,0x02,t);     // Read temperature from FIFO, 2 bytes 
-    //db.printf("get_temperature -- FIFO[%02X %02X] CRC[%02X %02X]\r\n",t[0],t[1],t[2],t[3]);
     //calculate temperture from data     
     count = (int)(t[0]*256 + t[1]);
     if (count >= 32768)count = count - 65536;     // 2s comp
     T = (double)count*0.005; 
     
-    //db.printf("T = %f count[%5d]\r\n", T,count);
     return T;    
 }// end get_temperature()
 
@@ -943,8 +919,6 @@
 //returns true if settings file exists and is in the proper format
 bool apply_settings_file(bool (&logged_devices)[MAX_DEVICES], int& interval)
 {
-    //int log_interval = 10;
-    //bool devices[MAX_DEVICES];
     
     char * line = NULL;
     int len = 0;
@@ -960,7 +934,6 @@
         {
             logged_devices[i] = false;
         }
-        
 
         // Read contents from file
         while ((getline(&line, &len, fp)) != -1) 
@@ -1003,9 +976,6 @@
     
 }
 
-
-
-
 //******************************************************************************
 //        main()
 //******************************************************************************
@@ -1054,11 +1024,6 @@
     
     
     i2c.frequency(400000);      //set I2C clock to 400kHz     
-       
-    //wait(1);  //wait 1 sec for USB serial port to init.
-    
-    //db.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\ndaplink serial port\r\n");
-    //pc.printf("--- OT07 - Base Logger Firmware uisng DS2484 bridge V0.2 ---\r\nmicro USB serial port\r\n");
     
     rLED = LED_OFF;
     gLED = LED_ON;
@@ -1069,32 +1034,17 @@
     data[0] = 0xE1;
     data[1] = 0xF0;
     i2c.write(DS2484_ADD,data,1,1);
-    i2c.read(DS2484_ADD,data,1); 
-    
-    //db.printf("DS2484 Reset --> status[%02X]\r\n",data[0]);    
+    i2c.read(DS2484_ADD,data,1);   
     
     // ******************  search for all OW devices on bus  *****************
     
     device_count = search_rom(rom_id_list);
-
-    
-    // print out rom codes found
-    /*pc.printf("\r\nsearch rom\r\n");
-    for(j=0;j<device_count;j++){
-        //pc.printf("device[%02X]  rom id[ ",j);
-        for(i=7;i>=0;i--){            
-            pc.printf("%02X",rom_id_list[j][i]); 
-        }    
-        pc.printf("]\r\n");
-    } 
-    pc.printf("\r\n");*/
     
     for(j=0;j<device_count;j++){                                   
         for(k=0;k<8;k++){                   
             device_id[k] = rom_id_list[j][k];   // get device_id from rom_id_list
         }
         set_test_mode(device_id);
-        //db.printf("device[%2X] in test mode\r\n",j);
     } 
     
     
@@ -1105,8 +1055,6 @@
     
     sd.disk_initialize(); //initialize sd card
      
-    //db.printf("start Main loop\r\n");
-    //db.printf("tf[%d]\r\n",tick_flag);
     while(1) {  // start main loop,  take data if logging, check for input, repeat   
     
     if(sd_insert_flag == true)
@@ -1151,7 +1099,6 @@
                 log_flag = true;
             }
         }else{
-            //pc.printf("<stop logging>\r\n");
             log_flag = false;
         }  
         button_flag = false;  
@@ -1166,24 +1113,15 @@
  //test if PC sent some charaters
         while(pc.readable()){  //characters in buffer,  get them                               
             rx_buff[rx_index] = pc.getc();
-            //pc.putc(rx_buff[rx_index]);     //echo character 
-            //pc.printf("<[%02x] %c i[%d]>",rx_buff[i],rx_buff[i],i); //echo charater
             
-                            
             if(rx_buff[rx_index] == CR){
-                //db.printf("\r\n");
-                //pc.printf("\r\n");
                 rx_buff[++rx_index] = 0;
-                //db.printf("%s\r\n",rx_buff);
-                //pc.printf("%s\r\n",rx_buff);
                 rx_index = -1;  // because i++ at end of while give i=0 on next loop 
                 device = 0; 
                 arg1 = 0;
                 arg2 = 0;
                 
                 n = sscanf(rx_buff, " %c %d %x %x", &c, & device, &arg1, &arg2);
-                
-                //db.printf("c[%c] d[%d] a1[%x] a2[%x] n[%d]\r\n",c,device,arg1,arg2,n);  //echo values read in
                //process input
                 if(n > 0){//got input so process it
                     switch(c){
@@ -1364,8 +1302,7 @@
                                             pc.printf("%02X",rom_id_list[j][i]); 
                                         }    
                                         pc.printf("]\r\n");
-                                    } 
-                                    //pc.printf("\r\n");
+                                    }
                             break;
                         
                         case 'T':
@@ -1377,8 +1314,7 @@
                                     convert_temperature(device_id);          //send OW convert selected device
                                     wait(0.02);  //wait 20 ms for convert temperature to complete                         
                                     T[0] = get_temperature(device_id);
-                                    pc.printf("device[%02X]  temperature[%.3f]\r\n",device,T[0]);                                 
-                                    //pc.printf("\r\n");
+                                    pc.printf("device[%02X]  temperature[%.3f]\r\n",device,T[0]);
                                 }
                                 if(n == 3){ // "t 1 5"  get temperature for devices 1 thru 5
                                         
@@ -1389,8 +1325,7 @@
                                             device_id[k] = rom_id_list[j][k];   // get device_id from rom_id_list
                                         } 
                                         pc.printf("device[%02X]  temperature[%.3f]\r\n",j,get_temperature(device_id));
-                                    }                             
-                                    //pc.printf("\r\n");
+                                    }                       
                                 }
                             break;
                             
@@ -1404,8 +1339,7 @@
                                     
                                     pc.printf("write -- device[%02X] add[%02X] data[%02X] ",device,arg1,arg2);                                   
                                     
-                                    // get CRC                                                                       
-                                    //OW_read_byte(data,2);
+                                    // get CRC                
                                     pc.printf("crc[%02X %02X]\r\n",data[0],data[1]);                                   
                                      
                                 break;
@@ -1420,7 +1354,6 @@
                                             device_id[k] = rom_id_list[j][k];   // get device_id from rom_id_list
                                         }
                                         set_test_mode(device_id);
-                                        //db.printf("device[%2X] in test mode\r\n",j);
                                     } 
                             break;
                             
@@ -1438,8 +1371,7 @@
       tick_flag = false;                // set to false for next time
              
       // only reached when tick_flag = true otherwise stuck in pc.readable() loop
-      if(log_flag == true){          
-            //pc.printf("tc{%5d] tts[%5d]\n\r",time_count,time_to_sample);                
+      if(log_flag == true){                       
             if(time_count >= time_to_sample){ //take next sample
                 time_to_sample += log_interval;   // calculate time for next sample
                    
@@ -1452,14 +1384,8 @@
                     }
                     T[j] = get_temperature(device_id);            
                 }
-                /*
-                // print out time count and temperatures
-                pc.printf("%5d",time_count);
-                for(j=0;j<device_count;j++){
-                    pc.printf(", %7.3f",T[j]);
-                }
-                pc.printf("\r\n");
-                */
+
+                //open file for microSD logging
                 FILE *fp = fopen(log_file, "a");
                 if (fp != NULL) 
                 {
@@ -1484,4 +1410,3 @@
       
     }//end while(1)
 }
-