This lib is supposed to be used as a sensor's calibration or control program. This makes Cubic Spline Model from some sample plots(sets of (value, voltage)), and then discretize the model (dividing the range of voltage into some steps) in order to use the calibrated model data without getting the INVERSE function.
Diff: TRP105F_Spline.cpp
- Revision:
- 14:a99bf22b919d
- Parent:
- 13:16fc61b41eff
- Child:
- 15:1d643b831a03
--- a/TRP105F_Spline.cpp Tue Jun 07 16:23:19 2016 +0000 +++ b/TRP105F_Spline.cpp Wed Jun 08 10:43:53 2016 +0000 @@ -21,6 +21,7 @@ // //#define DEBUG //#define DEBUG2 +//#define DEBUG3 #ifdef DEBUG DigitalOut led1(LED1); DigitalOut led2(LED2); @@ -589,7 +590,7 @@ } -#ifdef TARGET_LPC1768 +#ifdef HAS_COM_TO_CONSOLE void TRP105FS::printThresholds() { for(int i = 0; i < _ENUM; i++) @@ -597,6 +598,35 @@ } #endif + +#ifdef HAS_LOCAL_FILE_SYSTEM +void TRP105FS::printOutData( + const char *filename) +{ + FILE *fp; + char *filepath; + int fnnum = 0; + + while (filename[fnnum] != 0) fnnum++; + filepath = (char *)malloc((fnnum + 8) * sizeof(char)); // "/local/" are 7 char and \0 is 1 char. + sprintf(filepath, "/local/%s", filename); + fp = fopen(filepath, "w"); + //fp = fopen("/local/log.txt", "w"); // open file in writing mode + + fprintf(fp, "x, y,(threshold)\n"); + for(int i = 0; i < _ENUM; i++) { + fprintf(fp, "%d,%d,(%d)\n", _Set[i].x, _Set[i].y, _Threshold[i]); + } + fprintf(fp, "\nSample:x, y\n"); + for(int i = 0; i < _Sample_Num; i++) { + fprintf(fp, "%d,%d\n", _Sample_Set[i].x, _Sample_Set[i].y); + } + + free(filepath); + fclose(fp); + +} + void TRP105FS::loadSetting() { FILE *fp; @@ -731,6 +761,74 @@ fclose(fp); free(filepath); } +#endif// HAS_LOCAL_FILE_SYSTEM + + +void TRP105FS::saveSetting_intoSerial( + Serial* com +) +{ + char buffer[3]; + + for(int i = 0; i < _ENUM; i++) { + //_Set.X + buffer[0] = 0xFF & (_Set[i].x >> 8); + buffer[1] = 0xFF & (_Set[i].x); + buffer[2] = 0x2c; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + + //Set Y + buffer[0] = 0xFF & (_Set[i].y >> 8); + buffer[1] = 0xFF & (_Set[i].y); + buffer[2] = 0x2c; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + + //Threshold + buffer[0] = 0xFF & (_Threshold[i] >> 8); + buffer[1] = 0xFF & (_Threshold[i]); + buffer[2] = 0x3b; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + } + + //Sample Num + buffer[0] = 0xFF & (_Sample_Num >> 8); + buffer[1] = 0xFF & (_Sample_Num); + buffer[2] = 0x3b; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + + for(int i = 0; i < _Sample_Num; i++) { + //Sample Set X + buffer[0] = 0xFF & (_Sample_Set[i].x >> 8); + buffer[1] = 0xFF & (_Sample_Set[i].x); + buffer[2] = 0x2c; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + + //Sample Set Y + buffer[0] = 0xFF & (_Sample_Set[i].x >> 8); + buffer[1] = 0xFF & (_Sample_Set[i].x); + buffer[2] = 0x2c; + for(int j = 0; j < 3; j++) com->putc(buffer[j]); + while(1) { + if(com->getc() == 0x06) break; //wait ACK + } + } + com->putc(0x04); //send End of Transmission +} void TRP105FS::loadSetting_fromSerial( Serial* com @@ -738,22 +836,23 @@ { char buffer[3]; + for(int i = 0; i < _ENUM; i++) { - //Set X + //_Set.X for(int j = 0; j < 3; j++) { buffer[j] = com->getc(); } com->putc(0x06); //return ACK _Set[i].x = static_cast<unsigned short>(0xFFFF & (buffer[1] << 8 | buffer[0])); - //Set Y + //_Set/Y for(int j = 0; j < 3; j++) { buffer[j] = com->getc(); } com->putc(0x06); //return ACK _Set[i].y = static_cast<unsigned short>(0xFFFF & (buffer[1] << 8 | buffer[0])); - //Threshold + //_Threshold for(int j = 0; j < 3; j++) { buffer[j] = com->getc(); } @@ -761,7 +860,7 @@ _Threshold[i] = static_cast<unsigned short>(0xFFFF & (buffer[1] << 8 | buffer[0])); } - //Sample Num + //_Sample_Num for(int j = 0; j < 3; j++) { buffer[j] = com->getc(); } @@ -788,33 +887,8 @@ } } -void TRP105FS::printOutData( - const char *filename) -{ - FILE *fp; - char *filepath; - int fnnum = 0; - while (filename[fnnum] != 0) fnnum++; - filepath = (char *)malloc((fnnum + 8) * sizeof(char)); // "/local/" are 7 char and \0 is 1 char. - sprintf(filepath, "/local/%s", filename); - fp = fopen(filepath, "w"); - //fp = fopen("/local/log.txt", "w"); // open file in writing mode - - fprintf(fp, "x, y,(threshold)\n"); - for(int i = 0; i < _ENUM; i++) { - fprintf(fp, "%d,%d,(%d)\n", _Set[i].x, _Set[i].y, _Threshold[i]); - } - fprintf(fp, "\nSample:x, y\n"); - for(int i = 0; i < _Sample_Num; i++) { - fprintf(fp, "%d,%d\n", _Sample_Set[i].x, _Sample_Set[i].y); - } - - free(filepath); - fclose(fp); - -} - +#ifdef HAS_LOCAL_FILE_SYSTEM void TRP105FS::_printOutData(unsigned short *arg, int num, char* name) { FILE *fp; @@ -849,4 +923,5 @@ } fprintf(fp, "\n"); fclose(fp); -} \ No newline at end of file +} +#endif// HAS_LOCAL_FILE_SYSTEM \ No newline at end of file