Feng Hong / Mbed OS Nucleo_rtos_basic
Revision:
7:e0c7e624c5fa
Parent:
6:a9a03663fa23
Child:
8:6105ffbaf237
--- a/main.cpp	Sat Mar 23 05:45:51 2019 +0000
+++ b/main.cpp	Sat Mar 23 07:08:27 2019 +0000
@@ -1,8 +1,16 @@
 #include "mbed.h"
 #include <HX711.h>
 #include <eeprom.h>
+#include "eeprom_cust.h"
 //#include "digitLCD.h"
 #include "SB1602E.h"
+extern void analyzePayload();
+extern void scaleCalibration();
+void init_scale();
+extern HX711 hx711;
+
+unsigned char rx[8], tx[8];
+unsigned char rx_length, tx_length;
 
 #define LCD_1602
 SB1602E lcd( PB_9, PB_8 );  //  SDA, SCL
@@ -15,155 +23,11 @@
 //#define LCD_1621
 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
 
-#define EEPROM_ADDR 0x0   // I2c EEPROM address is 0x00
- 
-#define SDA PB_9            // I2C SDA pin
-#define SCL PB_8           // I2C SCL pin
- 
-#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
-#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+
 
-EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
-struct ScaleCalibrationData {
-  unsigned int calibrationWeight;  // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000. 
-  long offsetValue;       // the value for scale offset
-  float scaleValue;       // the ADC increment for 1g  
-  uint8_t checksum;  
-};
-ScaleCalibrationData customVar;
-
-unsigned int calibration_ADC_value;
-//#define CALIBRATION_VALUE   10    // 10g as the calibration weight
-#define WEIGHT_DIFFERENCE   200   // 10g ADC value minimum difference
-#define CALIBRATION_WEIGHT  2000    // calibration weight
-#define MAXIMUM_CALIBRATION_WEIGHT  5000 
-#define MINIMUM_CALIBRATION_WEIGHT  100 
-
-/* scale */
-HX711 hx711(PB_11, PB_10);
-
-long zero_value;
-long calibration_value;
-unsigned int calibration_times; // must calibration 3 times
-unsigned int calibration_done = 0;
-float scale_value;
+extern EEPROM ep;
 int init_id = 0x537;  // first 8 bit is the address
 
-/* scale */
-
-void scaleCalibration()
-{
-    unsigned char eeprom_data[sizeof(customVar)];
-    unsigned char checksum = 0;
-    printf("Start Calibration.\r\n");
-    calibration_done = 0;
-    if (!calibration_done)
-    {
-        led1 = 1; 
-        led2 = 0;
-        if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT)||(customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT))
-            customVar.calibrationWeight = CALIBRATION_WEIGHT;
-        zero_value = hx711.averageValue(10); // skip first 10 readings
-        zero_value = hx711.averageValue(20);
-        printf("zero_value=%d \r\n", zero_value);
-        calibration_value = 0;
-        scale_value = 0;
-        calibration_times = 0;
-        led2 = 1;
-        while (( calibration_times < 5))
-        {
-        
-            calibration_value = hx711.averageValue(20);
-            if (calibration_value > (zero_value + WEIGHT_DIFFERENCE))
-            {
-                calibration_times++;
-            }
-            else
-                calibration_times = 0; 
-        }
-        printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times);
-        if (calibration_times >=5)
-        {
-            // calibration is OK
-            calibration_times = 0;
-            scale_value = (calibration_value - zero_value) / customVar.calibrationWeight;
-            customVar.offsetValue = zero_value;
-            customVar.scaleValue = scale_value;
-//               EEPROM.put(0x00, customVar);
-            hx711.setOffset(zero_value);
-            hx711.setScale(scale_value);      // this value is obtained by calibrating the scale with known weights; see the README for details
-            memcpy(eeprom_data, &customVar, sizeof(customVar));
-            for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++)  // compiler bug need to -4 here 
-            {
-               checksum += eeprom_data[cnt];
-            }
-            customVar.checksum = checksum;     
-            printf("EEPROM write calibration data: \r\n");
-            printf("calibrationWeight=%d \r\n", customVar.calibrationWeight);
-            printf("offsetValue=%d \r\n", customVar.offsetValue);
-            printf("scaleValue=%f \r\n", customVar.scaleValue);
-            printf("checksum=0x%02x \r\n", customVar.checksum);    
-            ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32              
-            calibration_done = 1;
-            led1 = 1;   
-            printf("Calibration Done\r\n");         
-        }
-    } 
-}
-
-void init_scale()
-{
-    unsigned char eeprom_data;
-    unsigned char checksum = 0;
-    customVar.calibrationWeight = CALIBRATION_WEIGHT;
-
-#if 1
-    printf("sizeof(customVar)=%d \r\n", sizeof(customVar));
-    ep.read((uint32_t)0,(void *)&customVar, sizeof(customVar)); 
-    printf("EEPROM read calibration data: \r\n");
-    printf("calibrationWeight=%d \r\n", customVar.calibrationWeight);
-    printf("offsetValue=%d \r\n", customVar.offsetValue);
-    printf("scaleValue=%f \r\n", customVar.scaleValue);
-    printf("checksum=0x%02x \r\n", customVar.checksum);
-    printf("\r\n calculate checksum: \r\n");
-    for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++)  // compiler bug need to -4 here 
-    {
-        ep.read(cnt, (int8_t&)eeprom_data);
-        printf("0x%02x ", eeprom_data);
-        checksum += eeprom_data;
-        printf("checksum=0x%02x\r\n", checksum);
-    }
-    printf("\r\ncalculated checksum=0x%02x \r\n", checksum);
-    
-    if (checksum == customVar.checksum)
-    {
-        if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT) || (customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT))
-        {
-            customVar.calibrationWeight = CALIBRATION_WEIGHT;
-            scaleCalibration();
-        }
-        if ((customVar.offsetValue < 10000))
-        {
-            customVar.offsetValue = 10000;
-            scaleCalibration();
-        }
-        if ((customVar.scaleValue < 100))
-        {
-            customVar.scaleValue = 100; 
-            scaleCalibration();
-        }
-        //  delay(200);    
-        hx711.setOffset(customVar.offsetValue);
-        hx711.setScale(customVar.scaleValue);       
-    }
-    else
-    {
-        scaleCalibration();
-    }
-#endif  
-}
-
-/*scale end*/
 int a = 0;
 int b = 0;
 
