Barometer program : Data Logger function includes Barometer & temperature (BMP180), Humidity & temp. (RHT03), Sunshine (Cds), RTC(M41T62) data. : Logging data saves into EEPROM (AT24C1024) using ring buffer function.
Dependencies: AT24C1024 RHT03 TextLCD BMP180 M41T62
Fork of mbed_blinky by
Please see https://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-barometer-with-data-logging/#
Diff: mon.cpp
- Revision:
- 18:b3a27f681171
- Parent:
- 16:f164f8912201
--- a/mon.cpp Thu Jul 03 23:07:13 2014 +0000 +++ b/mon.cpp Sat Aug 08 07:01:19 2020 +0000 @@ -1,44 +1,37 @@ /* * mbed Application program - * Data logging & Monitor program for only LPC1114FN28 + * Data logging & Monitor program for only LPC1114FN28 * - * Copyright (c) 2010-2014 Kenji Arai / JH1PJL - * http://www.page.sannet.ne.jp/kenjia/index.html - * http://mbed.org/users/kenjiArai/ - * Created: May 15th, 2010 - * Spareted: June 25th, 2014 mon() & mon_hw() - * Revised: July 3rd, 2014 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE - * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * Copyright (c) 2010,'14,'20 Kenji Arai / JH1PJL + * http://www7b.biglobe.ne.jp/~kenjia/ + * https://os.mbed.com/users/kenjiArai/ + * Created: May 15th, 2010 + * Spareted: June 25th, 2014 mon() & mon_hw() + * Revised: August 8th, 2020 */ -// Include --------------------------------------------------------------------------------------- +// Include -------------------------------------------------------------------- #include "mbed.h" #include "m41t62_rtc.h" // Own lib. / RTC control #include "AT24C1024.h" // Own lib. / EEPROM control #include "dt_log.h" - -// Object ---------------------------------------------------------------------------------------- -Serial pc(dp16,dp15); -extern I2C xi2c(dp5,dp27); // SDA, SCL -extern M41T62 xm41t62(xi2c); // STmicro RTC(M41T62) -AT24C1024 at24c1024(xi2c); // Atmel 1Mbit EE-PROM +#include "redirect_stdio.h" -// Definition ------------------------------------------------------------------------------------ -#define BAUD(x) pc.baud(x) -#define GETC(x) pc.getc(x) -#define PUTC(x) pc.putc(x) -#define PRINTF(...) pc.printf(__VA_ARGS__) -#define READABLE(x) pc.readable(x) +// Object --------------------------------------------------------------------- +extern I2C xi2c; // SDA, SCL +extern M41T62 xm41t62; // STmicro RTC(M41T62) +AT24C1024 at24c1024(dp5,dp27); // Atmel 1Mbit EE-PROM + +// Definition ----------------------------------------------------------------- +#define GETC(x) getc(x) +#define PUTC(x) putc(x) +#define PRINTF(...) printf(__VA_ARGS__) +#define READABLE(x) readable(x) // EEPROM #define EEP_TOP 0x0 -// RAM ------------------------------------------------------------------------------------------- +// RAM ------------------------------------------------------------------------ char linebuf[64]; int buf_size = sizeof(linebuf); @@ -50,25 +43,25 @@ uint16_t tail; } ring_t; -union _inf{ - uint8_t buf_pointer[PTR_SIZE]; - ring_t log_inf; -}inf; +union _inf { + uint8_t buf_pointer[PTR_SIZE]; + ring_t log_inf; +} inf; typedef struct { - uint32_t time; - uint16_t vcc; - uint16_t baro; - int16_t b_temp; - uint16_t humi; - int16_t h_temp; - uint16_t lux; -}one_log; // 16 bytes total + uint32_t time; + uint16_t vcc; + uint16_t baro; + int16_t b_temp; + uint16_t humi; + int16_t h_temp; + uint16_t lux; +} one_log; // 16 bytes total -union _one{ - uint8_t bf[PKT_SIZE]; - one_log lg; -}one; +union _one { + uint8_t bf[PKT_SIZE]; + one_log lg; +} one; extern float baro; extern float baro_temp; @@ -78,36 +71,54 @@ extern float humidity_temp; extern float lux; -// ROM / Constant data --------------------------------------------------------------------------- -static char *const mon_msg = "Monitor for mbed system, created on "__DATE__""; +// ROM / Constant data -------------------------------------------------------- +static const char *const mon_msg = + "Monitor for mbed system, created on " __DATE__ ""; -// $, 2014/6/29,12:43:16,3.293,1004.5,+29.3,45.8,+29.2,1234,* -char *const log_head = "$,YYYY/MM/DD,HH:MM:SS,Vcc ,Press ,Temp ,Humi,Temp ,Lux ,*"; -char *const msg_emty = "Data empty"; -char *const msg_end = "\r\nreach to end"; +const char *const log_head = +// $, 2014/6/29,12:43:16,3.293,1004.5,+29.3,45.8,+29.2,1234,* + "$,YYYY/MM/DD,HH:MM:SS,Vcc ,Press ,Temp ,Humi,Temp ,Lux ,*"; +const char *const msg_emty = "Data empty"; +const char *const msg_end = "\r\nreach to end"; -// Function prototypes --------------------------------------------------------------------------- -extern void mon_hw(void); +// Function prototypes -------------------------------------------------------- +//extern void mon_hw(void); // ROM& RAM limitation -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Control Program -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Put \r\n -void put_rn ( void ){ PUTC('\r'); PUTC('\n');} +void put_rn ( void ) +{ + PUTC('\r'); + PUTC('\n'); +} // Put \r -void put_r ( void ){ PUTC('\r');} +void put_r ( void ) +{ + PUTC('\r'); +} // Put ", " -void put_lin ( void ){ PRINTF(", ");} +void put_lin ( void ) +{ + PRINTF(", "); +} // Put space n -void put_spc( uint8_t n){ for(;n > 0; n--){ PUTC(' '); }} +void put_spc( uint8_t n) +{ + for(; n > 0; n--) { + PUTC(' '); + } +} // Change string -> integer -int xatoi (char **str, unsigned long *res){ -unsigned long val; -unsigned char c, radix, s = 0; +int xatoi (char **str, unsigned long *res) +{ + unsigned long val; + unsigned char c, radix, s = 0; while ((c = **str) == ' ') (*str)++; if (c == '-') { @@ -116,7 +127,10 @@ } if (c == '0') { c = *(++(*str)); - if (c <= ' ') { *res = 0; return 1; } + if (c <= ' ') { + *res = 0; + return 1; + } if (c == 'x') { radix = 16; c = *(++(*str)); @@ -125,12 +139,17 @@ radix = 2; c = *(++(*str)); } else { - if ((c >= '0')&&(c <= '9')){ radix = 8; - } else { return 0;} + if ((c >= '0')&&(c <= '9')) { + radix = 8; + } else { + return 0; + } } } } else { - if ((c < '1')||(c > '9')){ return 0;} + if ((c < '1')||(c > '9')) { + return 0; + } radix = 10; } val = 0; @@ -150,24 +169,25 @@ return 1; } -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Data Logging / Save into EEPROM -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ /* head = H, tail =T state 1: H=1(RING_TOP),T=1(RING_TOP) -> just after Clear command - state 2: H=1,T=n -> n = 2 to RING_TAIL-1 (not filled yet) + state 2: H=1,T=n -> n = 2 to RING_TAIL-1 (not filled yet) state 3: H=1,T=RING_TAIL -> need to check!!!! (just filled) state 4: H=2,T=1(RING_TOP) -> start ringed state state 5: H=n,T=n-1 -> n = 2 to RING_TAIL-1 (ringed) state 6: H=RING_TAIL,T=RING_TAIL-1 -> need to check!!!!! state 7: H=1(RING_TOP),T=RING_TAIL -> need to check!!!!! - state 8: same as "state 5" + state 8: same as "state 5" -> Need to check state 3,6,7 */ // Make one data packet data structure -void dtlog_data_pack(void){ -struct tm t; +void dtlog_data_pack(void) +{ + struct tm t; xm41t62.read_rtc_std(&t); one.lg.time = mktime(&t); @@ -180,235 +200,256 @@ } // Print one packet as normalized data -void print_one_block_data(void){ -struct tm *t; -time_t seconds; -uint16_t dt0; -int16_t dt1; +void print_one_block_data(void) +{ + struct tm *t; + time_t seconds; + uint16_t dt0; + int16_t dt1; put_rn(); PUTC( '$' ); //--- Time seconds = one.lg.time; t = localtime(&seconds); - PRINTF(",%04d/%02d/%02d,%02d:%02d:%02d,", - t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); - //--- Vcc - dt0 = one.lg.vcc; - PRINTF("%01d.%03d,", dt0/1000, dt0%1000); - //--- Pressure - dt0 = one.lg.baro; - PRINTF("%04d.%01d,", dt0/10, dt0%10 ); - //--- Temp. - dt1 = one.lg.b_temp; - PRINTF("%\+-03d.%01d,", dt1/10, abs(dt1)%10 ); - //--- Humidity - dt0 = one.lg.humi; - PRINTF("%02d.%01d,", dt0/10, dt0%10 ); - //--- Temp. - dt1 = one.lg.h_temp; - PRINTF("%\+-03d.%01d,", dt1/10, abs(dt1)%10 ); - //--- Lux - dt0 = one.lg.lux; - PRINTF("%05d,*", dt0); + PRINTF(",%04d/%02d/%02d,%02d:%02d:%02d,", + t->tm_year + 1900, t->tm_mon + 1, + t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); + //--- Vcc + dt0 = one.lg.vcc; + PRINTF("%01d.%03d,", dt0/1000, dt0%1000); + //--- Pressure + dt0 = one.lg.baro; + PRINTF("%04d.%01d,", dt0/10, dt0%10 ); + //--- Temp. + dt1 = one.lg.b_temp; + PRINTF("%+03d.%01d,", dt1/10, abs(dt1)%10 ); + //--- Humidity + dt0 = one.lg.humi; + PRINTF("%02d.%01d,", dt0/10, dt0%10 ); + //--- Temp. + dt1 = one.lg.h_temp; + PRINTF("%+03d.%01d,", dt1/10, abs(dt1)%10 ); + //--- Lux + dt0 = one.lg.lux; + PRINTF("%05d,*", dt0); } // Read buffer pointer -static void dtlog_pointer_read(void){ -uint8_t i; -uint8_t *addr; +static void dtlog_pointer_read(void) +{ + uint8_t i; + uint8_t *addr; /* Read EEPROM and save to buf_pointer[] */ - for ( i = 0; i < PTR_SIZE; i++ ){ + for ( i = 0; i < PTR_SIZE; i++ ) { addr = (uint8_t *)(EEP_TOP + i); inf.buf_pointer[i] = at24c1024.read((int)addr); - wait(0.002); + ThisThread::sleep_for(2ms); } // PRINTF("head %d, Tail %d\r\n", inf.log_inf.head, inf.log_inf.tail); } // Write one packet -void dtlog_one_write(void){ -uint8_t i; -uint8_t *addr; +void dtlog_one_write(void) +{ + uint8_t i; + uint8_t *addr; // Read EEPROM buffer pointer to RAM - for ( i = 0; i < PTR_SIZE; i++ ){ + for ( i = 0; i < PTR_SIZE; i++ ) { addr = (uint8_t *)(EEP_TOP + i); inf.buf_pointer[i] = at24c1024.read((int)addr); - wait(0.002); + ThisThread::sleep_for(2ms); } - //PRINTF("head %d, Tail %d\r\n", inf.log_inf.head, inf.log_inf.tail); +//PRINTF("head %d, Tail %d\r\n", inf.log_inf.head, inf.log_inf.tail); // Write data_pack[] into EEPROM - for (i = 0; i < PKT_SIZE; i++){ - addr = (uint8_t *)(EEP_TOP + (inf.log_inf.tail * PTR_SIZE) + i); + for (i = 0; i < PKT_SIZE; i++) { + addr = (uint8_t *)(EEP_TOP + (inf.log_inf.tail * PTR_SIZE) + i); at24c1024.write((int)addr, one.bf[i]); - wait(0.008); + ThisThread::sleep_for(8ms); } // Increment buffer pointer in RAM - if (inf.log_inf.head == RING_TOP){ // check state 1,2,3 - if (inf.log_inf.tail == RING_TAIL){ // check state 3 - inf.log_inf.tail = RING_TOP; // set state 4 - inf.log_inf.head = 2; // missing one oldest data - } else { - inf.log_inf.tail++; // set state 2 - } + if (inf.log_inf.head == RING_TOP) { // check state 1,2,3 + if (inf.log_inf.tail == RING_TAIL) { // check state 3 + inf.log_inf.tail = RING_TOP; // set state 4 + inf.log_inf.head = 2; // missing one oldest data + } else { + inf.log_inf.tail++; // set state 2 + } } else { // check state 4,5,6,7 - if (inf.log_inf.head == RING_TAIL){ // check state 6 - inf.log_inf.head = RING_TOP; // set state 7 - inf.log_inf.tail = RING_TAIL; - } else if (inf.log_inf.tail == inf.log_inf.head - 1){ // check state 4,5 - ++inf.log_inf.tail; // continue state 5 - ++inf.log_inf.head; - } + if (inf.log_inf.head == RING_TAIL) { // check state 6 + inf.log_inf.head = RING_TOP; // set state 7 + inf.log_inf.tail = RING_TAIL; + // check state 4,5 + } else if (inf.log_inf.tail == inf.log_inf.head - 1) { + ++inf.log_inf.tail; // continue state 5 + ++inf.log_inf.head; + } } - // Write buffer pointer into EEPROM - for (i = 0; i < PTR_SIZE; i++){ - addr = (uint8_t *)(EEP_TOP + i); + // Write buffer pointer into EEPROM + for (i = 0; i < PTR_SIZE; i++) { + addr = (uint8_t *)(EEP_TOP + i); at24c1024.write((int)addr, inf.buf_pointer[i]); - wait(0.008); - } + ThisThread::sleep_for(8ms); + } } // Read some block from buffer -void dtlog_block_read(int16_t *pt, uint16_t n){ -uint8_t i; -uint8_t *addr; -uint16_t num; +void dtlog_block_read(int16_t *pt, uint16_t n) +{ + uint8_t i; + uint8_t *addr; + uint16_t num; dtlog_pointer_read(); - if (inf.log_inf.tail == inf.log_inf.head){ // Check pointer + if (inf.log_inf.tail == inf.log_inf.head) { // Check pointer PRINTF(msg_emty); put_rn(); return; } - PRINTF("Head:%d, Tail:%d, Start pointer:%d, Number of data:%d\r\n", - inf.log_inf.head, inf.log_inf.tail, *pt, n); + PRINTF("Head:%d, Tail:%d, Start pointer:%d, Number of data:%d\r\n", + inf.log_inf.head, inf.log_inf.tail, *pt, n); PRINTF( log_head ); - for (num = 0; num < n; num++){ + for (num = 0; num < n; num++) { /* Read EEPROM and save to data_pack[] */ - for (i = 0; i < PKT_SIZE; i++){ + for (i = 0; i < PKT_SIZE; i++) { addr = (uint8_t *)(EEP_TOP + (*pt * PTR_SIZE) + i); one.bf[i] =at24c1024.read((int)addr); - wait(0.002); + ThisThread::sleep_for(2ms); } print_one_block_data(); - if (READABLE()){ GETC(); break;} - wait(0.001); - if (inf.log_inf.head == RING_TOP){ // check state 1,2,3 - *pt += 1; - if (*pt >= inf.log_inf.tail){ // check state 2,3 - PRINTF(msg_end); - break; + if (READABLE()) { + GETC(); + break; + } + ThisThread::sleep_for(1ms); + if (inf.log_inf.head == RING_TOP) { // check state 1,2,3 + *pt += 1; + if (*pt >= inf.log_inf.tail) { // check state 2,3 + PRINTF(msg_end); + break; } - } else { // state 4,5,6,7 - if (inf.log_inf.head == RING_TAIL){ // check state 6 - if (inf.log_inf.tail == RING_TAIL -1){ // check state 6 - if (*pt == RING_TAIL){ // same as :pt += 1 - *pt = RING_TOP; - } else { // next read - *pt += 1; - if (*pt >= inf.log_inf.tail){ - PRINTF(msg_end); - break; - } - } - } - } else if (inf.log_inf.tail == inf.log_inf.head - 1){ // check state 5 - *pt += 1; - if (*pt > RING_TAIL){ // same as :pt += 1 - *pt = RING_TOP; - } else if (*pt == inf.log_inf.tail){ // reach to end - PRINTF(msg_end); - break; - } - } - } + } else { // state 4,5,6,7 + if (inf.log_inf.head == RING_TAIL) { // check state 6 + if (inf.log_inf.tail == RING_TAIL -1) { // check state 6 + if (*pt == RING_TAIL) { // same as :pt += 1 + *pt = RING_TOP; + } else { // next read + *pt += 1; + if (*pt >= inf.log_inf.tail) { + PRINTF(msg_end); + break; + } + } + } + // check state 5 + } else if (inf.log_inf.tail == inf.log_inf.head - 1) { + *pt += 1; + if (*pt > RING_TAIL) { // same as :pt += 1 + *pt = RING_TOP; + } else if (*pt == inf.log_inf.tail) { // reach to end + PRINTF(msg_end); + break; + } + } + } } put_rn(); } // Clear all buffer -void dtlog_clear_all_buff(void){ -uint8_t i; -uint8_t *addr; +void dtlog_clear_all_buff(void) +{ + uint8_t i; + uint8_t *addr; /* Set initial data */ - inf.log_inf.head = inf.log_inf.tail = RING_TOP; + inf.log_inf.head = inf.log_inf.tail = RING_TOP; /* Write buffer pointer */ - for (i = 0; i < PTR_SIZE; i++){ + for (i = 0; i < PTR_SIZE; i++) { addr = (uint8_t *)(EEP_TOP + i); at24c1024.write((int)addr, inf.buf_pointer[i]); - wait(0.008); + ThisThread::sleep_for(8ms); } } // EEPROM buffer occupation -uint16_t dtlog_buf_occupation(void){ -uint16_t i = 0; -uint16_t dt = 0; -uint8_t *addr; +uint16_t dtlog_buf_occupation(void) +{ + uint16_t i = 0; + uint16_t dt = 0; + uint8_t *addr; // Read EEPROM buffer pointer to RAM - for ( i = 0; i < PTR_SIZE; i++ ){ + for ( i = 0; i < PTR_SIZE; i++ ) { addr = (uint8_t *)(EEP_TOP + i); inf.buf_pointer[i] = at24c1024.read((int)addr); - wait(0.002); + ThisThread::sleep_for(2ms); } // check buffer pointer - if (inf.log_inf.head == inf.log_inf.tail){ + if (inf.log_inf.head == inf.log_inf.tail) { PRINTF(msg_emty); put_rn(); return 0; } - if (inf.log_inf.head == RING_TOP){ // check state 1,2,3 - dt = inf.log_inf.tail - inf.log_inf.head; + if (inf.log_inf.head == RING_TOP) { // check state 1,2,3 + dt = inf.log_inf.tail - inf.log_inf.head; } else { // state 4,5,6,7 - if (inf.log_inf.head == RING_TAIL){ // check state 6 - if (inf.log_inf.tail == RING_TAIL - 1){ // check state 6 - dt = inf.log_inf.tail - RING_TOP + 1; - } - } else if (inf.log_inf.tail == inf.log_inf.head - 1){ // check state 4,5 - dt = RING_TAIL; - } else { // error - dt = 0; - } + if (inf.log_inf.head == RING_TAIL) { // check state 6 + if (inf.log_inf.tail == RING_TAIL - 1) { // check state 6 + dt = inf.log_inf.tail - RING_TOP + 1; + } + // check state 4,5 + } else if (inf.log_inf.tail == inf.log_inf.head - 1) { + dt = RING_TAIL; + } else { // error + dt = 0; + } } return dt; } // Read block number -void dtlog_num_of_block(void){ -uint16_t dt; +void dtlog_num_of_block(void) +{ + uint16_t dt; - dt = dtlog_buf_occupation(); - if (dt == 0){ - PRINTF(msg_emty); + dt = dtlog_buf_occupation(); + if (dt == 0) { + PRINTF(msg_emty); } else { - PRINTF("Number of data = %d", dt); - put_rn(); - dt = (uint16_t)(((uint32_t)dt * 1000 )/ (BLK_NO - 2)); - PRINTF("EEPROM Occupation = %d.%01d%%", dt / 10, dt % 10); + PRINTF("Number of data = %d", dt); + put_rn(); + dt = (uint16_t)(((uint32_t)dt * 1000 )/ (BLK_NO - 2)); + PRINTF("EEPROM Occupation = %d.%01d%%", dt / 10, dt % 10); } put_rn(); } -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Monitor -//------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------ // Help Massage -void msg_hlp (void){ - PRINTF(mon_msg); put_rn(); - PRINTF("d - Data logger"); put_rn(); - PRINTF("t - Check and set RTC"); put_rn(); - PRINTF("x - Goto HW monitor"); put_rn(); - PRINTF("q - Return to main"); put_rn(); +void msg_hlp (void) +{ + PRINTF(mon_msg); + put_rn(); + PRINTF("d - Data logger"); + put_rn(); + PRINTF("t - Check and set RTC"); + put_rn(); + PRINTF("x - Goto HW monitor"); + put_rn(); + PRINTF("q - Return to main"); + put_rn(); } // Get key input data -void get_line (char *buff, int len){ -char c; -int idx = 0; +void get_line (char *buff, int len) +{ + char c; + int idx = 0; for (;;) { c = GETC(); @@ -434,40 +475,42 @@ // RTC related subroutines -void chk_and_set_time(char *ptr){ -unsigned long p1; -struct tm t; +void chk_and_set_time(char *ptr) +{ + unsigned long p1; + struct tm t; - if (xatoi(&ptr, &p1)){ - t.tm_year = (uint8_t)p1 + 100; - PRINTF("Year:%d ",p1); - xatoi( &ptr, &p1 ); - t.tm_mon = (uint8_t)p1 - 1; - PRINTF("Month:%d ",p1); - xatoi( &ptr, &p1 ); - t.tm_mday = (uint8_t)p1; - PRINTF("Day:%d ",p1); - xatoi( &ptr, &p1 ); - t.tm_hour = (uint8_t)p1; - PRINTF("Hour:%d ",p1); - xatoi( &ptr, &p1 ); - t.tm_min = (uint8_t)p1; - PRINTF("Min:%d ",p1); - xatoi( &ptr, &p1 ); - t.tm_sec = (uint8_t)p1; - PRINTF("Sec: %d \r\n",p1); - xm41t62.write_rtc_std(&t); - } + if (xatoi(&ptr, &p1)) { + t.tm_year = (uint8_t)p1 + 100; + PRINTF("Year:%d ",(int)p1); + xatoi( &ptr, &p1 ); + t.tm_mon = (uint8_t)p1 - 1; + PRINTF("Month:%d ",(int)p1); + xatoi( &ptr, &p1 ); + t.tm_mday = (uint8_t)p1; + PRINTF("Day:%d ",(int)p1); + xatoi( &ptr, &p1 ); + t.tm_hour = (uint8_t)p1; + PRINTF("Hour:%d ",(int)p1); + xatoi( &ptr, &p1 ); + t.tm_min = (uint8_t)p1; + PRINTF("Min:%d ",(int)p1); + xatoi( &ptr, &p1 ); + t.tm_sec = (uint8_t)p1; + PRINTF("Sec: %d \r\n",(int)p1); + xm41t62.write_rtc_std(&t); + } xm41t62.read_rtc_std(&t); // Show Time with several example // ex.1 PRINTF("Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", - t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); + t.tm_year + 1900, t.tm_mon + 1, + t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); #if 0 -time_t seconds; -char buf[40]; - - seconds = mktime(&t); + time_t seconds; + char buf[40]; + + seconds = mktime(&t); // ex.2 strftime(buf, 40, "%x %X", localtime(&seconds)); PRINTF("Date: %s\r\n", buf); @@ -481,95 +524,111 @@ } // Data Logger / Check status and Output data -static void data_logger(char *ptr){ -char c; -unsigned long dt; -uint16_t n; -char *const Msg = "Data Logger Mode, ?[Help]"; +static void data_logger(char *ptr) +{ + char c; + unsigned long dt; + uint16_t n; + const char * Msg = "Data Logger Mode, ?[Help]"; - PRINTF(Msg); - put_rn(); - /* Get EEPROM resource */ - dtlog_pointer_read(); - dt = inf.log_inf.head; - while (1){ - /* Get EEPROM resource */ - dtlog_pointer_read(); - PRINTF("DL>"); + PRINTF("%s", Msg); + put_rn(); + /* Get EEPROM resource */ + dtlog_pointer_read(); + dt = inf.log_inf.head; + while (1) { + /* Get EEPROM resource */ + dtlog_pointer_read(); + PRINTF("DL>"); ptr = linebuf; get_line(ptr, buf_size); - switch (*ptr++) { - case 'a' : - put_r(); - read_pointer = inf.log_inf.head; - n = dtlog_buf_occupation(); - dtlog_block_read(&read_pointer, n); - break; - case 'c' : // Clear data - put_r(); - PRINTF("Delete all data?"); - put_rn(); - PRINTF("Enter y/n (n-cancel)"); - put_rn(); - c = GETC(); - PUTC(c); - put_rn(); - if (c == 'y'){ - PRINTF("Cleared all logging data"); - dtlog_clear_all_buff(); - } else { - PRINTF("Canceled"); - } - put_rn(); - break; - case 'd' : // d <pointer> [<count>] - Dump buffer - put_r(); - if (xatoi(&ptr, &dt)){ read_pointer = (uint16_t)dt; - } else { read_pointer = inf.log_inf.head; } - if (xatoi(&ptr, &dt)){ n = (uint8_t)dt; - } else { n = BLK_SIZE; } - if (read_pointer == 0){ read_pointer = 1;} - dtlog_block_read(&read_pointer, n); - break; - case 0x0d : // CR - put_r(); - dtlog_block_read(&read_pointer, BLK_SIZE); - break; - case 'b' : // Back - put_r(); - read_pointer -= (BLK_SIZE * 2); - if (read_pointer <= 0){ read_pointer = 1;} - dtlog_block_read(&read_pointer, n); - break; - case 'n' : - case 's' : // Status - put_r(); - dtlog_num_of_block(); - break; - case 'q' : // exit - linebuf[0] = 0; - return; - case '?' : - put_r(); - PRINTF("d - <pointer> [<count>] Dump one block data"); put_rn(); - PRINTF("a - Dump all log data"); put_rn(); - PRINTF("c - Clear log data"); put_rn(); - PRINTF("s - Logger status"); put_rn(); - PRINTF("q - Exit DL mode"); put_rn(); - break; - default: - PUTC('?'); - put_rn(); - break; - } - } + switch (*ptr++) { + case 'a' : + put_r(); + read_pointer = inf.log_inf.head; + n = dtlog_buf_occupation(); + dtlog_block_read(&read_pointer, n); + break; + case 'c' : // Clear data + put_r(); + PRINTF("Delete all data?"); + put_rn(); + PRINTF("Enter y/n (n-cancel)"); + put_rn(); + c = GETC(); + PUTC(c); + put_rn(); + if (c == 'y') { + PRINTF("Cleared all logging data"); + dtlog_clear_all_buff(); + } else { + PRINTF("Canceled"); + } + put_rn(); + break; + case 'd' : // d <pointer> [<count>] - Dump buffer + put_r(); + if (xatoi(&ptr, &dt)) { + read_pointer = (uint16_t)dt; + } else { + read_pointer = inf.log_inf.head; + } + if (xatoi(&ptr, &dt)) { + n = (uint8_t)dt; + } else { + n = BLK_SIZE; + } + if (read_pointer == 0) { + read_pointer = 1; + } + dtlog_block_read(&read_pointer, n); + break; + case 0x0d : // CR + put_r(); + dtlog_block_read(&read_pointer, BLK_SIZE); + break; + case 'b' : // Back + put_r(); + read_pointer -= (BLK_SIZE * 2); + if (read_pointer <= 0) { + read_pointer = 1; + } + dtlog_block_read(&read_pointer, n); + break; + case 'n' : + case 's' : // Status + put_r(); + dtlog_num_of_block(); + break; + case 'q' : // exit + linebuf[0] = 0; + return; + case '?' : + put_r(); + PRINTF("d - <pointer> [<count>] Dump one block data"); + put_rn(); + PRINTF("a - Dump all log data"); + put_rn(); + PRINTF("c - Clear log data"); + put_rn(); + PRINTF("s - Logger status"); + put_rn(); + PRINTF("q - Exit DL mode"); + put_rn(); + break; + default: + PUTC('?'); + put_rn(); + break; + } + } } -// ---------- Program starts here! --------------------------------------------------------------- -int mon(void) { -char *ptr; +// ---------- Program starts here! --------------------------------------------- +int mon(void) +{ + char *ptr; - BAUD(9600); put_rn(); put_rn(); PRINTF("%s [Help:'?' key]", mon_msg); @@ -580,41 +639,43 @@ ptr = linebuf; get_line(ptr, sizeof(linebuf)); switch (*ptr++) { - //--------------------------------------------------------------------------------- - // check and set RTC - //--------------------------------------------------------------------------------- - case 't' : - put_r(); - chk_and_set_time(ptr); - break; - //--------------------------------------------------------------------------------- - // check EEPROM status - //--------------------------------------------------------------------------------- - case 'd' : - put_r(); - data_logger(ptr); - break; - //--------------------------------------------------------------------------------- - // help - //--------------------------------------------------------------------------------- - case '?' : - put_r(); - msg_hlp(); - break; - //--------------------------------------------------------------------------------- - // Go to special command - //--------------------------------------------------------------------------------- - case 'x' : // - mon_hw(); - PRINTF("->Came back monitor\r\n"); - break; - //--------------------------------------------------------------------------------- - // Go back to main() - //--------------------------------------------------------------------------------- - case 'q' : // Quit - PRINTF("\rReturn to main\r\n"); - PRINTF("cannot control anymore from here\r\n"); - return 0; + //------------------------------------------------------------------ + // check and set RTC + //------------------------------------------------------------------ + case 't' : + put_r(); + chk_and_set_time(ptr); + break; + //------------------------------------------------------------------ + // check EEPROM status + //------------------------------------------------------------------ + case 'd' : + put_r(); + data_logger(ptr); + break; + //------------------------------------------------------------------ + // help + //------------------------------------------------------------------ + case '?' : + put_r(); + msg_hlp(); + break; + //------------------------------------------------------------------ + // Go to special command + //------------------------------------------------------------------ + case 'x' : // + PRINTF("->ROM & RAM size limitation. "); + PRINTF("-> Cannot impliment the function\r\n"); + //mon_hw(); + PRINTF("->Came back monitor\r\n"); + break; + //------------------------------------------------------------------ + // Go back to main() + //------------------------------------------------------------------ + case 'q' : // Quit + PRINTF("\rReturn to main\r\n"); + PRINTF("cannot control anymore from here\r\n"); + return 0; } } }