diff options
-rw-r--r-- | gpslogger/MicroLCD.cpp | 26 | ||||
-rw-r--r-- | obdlogger/datalogger.h | 3 | ||||
-rw-r--r-- | obdlogger/obdlogger.cbp | 1 | ||||
-rw-r--r-- | obdlogger/obdlogger.ino | 148 |
4 files changed, 94 insertions, 84 deletions
diff --git a/gpslogger/MicroLCD.cpp b/gpslogger/MicroLCD.cpp index 19a45fa..da1cdc2 100644 --- a/gpslogger/MicroLCD.cpp +++ b/gpslogger/MicroLCD.cpp @@ -307,7 +307,7 @@ size_t LCD_SSD1306::write(uint8_t c) if (c > 0x20 && c < 0x7f) { c -= 0x21; for (byte i = 0; i < 5; i++) { - byte d = pgm_read_byte_near(&font5x8[c][i]); + byte d = pgm_read_byte(&font5x8[c][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -335,7 +335,7 @@ size_t LCD_SSD1306::write(uint8_t c) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (byte i = 0; i <= 14; i += 2) { - byte d = pgm_read_byte_near(&font8x16_terminal[c][i]); + byte d = pgm_read_byte(&font8x16_terminal[c][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -348,7 +348,7 @@ size_t LCD_SSD1306::write(uint8_t c) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (byte i = 1; i <= 15; i += 2) { - byte d = pgm_read_byte_near(&font8x16_terminal[c][i]); + byte d = pgm_read_byte(&font8x16_terminal[c][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -396,7 +396,7 @@ void LCD_SSD1306::writeDigit(byte n) if (n <= 9) { n += '0' - 0x21; for (byte i = 0; i < 5; i++) { - Wire.write(pgm_read_byte_near(&font5x8[n][i])); + Wire.write(pgm_read_byte(&font5x8[n][i])); } Wire.write(0); } else { @@ -416,7 +416,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (i = 0; i < 16; i ++) { - byte d = pgm_read_byte_near(&digits16x24[n][i * 3]); + byte d = pgm_read_byte(&digits16x24[n][i * 3]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -429,7 +429,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (i = 0; i < 16; i ++) { - byte d = pgm_read_byte_near(&digits16x24[n][i * 3 + 1]); + byte d = pgm_read_byte(&digits16x24[n][i * 3 + 1]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -442,7 +442,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (i = 0; i < 16; i ++) { - byte d = pgm_read_byte_near(&digits16x24[n][i * 3 + 2]); + byte d = pgm_read_byte(&digits16x24[n][i * 3 + 2]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -493,7 +493,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (byte i = 0; i <= 14; i += 2) { - byte d = pgm_read_byte_near(&font8x16_terminal[n][i]); + byte d = pgm_read_byte(&font8x16_terminal[n][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -506,7 +506,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (byte i = 1; i <= 15; i += 2) { - byte d = pgm_read_byte_near(&font8x16_terminal[n][i]); + byte d = pgm_read_byte(&font8x16_terminal[n][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -545,7 +545,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (i = 0; i < 16; i ++) { - byte d = pgm_read_byte_near(&digits16x16[n][i]); + byte d = pgm_read_byte(&digits16x16[n][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -558,7 +558,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (; i < 32; i ++) { - byte d = pgm_read_byte_near(&digits16x16[n][i]); + byte d = pgm_read_byte(&digits16x16[n][i]); Wire.write(d); if (m_flags & FLAG_PIXEL_DOUBLE_H) Wire.write(d); } @@ -593,7 +593,7 @@ void LCD_SSD1306::writeDigit(byte n) Wire.write(0x40); if (n <= 9) { for (byte i = 0; i < 8; i++) { - Wire.write(pgm_read_byte_near(&digits8x8[n][i])); + Wire.write(pgm_read_byte(&digits8x8[n][i])); } } else { for (byte i = 0; i < 8; i++) { @@ -631,7 +631,7 @@ void LCD_SSD1306::draw(const PROGMEM byte* buffer, byte x, byte y, byte width, b Wire.beginTransmission(_i2caddr); Wire.write(0x40); for (byte k = 0; k < width; k++, p++) { - Wire.write(pgm_read_byte_near(p)); + Wire.write(pgm_read_byte(p)); } Wire.endTransmission(); } diff --git a/obdlogger/datalogger.h b/obdlogger/datalogger.h index 1ca3ef9..f02a60d 100644 --- a/obdlogger/datalogger.h +++ b/obdlogger/datalogger.h @@ -1,5 +1,5 @@ // configurations -#define ENABLE_DATA_OUT 1 +#define ENABLE_DATA_OUT 0 #define ENABLE_DATA_LOG 1 typedef enum { @@ -81,6 +81,7 @@ typedef struct { #define PID_GYRO 0xF021 #define PID_MESSAGE 0xFE00 +#define PID_HEART_BEAT 0xFFEE #define MSG_FILE_LIST_BEGIN 0x1 #define MSG_FILE_LIST_END 0x2 diff --git a/obdlogger/obdlogger.cbp b/obdlogger/obdlogger.cbp index b1767af..26cf9bc 100644 --- a/obdlogger/obdlogger.cbp +++ b/obdlogger/obdlogger.cbp @@ -583,6 +583,7 @@ <Add directory="." /> </Compiler> <Unit filename="MicroLCD.cpp" /> + <Unit filename="MicroLCD.h" /> <Unit filename="SSD1306.cpp" /> <Unit filename="datalogger.h" /> <Unit filename="images.h" /> diff --git a/obdlogger/obdlogger.ino b/obdlogger/obdlogger.ino index 7ed73cf..ca5bf5c 100644 --- a/obdlogger/obdlogger.ino +++ b/obdlogger/obdlogger.ino @@ -18,15 +18,15 @@ /************************************** * Choose SD pin here **************************************/ -//#define SD_CS_PIN SS // generic +#define SD_CS_PIN SS // generic //#define SD_CS_PIN 4 // ethernet shield //#define SD_CS_PIN 7 // microduino -#define SD_CS_PIN 10 // SD breakout +//#define SD_CS_PIN 10 // SD breakout /************************************** * Config GPS here **************************************/ -#define USE_GPS +//#define USE_GPS #define GPS_BAUDRATE 38400 /* bps */ //#define GPS_OPEN_BAUDRATE 4800 /* bps */ @@ -42,6 +42,7 @@ LCD_SSD1306 lcd; #define USE_MPU6050 0 #define OBD_MIN_INTERVAL 50 /* ms */ #define GPS_DATA_TIMEOUT 2000 /* ms */ +//#define DEBUG Serial // logger states #define STATE_SD_READY 0x1 @@ -51,20 +52,12 @@ LCD_SSD1306 lcd; #define STATE_ACC_READY 0x10 #define STATE_SLEEPING 0x20 -// additional PIDs (non-OBD) -#define PID_GPS_DATETIME 0xF0 -#define PID_GPS_SPEED 0xF01 -#define PID_GPS_COORDINATE 0xF2 -#define PID_GPS_ALTITUDE 0xF3 -#define PID_ACC 0xF8 -#define PID_GYRO 0xF9 - #ifdef USE_GPS // GPS logging can only be enabled when there is additional hardware serial UART #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) #define GPSUART Serial2 #elif defined(__AVR_ATmega644P__) -#define GPSUART Serial1 +#define GPSUART Serial #endif #ifdef GPSUART @@ -86,6 +79,7 @@ static uint32_t lastFileSize = 0; //static uint32_t lastDataTime; static uint32_t lastGPSDataTime = 0; static uint16_t lastSpeed = -1; +static uint16_t speed = 0; static int startDistance = 0; static uint16_t fileIndex = 0; static uint32_t startTime = 0; @@ -94,6 +88,18 @@ static uint32_t startTime = 0; static byte lastPID = 0; static int lastData; +static byte pollPattern[]= { + PID_RPM, PID_SPEED, PID_ENGINE_LOAD, PID_RPM, PID_SPEED, PID_THROTTLE, PID_RPM, PID_SPEED, PID_ENGINE_LOAD, PID_RPM, PID_SPEED, PID_THROTTLE, +}; + +static byte pollPattern2[] = { + PID_DISTANCE, PID_COOLANT_TEMP, PID_INTAKE_TEMP, PID_AMBIENT_TEMP, PID_FUEL_LEVEL, +}; + +#define PATTERN_NUM sizeof(pollPattern) +#define PATTERN_NUM2 sizeof(pollPattern2) +#define POLL_INTERVAL 10000 /* ms */ + class COBDLogger : public COBD, public CDataLogger { public: @@ -130,6 +136,7 @@ public: uint16_t flags = FLAG_CAR | FLAG_OBD; if (state & STATE_GPS_CONNECTED) flags |= FLAG_GPS; if (state & STATE_ACC_READY) flags |= FLAG_ACC; +#if ENABLE_DATA_LOG uint16_t index = openFile(LOG_TYPE_DEFAULT, flags); lcd.setFont(FONT_SIZE_SMALL); lcd.setCursor(86, 0); @@ -143,6 +150,7 @@ public: lcd.print("NO LOG"); } delay(1000); +#endif #ifndef MEMORY_SAVING showECUCap(); @@ -151,6 +159,7 @@ public: readSensor(PID_DISTANCE, startDistance); +#if ENABLE_DATA_LOG // open file for logging if (!(state & STATE_SD_READY)) { if (checkSD()) { @@ -158,13 +167,15 @@ public: showStates(); } } +#endif - - initScreen(); + initLoggerScreen(); } void loop() { - static byte count = 0; + static byte index = 0; + static byte index2 = 0; + static uint32_t lastTime = 0; #ifdef GPSUART if (millis() - lastGPSDataTime > GPS_DATA_TIMEOUT || gps.satellites() < 3) { @@ -176,52 +187,36 @@ public: } #endif - logOBDData(PID_RPM); - logOBDData(PID_SPEED); + logOBDData(pollPattern[index]); + index = (index + 1) % PATTERN_NUM; -#if USE_MPU6050 - if (state & STATE_ACC_READY) { - processAccelerometer(); - } -#endif - - switch (count++) { - case 0: - case 128: - logOBDData(PID_DISTANCE); - break; - case 32: - logOBDData(PID_COOLANT_TEMP); - break; - case 64: - logOBDData(PID_INTAKE_TEMP); - break; - case 160: - if (isValidPID(PID_AMBIENT_TEMP)) - logOBDData(PID_AMBIENT_TEMP); - break; - case 192: - if (isValidPID(PID_BAROMETRIC)) - logOBDData(PID_BAROMETRIC); - break; - default: - logOBDData(PID_THROTTLE); - } - - if ((count & 1) == 0) { - logOBDData(PID_ENGINE_LOAD); - } else { + if (index == 0) { if (isValidPID(PID_INTAKE_MAP)) logOBDData(PID_INTAKE_MAP); else if (isValidPID(PID_MAF_FLOW)) logOBDData(PID_MAF_FLOW); } - if (errors >= 3) { + if (millis() - lastTime > POLL_INTERVAL) { + byte pid = pollPattern2[index2]; + if (isValidPID(pid)) { + lastTime = millis(); + logOBDData(pid); + index2 = (index2 + 1) % PATTERN_NUM2; + } + } + +#if USE_MPU6050 + if (state & STATE_ACC_READY) { + processAccelerometer(); + } +#endif + + if (errors >= 2) { reconnect(); - count = 0; } } +#if ENABLE_DATA_LOG bool checkSD() { Sd2Card card; @@ -279,18 +274,12 @@ public: state |= STATE_SD_READY; return true; } +#endif private: - void initIdleLoop() + void dataIdleLoop() { if (state & STATE_SLEEPING) return; - // called while initializing - char buf[10]; - unsigned int t = (millis() - startTime) / 1000; - sprintf(buf, "%02u:%02u", t / 60, t % 60); - lcd.setFont(FONT_SIZE_SMALL); - lcd.setCursor(97, 7); - lcd.print(buf); #ifdef GPSUART // detect GPS signal if (GPSUART.available()) @@ -300,6 +289,17 @@ private: state |= STATE_GPS_READY; } #endif + + if (getState() == OBD_CONNECTED) + return; + + // called while initializing + char buf[10]; + unsigned int t = (millis() - startTime) / 1000; + sprintf(buf, "%02u:%02u", t / 60, t % 60); + lcd.setFont(FONT_SIZE_SMALL); + lcd.setCursor(97, 7); + lcd.print(buf); #if USE_MPU6050 if (state & STATE_ACC_READY) { accel_t_gyro_union data; @@ -335,11 +335,6 @@ private: #endif } #ifdef GPSUART - void dataIdleLoop() - { - if (GPSUART.available()) - processGPS(); - } void processGPS() { // process GPS data @@ -419,6 +414,10 @@ private: // send a query to OBD adapter for specified OBD-II pid sendQuery(pid); + + // display data while waiting for OBD response + showSensorData(lastPID, lastData); + // wait for reponse bool hasData; do { @@ -430,9 +429,6 @@ private: return; } - // display data while waiting for OBD response - showSensorData(lastPID, lastData); - // get response from OBD adapter pid = 0; char* data = getResponse(pid, buffer); @@ -453,6 +449,7 @@ private: lastPID = pid; lastData = value; +#if ENABLE_DATA_LOG // flush SD data every 1KB if (dataSize - lastFileSize >= 1024) { flushFile(); @@ -464,6 +461,7 @@ private: lcd.print(buf); lastFileSize = dataSize; } +#endif // if OBD response is very fast, go on processing other data for a while #ifdef OBD_MIN_INTERVAL @@ -472,6 +470,7 @@ private: } #endif } +#ifndef MEMORY_SAVING void showECUCap() { char buffer[24]; @@ -491,9 +490,12 @@ private: lcd.print(buffer); } } +#endif void reconnect() { +#if ENABLE_DATA_LOG closeFile(); +#endif lcd.clear(); lcd.setFont(FONT_SIZE_MEDIUM); lcd.print("Reconnecting"); @@ -593,7 +595,7 @@ private: } } } - virtual void initScreen() + virtual void initLoggerScreen() { lcd.clear(); lcd.backlight(true); @@ -613,6 +615,10 @@ static COBDLogger logger; void setup() { +#ifdef DEBUG + DEBUG.begin(38400); +#endif + lcd.begin(); lcd.backlight(true); lcd.setFont(FONT_SIZE_MEDIUM); @@ -637,9 +643,11 @@ void setup() delay(500); - //lcd.setColor(0x7FF); - lcd.setCursor(0, 2); +#if ENABLE_DATA_LOG logger.checkSD(); +#else + lcd.clear(); +#endif logger.setup(); } |