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:
- 15:065fbeddc305
- Child:
- 16:f164f8912201
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mon.cpp Sun Jun 29 06:58:00 2014 +0000 @@ -0,0 +1,586 @@ +/* + * mbed Application program + * 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: June 29th, 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. + */ + +// 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 + +// 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) + +// EEPROM +#define EEP_TOP 0x0 + +// RAM ------------------------------------------------------------------------------------------- +char linebuf[64]; +int buf_size = sizeof(linebuf); + +// for EEPROM control +uint8_t buf_pointer[PTR_SIZE]; +xEeprom_ptr log_inf; + +struct one_log{ + uint32_t time; + uint16_t vcc; + uint16_t baro; + int16_t b_temp; + uint16_t humi; + int16_t h_temp; + uint16_t lux; +}; // 16 bytes total + +one_log lg; +uint8_t bf[PKT_SIZE]; + +extern float baro; +extern float baro_temp; +extern float cal_vcc; +extern float lux; +extern float humidity; +extern float humidity_temp; +extern float lux; + +// ROM / Constant data --------------------------------------------------------------------------- +static 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"; + +// Function prototypes --------------------------------------------------------------------------- +extern void mon_hw(void); + +//------------------------------------------------------------------------------------------------- +// Control Program +//------------------------------------------------------------------------------------------------- +// Put \r\n +void put_rn ( void ){ PUTC('\r'); PUTC('\n');} + +// Put \r +void put_r ( void ){ PUTC('\r');} + +// Put ", " +void put_lin ( void ){ PRINTF(", ");} + +// Put space n +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; + + while ((c = **str) == ' ') (*str)++; + if (c == '-') { + s = 1; + c = *(++(*str)); + } + if (c == '0') { + c = *(++(*str)); + if (c <= ' ') { *res = 0; return 1; } + if (c == 'x') { + radix = 16; + c = *(++(*str)); + } else { + if (c == 'b') { + radix = 2; + c = *(++(*str)); + } else { + if ((c >= '0')&&(c <= '9')){ radix = 8; + } else { return 0;} + } + } + } else { + if ((c < '1')||(c > '9')){ return 0;} + radix = 10; + } + val = 0; + while (c > ' ') { + if (c >= 'a') c -= 0x20; + c -= '0'; + if (c >= 17) { + c -= 7; + if (c <= 9) return 0; + } + if (c >= radix) return 0; + val = val * radix + c; + c = *(++(*str)); + } + if (s) val = -val; + *res = val; + return 1; +} + +//------------------------------------------------------------------------------------------------- +// Data Logging / Save into EEPROM +//------------------------------------------------------------------------------------------------- +// Read buffer pointer +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++ ){ + addr = (uint8_t *)(EEP_TOP + i); + buf_pointer[i] = at24c1024.read((int)addr); + wait(0.002); + } +} + +// Make one data packet data structure +void dtlog_data_pack(void){ +struct tm t; + + xm41t62.read_rtc_std(&t); + lg.time = mktime(&t); + bf[0] = (uint8_t)(lg.time >> 24); + bf[1] = (uint8_t)(lg.time >> 16); + bf[2] = (uint8_t)(lg.time >> 8); + bf[3] = (uint8_t)(lg.time >> 0); + lg.vcc = (uint16_t)(cal_vcc * 1000); + bf[4] = (uint8_t)(lg.vcc >> 8); + bf[5] = (uint8_t)(lg.vcc >> 0); + lg.baro = (uint16_t)(baro * 10); + bf[6] = (uint8_t)(lg.baro >> 8); + bf[7] = (uint8_t)(lg.baro >> 0); + lg.b_temp = (int16_t)(baro_temp * 10); + bf[8] = (uint8_t)(lg.b_temp >> 8); + bf[9] = (uint8_t)(lg.b_temp >> 0); + lg.humi = (uint16_t)(humidity * 10); + bf[10] = (uint8_t)(lg.humi >> 8); + bf[11] = (uint8_t)(lg.humi >> 0); + lg.h_temp = (int16_t)(humidity_temp * 10); + bf[12] = (uint8_t)(lg.h_temp >> 8); + bf[13] = (uint8_t)(lg.h_temp >> 0); + lg.lux = (uint16_t)lux; + bf[14] = (uint8_t)(lg.lux >> 8); + bf[15] = (uint8_t)(lg.lux >> 0); +} + +// Print one packet as normalized data +void print_one_block_data(void){ +struct tm *t; +time_t seconds; +uint16_t dt0; +int16_t dt1; + + put_rn(); + PUTC( '$' ); + //--- Time + seconds = (time_t)bf[0] * 16777216 + (time_t)bf[1] * 65536 + (time_t)bf[2] * 256 + (time_t)bf[3]; + //PRINTF(",0x%x,",seconds); + 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 = (uint16_t)bf[4] * 256 + (uint16_t)bf[5]; + //PRINTF("0x%x,",dt0); + PRINTF("%01d.%03d,", dt0/1000, dt0%1000); + //--- Pressure + dt0 = (uint16_t)bf[6] * 256 + (uint16_t)bf[7]; + //PRINTF("0x%x,",dt0); + PRINTF("%04d.%01d,", dt0/10, dt0%10 ); + //--- Temp. + dt1 = (int16_t)bf[8] * 256 + (int16_t)bf[9]; + //PRINTF("0x%x,",dt1); + PRINTF("%\+-03d.%01d,", dt1/10, abs(dt1)%10 ); + //--- Humidity + dt0 = (uint16_t)bf[10] * 256 + (uint16_t)bf[11]; + //PRINTF("0x%x,",dt0); + PRINTF("%02d.%01d,", dt0/10, dt0%10 ); + //--- Temp. + dt1 = (int16_t)bf[12] * 256 + (int16_t)bf[13]; + //PRINTF("0x%x,",dt1); + PRINTF("%\+-03d.%01d,", dt1/10, abs(dt1)%10 ); + //--- Lux + dt0 = (uint16_t)bf[14] * 256 + (uint16_t)bf[15]; + //PRINTF("0x%x",dt0); + PRINTF("%05d,*", dt0); +} + +// Read block data +static void dtlog_blk_read(xEeprom_ptr *read_inf){ +uint8_t i; +uint8_t *addr; + + PRINTF( log_head ); + for ( ; ; ){ + /* Read EEPROM and save to data_pack[] */ + for (i = 0; i < PKT_SIZE; i++){ + addr = (uint8_t *)(EEP_TOP + (read_inf->ptr * PTR_SIZE) + i); + bf[i] =at24c1024.read((int)addr); + wait(0.002); + } + print_one_block_data(); + wait(0.001); + if (++read_inf->ptr >= read_inf->max){ + break; + } + if (READABLE() != 0){ + break; + } + } + put_rn(); +} + +// Write one packet +void dtlog_one_write(void){ +xEeprom_ptr write_inf; +uint8_t i; +uint8_t *addr; + + /* Read buffer pointer */ + dtlog_pointer_read(); + write_inf.wr_pointer = READ_POINTER(); + write_inf.status = RD_STATUS(); + if (write_inf.status == BUF_FULL){ return;} + /* Write data_pack[] into EEPROM */ + for (i = 0; i < PKT_SIZE; i++){ + addr = (uint8_t *)(EEP_TOP + (write_inf.wr_pointer * PTR_SIZE) + i); + at24c1024.write((int)addr, bf[i]); + wait(0.008); + } + /* Write buffer pointer */ + if (++write_inf.wr_pointer == BLK_NO){ + write_inf.status = BUF_FULL; + } else { + write_inf.status = 0; + } + WRITE_POINTER(write_inf.wr_pointer); + WR_STATUS(write_inf.status); + for (i = 0; i < PTR_SIZE; i++){ + addr = (uint8_t *)(EEP_TOP + i); + at24c1024.write((int)addr, buf_pointer[i]); + wait(0.008); + } +} + +// Read Data Pointer Information +void dtlog_rd_buf_inf(xEeprom_ptr *log_ptr_inf){ + dtlog_pointer_read(); + log_ptr_inf->wr_pointer = READ_POINTER(); + log_ptr_inf->status = RD_STATUS(); +} + +// Clear all buffer +void dtlog_clear_all_buff(xEeprom_ptr *log_ptr_inf){ +uint8_t i; +uint8_t *addr; + + /* Set initial data */ + WRITE_POINTER(1); + log_ptr_inf->wr_pointer = 1; + WR_STATUS(0); + log_ptr_inf->status = 0; + log_ptr_inf->max = BLK_NO; + log_ptr_inf->ptr = 1; + log_ptr_inf->size = BLK_SIZE; + /* Write buffer pointer */ + for (i = 0; i < PTR_SIZE; i++){ + addr = (uint8_t *)(EEP_TOP + i); + at24c1024.write((int)addr, buf_pointer[i]); + wait(0.008); + } +} + +// Read some block from buffer +void dtlog_block_read(xEeprom_ptr *read_inf){ + if (read_inf->wr_pointer == BUF_HEAD){ // Check pointer + PRINTF(msg_emty); + put_rn(); + return; + } + if (read_inf->ptr >= read_inf->wr_pointer){ + PRINTF("Reach to end!"); + put_rn(); + return; + } + if (read_inf->wr_pointer > (read_inf->ptr + read_inf->size)){ + read_inf->max = read_inf->ptr + read_inf->size; + } else { + read_inf->max = read_inf->wr_pointer; + } + dtlog_blk_read(read_inf); +} + +// EEPROM buffer occupation +uint16_t dtlog_buf_occupation(void){ + return (uint16_t)(((uint32_t)READ_POINTER() * 1000 )/ BLK_NO); +} + +// Read block number +void dtlog_num_of_block(xEeprom_ptr *read_inf){ +uint16_t dt; + + if (read_inf->wr_pointer == BUF_HEAD){ + PRINTF(msg_emty); + put_rn(); + return; + } + dt = read_inf->wr_pointer - 1; + PRINTF("Number of data = %d", dt); + put_rn(); + dt = dtlog_buf_occupation(); + 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(); +} + +// Get key input data +void get_line (char *buff, int len){ +char c; +int idx = 0; + + for (;;) { + c = GETC(); + // Added by Kenji Arai / JH1PJL May 9th, 2010 + if (c == '\r') { + buff[idx++] = c; + break; + } + if ((c == '\b') && idx) { + idx--; + PUTC(c); + PUTC(' '); + PUTC(c); + } + if (((uint8_t)c >= ' ') && (idx < len - 1)) { + buff[idx++] = c; + PUTC(c); + } + } + buff[idx] = 0; + PUTC('\n'); +} + + +// RTC related subroutines +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); + t.tm_sec = (uint8_t)p1; + PRINTF("Sec: %d \r\n",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); +#if 0 +time_t seconds; +char buf[40]; + + seconds = mktime(&t); + // ex.2 + strftime(buf, 40, "%x %X", localtime(&seconds)); + PRINTF("Date: %s\r\n", buf); + // ex.3 + strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); + PRINTF("Date: %s\r\n", buf); + // ex.4 + strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds)); + PRINTF("Date: %s\r\n", buf); +#endif +} + +// Data Logger / Check status and Output data +static void data_logger(char *ptr){ +char c; +unsigned long dt; +char *const Msg = "Data Logger Mode, ?[Help]"; + + PRINTF(Msg); + put_rn(); + /* Get EEPROM resource */ + dtlog_rd_buf_inf(&log_inf); + while (1){ + PRINTF("DL>"); + ptr = linebuf; + get_line(ptr, buf_size); + switch (*ptr++) { + case 'a' : + put_r(); + log_inf.ptr = BUF_HEAD; + log_inf.size = log_inf.wr_pointer; + dtlog_block_read(&log_inf); + log_inf.ptr = BUF_HEAD; + log_inf.size = BLK_SIZE; + 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(&log_inf); + } else { + PRINTF("Canceled"); + } + put_rn(); + break; + case 'd' : // d <pointer> [<count>] - Dump buffer + put_r(); + log_inf.size = BLK_SIZE; + if (xatoi(&ptr, &dt)){ log_inf.ptr = (uint16_t)dt; + } else { log_inf.ptr = BUF_HEAD; } + if (xatoi(&ptr, &dt)){ log_inf.size = (uint8_t)dt; + } else { log_inf.size = BLK_SIZE; } + dtlog_block_read(&log_inf); + break; + case 0x0d : // CR + put_r(); + log_inf.size = BLK_SIZE; + dtlog_block_read(&log_inf); + break; + case 'b' : // Back + put_r(); + log_inf.size = BLK_SIZE; + if (log_inf.ptr > log_inf.size){ + log_inf.ptr -= log_inf.size; + if (log_inf.ptr == 0){ + log_inf.ptr = 1; + --log_inf.size; + } + } else { + log_inf.size = log_inf.ptr - 1; + log_inf.ptr = 1; + } + dtlog_block_read(&log_inf); + break; + case 'n' : + case 's' : // Status + put_r(); + dtlog_num_of_block(&log_inf); + 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; + + BAUD(9600); + put_rn(); + put_rn(); + PRINTF("%s [Help:'?' key]", mon_msg); + put_rn(); + for (;;) { + put_r(); + PUTC('>'); + 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; + } + } +}