EMIR - Ekvitermní mikroprocesorová regulace https://code.google.com/p/emir/ https://code.google.com/p/emir/wiki/DesignV3
Dependencies: ConfigFile DS1307 OneWire SDFileSystem USBDeviceLite mbed-rtos mbed
Revision 5:5c8e97742751, committed 2014-05-28
- Comitter:
- alpov
- Date:
- Wed May 28 13:58:06 2014 +0000
- Parent:
- 4:9065e9ccb078
- Child:
- 6:c24057b64a66
- Commit message:
- SD card access, logging, configuration files
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/sd.h Wed May 28 13:58:06 2014 +0000 @@ -0,0 +1,20 @@ +#ifndef _SD_H +#define _SD_H + +extern bool read_config(); +extern bool write_config(); + +extern int get_config(char *key, int def = 0); +extern bool set_config(char *key, int value); + +extern int get_sensor_cfg(uint8_t *romid, char *desc); + +extern bool log_temperature(int sensor_id, int value); +extern bool log_io(int io_id, int value); +extern bool log_event(int event_id, int sub_id, char *value); + +extern Mutex sd_mutex; + +extern void sd_init(); + +#endif
--- a/mbed-rtos.lib Wed May 07 07:03:18 2014 +0000 +++ b/mbed-rtos.lib Wed May 28 13:58:06 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#5dfe422a963d +http://mbed.org/users/mbed_official/code/mbed-rtos/#3761f69dbbb2
--- a/mbed.bld Wed May 07 07:03:18 2014 +0000 +++ b/mbed.bld Wed May 28 13:58:06 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877 \ No newline at end of file
--- a/src/disk.txt Wed May 07 07:03:18 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ - - -void read_config() - -void write_config() - -void log_temperature() - -void log_io() - -void log_event() -
--- a/src/main.cpp Wed May 07 07:03:18 2014 +0000 +++ b/src/main.cpp Wed May 28 13:58:06 2014 +0000 @@ -1,12 +1,9 @@ #include "mbed.h" +#include "mbed_debug.h" #include "rtos.h" -#include "SDFileSystem.h" #include "sensor.h" #include "comm.h" - -Serial pc(MBED_UARTUSB); - -SDFileSystem sd(MBED_SPI0, "sd"); +#include "sd.h" DigitalOut ledR(LED_RED, 1); DigitalOut ledG(LED_GREEN, 1); @@ -27,38 +24,36 @@ int main(void) { - pc.printf("EMIRv2 compiled " __DATE__ " " __TIME__ "\n"); + debug("\n\n\nEMIRv2 compiled " __DATE__ " " __TIME__ "\n"); - pc.printf("Starting threads... "); + debug("Initializing SD... "); + sd_init(); + + debug("Starting threads... "); Thread sensor(sensor_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); Thread comm(comm_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); - pc.printf("done\n"); - - FILE *fp = fopen("/sd/sdtest2.txt", "w"); - if (fp == NULL) { - pc.printf("Could not open file for write\n"); - } else { - pc.printf("Writing... "); - fprintf(fp, "Hello fun SD Card World!\nOK!\n"); - pc.printf("closing... "); - fclose(fp); - } - pc.printf("done\n"); + debug("done\n"); OUT1 = 1; OUT2 = 1; OUT3 = 1; OUT4 = 1; - wait(1.0); + wait(0.5); OUT1 = 0; OUT2 = 0; OUT3 = 0; OUT4 = 0; + int i = 0; while (1) { ledG = !ledG; - Thread::wait(200); + Thread::wait(100); + + if (i++ >= 100) { + write_config(); + i = 0; + } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sd.cpp Wed May 28 13:58:06 2014 +0000 @@ -0,0 +1,134 @@ +#include "mbed.h" +#include "mbed_debug.h" +#include "rtos.h" +#include "rtc.h" +#include "sd.h" +#include "SDFileSystem.h" +#include "ConfigFile.h" + +Mutex sd_mutex; +SDFileSystem sd(MBED_SPI0, "sd"); +ConfigFile config; +ConfigFile sensor_cfg; + +bool read_config() +{ + bool b = true; + sd_mutex.lock(); + if (b && !config.read("/sd/emir2.cfg")) b = false; + if (b && !sensor_cfg.read("/sd/sensors.cfg")) b = false; + sd_mutex.unlock(); + return b; +} + + +bool write_config() +{ + bool b = true; + sd_mutex.lock(); + if (b && !config.write("/sd/emir2.cfg", "# EMIR2 main configuration", ConfigFile::DOS)) b = false; + if (b && !sensor_cfg.write("/sd/sensors.cfg", "# EMIR2 sensor configuration", ConfigFile::DOS)) b = false; + sd_mutex.unlock(); + return b; +} + + +int get_config(char *key, int def) +{ + char buffer[32]; + + if (!config.getValue(key, buffer, sizeof(buffer))) return def; + return atoi(buffer); +} + + +bool set_config(char *key, int value) +{ + char buffer[32]; + + sprintf(buffer, "%d", value); + if (!config.setValue(key, buffer)) return false; + return true; +} + + +int get_sensor_cfg(uint8_t *romid, char *desc) +{ + char romid_str[20]; + char buffer[32]; + char *pch; + int index; + + for (int i = 0; i < 8; i++) sprintf(&romid_str[i*2], "%.2X", romid[i]); + if (!sensor_cfg.getValue(romid_str, buffer, sizeof(buffer))) { + sensor_cfg.setValue(romid_str, "0;Unknown"); + return 0; + } + + pch = strchr(buffer, ';'); + if (pch == NULL) return 0; + + *pch = 0; + index = atoi(buffer); + + pch++; + if (desc != NULL) strcpy(desc, pch); + + return index; +} + + +bool log_temperature(int sensor_id, int value) +{ + char buffer[32]; + sprintf(buffer, "%d", value); + return log_event(101, sensor_id, buffer); +} + + +bool log_io(int io_id, int value) +{ + char buffer[32]; + sprintf(buffer, "%d", value); + return log_event(102, io_id, buffer); +} + + +bool log_event(int event_id, int sub_id, char *value) +{ + sd_mutex.lock(); + FILE *fp = fopen("/sd/emir2.log", "a"); + if (fp == NULL) { + debug("Could not open file for write\n"); + sd_mutex.unlock(); + return false; + } + + time_t m_time = get_rtc(); + struct tm *now; + now = localtime(&m_time); + + fprintf(fp, "%d.%d.%04d %02d:%02d:%02d;%lu;", + now->tm_mday, now->tm_mon+1, now->tm_year+1900, + now->tm_hour, now->tm_min, now->tm_sec, + m_time + ); + fprintf(fp, "%d;%d;%s\r\n", event_id, sub_id, value); + + fclose(fp); + sd_mutex.unlock(); + return true; +} + + +void sd_init() +{ + debug("Reading configs... "); + bool b = read_config(); + if (b) debug("OK\n"); else debug("failed\n"); + + debug("Writing log... "); + log_event(100, 0, "EMIRv2 compiled " __DATE__ " " __TIME__ ", starting"); + debug("done\n"); +} +
--- a/src/sensor.cpp Wed May 07 07:03:18 2014 +0000 +++ b/src/sensor.cpp Wed May 28 13:58:06 2014 +0000 @@ -2,6 +2,7 @@ #include "rtos.h" #include "1wire.h" #include "sensor.h" +#include "sd.h" uint8_t sensor_roms[MAX_SENSORS][8]; int16_t sensor_temps[MAX_SENSORS]; @@ -28,6 +29,9 @@ memcpy(sensor_roms[sensor_count], rom, sizeof(rom)); sensor_temps[sensor_count] = temp; sensor_count++; + + int id = get_sensor_cfg(rom, NULL); + log_temperature(id, temp); } result = ow.Next(rom); }