@@ -224,304 +88,6 @@
     }
 }
 
-typedef struct _MyData {
-                         int16_t sdata;
-                         int32_t idata;
-                         float fdata;
-                       } MyData;
- 
-static void myerror(std::string msg)
-{
-  printf("Error %s\n",msg.c_str());
-  exit(1);
-}
- 
-void eeprom_test(void)
-{
- // EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64);  // 24C64 eeprom with sda = p9 and scl = p10
-  uint8_t data[256],data_r[256];
-  int8_t ival;
-  uint16_t s;
-  int16_t sdata,sdata_r;
-  int32_t ldata[1024];
-  int32_t eeprom_size,max_size;
-  uint32_t addr;
-  int32_t idata,idata_r;
-  uint32_t i,j,k,l,t,id;
-  float fdata,fdata_r;
-  MyData md,md_r;
-    
-  eeprom_size = ep.getSize();
-  max_size = MIN(eeprom_size,256);
-  
-  printf("Test EEPROM I2C model %s of %d bytes\r\n",ep.getName(),eeprom_size);
- 
-  // Test sequential read byte (max_size first bytes)
-  for(i = 0;i < max_size;i++) {
-     ep.read(i,ival);
-     data_r[i] = ival;
-     if(ep.getError() != 0)
-       myerror(ep.getErrorMessage());
-  }
-  
-  printf("Test sequential read %d first bytes :\r\n",max_size);
-  for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\r\n");
-  }
-   
-    // Test sequential read byte (max_size last bytes)
-  for(i = 0;i < max_size;i++) {
-        addr = eeprom_size - max_size + i;
-    ep.read(addr,ival);
-    data_r[i] = ival;
-    if(ep.getError() != 0)
-      myerror(ep.getErrorMessage());
-  }
-  
-  printf("\nTest sequential read %d last bytes :\r\n",max_size);
-  for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\r\n");
-  }
-    
-  // Test write byte (max_size first bytes)
-  for(i = 0;i < max_size;i++)
-     data[i] = i;
-  
-  for(i = 0;i < max_size;i++) {
-     ep.write(i,(int8_t)data[i]);
-     if(ep.getError() != 0)
-       myerror(ep.getErrorMessage());
-  }
-  
-  // Test read byte (max_size first bytes)
-  for(i = 0;i < max_size;i++) {
-     ep.read(i,(int8_t&)ival);
-     data_r[i] = (uint8_t)ival;
-     if(ep.getError() != 0)
-       myerror(ep.getErrorMessage());
-  }
-  
-  printf("\nTest write and read %d first bytes :\r\n",max_size);
-  for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\r\n");
-  }
- 
-  // Test current address read byte (max_size first bytes)
-  ep.read((uint32_t)0,(int8_t&)ival); // current address is 0
-  data_r[0] = (uint8_t)ival;
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  
-  for(i = 1;i < max_size;i++) {
-     ep.read((int8_t&)ival);
-     data_r[i] = (uint8_t)ival;
-     if(ep.getError() != 0)
-       myerror(ep.getErrorMessage());
-  }
-  
-  printf("\nTest current address read %d first bytes :\r\n",max_size);
-  for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\n");
-  }
-   
-  // Test sequential read byte (first max_size bytes)
-  ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  
-  printf("\nTest sequential read %d first bytes :\r\n",max_size);
-  for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\r\n");
-  }
-  
-  // Test write short, long, float 
-  sdata = -15202;
-    addr = eeprom_size - 16;
-  ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  
-  idata = 45123;
-    addr = eeprom_size - 12;
-  ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-    
-  fdata = -12.26;
-    addr = eeprom_size - 8;
-  ep.write(addr,(float)fdata); // float write at address eeprom_size - 8
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  
-  // Test read short, long, float
-  printf("\nTest write and read short (%d), long (%d), float (%f) :\r\n",
-           sdata,idata,fdata);  
-  
-  ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  printf("sdata %d\r\n",sdata_r);
-  
-  ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  printf("idata %d\r\n",idata_r);
-  
-  ep.read((uint32_t)(eeprom_size - 8),fdata_r);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  printf("fdata %f\r\n",fdata_r);
-  
-  // Test read and write a structure
-  md.sdata = -15203;
-  md.idata = 45124;
-  md.fdata = -12.27;
- 
-  ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-    
-  printf("\nTest write and read a structure (%d %d %f) :\r\n",md.sdata,md.idata,md.fdata);
-  
-  ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r));
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-  
-  printf("md.sdata %d\r\n",md_r.sdata);
-  printf("md.idata %d\r\n",md_r.idata);
-  printf("md.fdata %f\r\n",md_r.fdata);
-  
-    // Test read and write of an array of the first max_size bytes
-    for(i = 0;i < max_size;i++)
-       data[i] = max_size - i - 1;
-    
-    ep.write((uint32_t)(0),data,(uint32_t)max_size);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-    
-    ep.read((uint32_t)(0),data_r,(uint32_t)max_size);
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-    
-    printf("\nTest write and read an array of the first %d bytes :\r\n",max_size);
-    for(i = 0;i < max_size/16;i++) {
-     for(j = 0;j < 16;j++) {
-        addr = i * 16 + j;
-        printf("%3d ",(uint8_t)data_r[addr]);
-     }
-     printf("\r\n");
-  }
-    printf("\r\n");
- #if 0 
-  // Test write and read an array of int32
-  s = eeprom_size / 4;                // size of eeprom in int32
-  int ldata_size = sizeof(ldata) / 4; // size of data array in int32
-  l = s / ldata_size;                 // loop index
-  
-  // size of read / write in bytes
-  t = eeprom_size;
-  if(t > ldata_size * 4)
-    t = ldata_size * 4;
-  
-  printf("Test write and read an array of %d int32 (write entire memory) :\r\n",t/4);
- 
-  // Write entire eeprom
-    if(l) {
-    for(k = 0;k < l;k++) {
-       for(i = 0;i < ldata_size;i++)
-          ldata[i] = ldata_size * k + i;
-        
-       addr = k * ldata_size * 4;
-       ep.write(addr,(void *)ldata,t);
-       if(ep.getError() != 0)
-         myerror(ep.getErrorMessage());
-    }  
-    
-      printf("Write OK\n");
-    
-    // Read entire eeprom
-      id = 0;
-    for(k = 0;k < l;k++) {
-       addr = k * ldata_size * 4;
-       ep.read(addr,(void *)ldata,t);
-       if(ep.getError() != 0)
-         myerror(ep.getErrorMessage());
-  
-       // format outputs with 8 words rows
-       for(i = 0;i < ldata_size / 8;i++) {
-                id++;
-          printf("%4d ",id);
-          for(j = 0;j < 8;j++) {
-             addr = i * 8 + j;
-             printf("%5d ",ldata[addr]);
-          }
-          printf("\n");
-       }
-    }
-  }
-    else {
-        for(i = 0;i < s;i++)
-       ldata[i] = i;
-        
-    addr = 0;
-    ep.write(addr,(void *)ldata,t);
-    if(ep.getError() != 0)
-      myerror(ep.getErrorMessage());
-        
-        printf("Write OK\n");
-    
-    // Read entire eeprom
-      id = 0;
-    
-    addr = 0;
-    ep.read(addr,(void *)ldata,t);
-    if(ep.getError() != 0)
-      myerror(ep.getErrorMessage());
-  
-    // format outputs with 8 words rows
-    for(i = 0;i < s / 8;i++) {
-             id++;
-       printf("%4d ",id);
-       for(j = 0;j < 8;j++) {
-          addr = i * 8 + j;
-          printf("%5d ",ldata[addr]);
-       }
-       printf("\n");
-    }
-    }
-#endif    
-  // clear eeprom
-  printf("\nClear eeprom\n");
- 
-  ep.clear();
-  if(ep.getError() != 0)
-    myerror(ep.getErrorMessage());
-   
-  printf("End\n");  
-    
-}
-
-
 int main()
 {
     wait(1);
@@ -532,6 +98,7 @@
  //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
 #endif  
     printf("\n\n*** RTOS basic example ***\r\n");
+    analyzePayload();
 #ifdef LCD_1602
     lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
     lcd.printf( 1, "pi = %.6f", 3.14159265 );