From 7b6035179821fd7a7553ac3b60f36e84fc1cccd4 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sat, 12 Jun 2021 00:42:19 +0200 Subject: Import inital sketch lpo pour dumper les DTC et des valeurs live --- lpo/MyLCD.h | 51 ++++++++++++++++++++++++++++++++++ lpo/MyOBD.h | 32 +++++++++++++++++++++ lpo/lpo.ino | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 lpo/MyLCD.h create mode 100644 lpo/MyOBD.h create mode 100644 lpo/lpo.ino (limited to 'lpo') diff --git a/lpo/MyLCD.h b/lpo/MyLCD.h new file mode 100644 index 0000000..565e110 --- /dev/null +++ b/lpo/MyLCD.h @@ -0,0 +1,51 @@ +#ifndef __MYLCD_H +#define __MYLCD_H +#undef PROGMEM +#define PROGMEM +#include +class MyLCD : public LCD_ILI9325D { + /* 2.8" ILI9325 based LCD 320x240x16bpp*/ + public: + uint8_t column, line; + MyLCD() { + LCD_ILI9325D::setFontSize(FONT_SIZE_SMALL); + this->clear(); + } + byte getLines() { return 30; } + byte getCols() { return 53; } + void setCursor(uint8_t column, uint8_t line) { + /* column and line as in a terminal, start at line 1, col 1. each column is one character wide*/ + this->column = (column==0)?1:column; + this->line = (column==0)?1:line; + LCD_ILI9325D::setCursor((column-1)*6, line-1); + } + void clear() { + LCD_ILI9325D::clear(); + this->setCursor(1, 1); + } + size_t println() { + if ( this->line < this->getLines() ) { + this->setCursor(1, this->line+1); + } else { + this->clear(); + } + } + void moveRight(size_t len) { + if ( this->column+len <= this->getCols() ) { + this->setCursor(this->column+len, this->line); + } else { + this->println(); + } + } + size_t println(const String &s) { + size_t len = LCD_ILI9325D::print(s); + this->println(); + return len; + } + size_t print(const String &s) { + size_t len = LCD_ILI9325D::print(s); + this->moveRight(len); + return len; + } +}; +#endif diff --git a/lpo/MyOBD.h b/lpo/MyOBD.h new file mode 100644 index 0000000..ea143be --- /dev/null +++ b/lpo/MyOBD.h @@ -0,0 +1,32 @@ +#ifndef __MYOBD_H +#define __MYOBD_H +#include +class MyOBD : public COBDI2C { + public: + /* Fake OBD for testing + int i; + MyOBD() { + i=0; + } + byte begin() { + delay(500); + return 1; + } + bool init() { + delay(500); + return ++i>4; + } + bool readPID(byte pid, int& result) { + result=millis(); + if ( result%1000 == 0 ) { this->errors++; } + return true; + } + byte readDTC(uint16_t codes[], byte maxCodes = 1) { + codes[0]=6002; + codes[1]=6032; + codes[2]=12; + return 4; + } + */ +}; +#endif diff --git a/lpo/lpo.ino b/lpo/lpo.ino new file mode 100644 index 0000000..85a595b --- /dev/null +++ b/lpo/lpo.ino @@ -0,0 +1,92 @@ +/************************************************************************* +* Inpired from simple_obd_display.ino by Stanley Huang +* (Simple OBD Data Display for Freematics OBD-II Adapter) +*************************************************************************/ +#include "MyLCD.h" +#include "MyOBD.h" + +MyLCD lcd; +MyOBD obd; + +void setup() +{ + lcd.begin(); + lcd.print("obd.begin()"); + obd.begin(); + lcd.println(" done"); + reconnect(); + showDTC(); + initScreen(); +} + +void reconnect() +{ + lcd.setCursor(1,2); + lcd.print("obd.init() "); + lcd.setCursor(11,2); + while (!obd.init()) { + lcd.print("."); + } + lcd.println(" done"); +} + +void showDTC() { + static char buf[10]; + static uint16_t codes[16]; + byte c, count = obd.readDTC(codes, 16); + lcd.print("DTC: "); + if ( count==0 ) { + lcd.println("none"); + } else { + for (c=0; c prev_err) { + showData(0, obd.errors); + } + if ( obd.errors > 10 ) { + obd.errors=0; + reconnect(); + } +} + +void initScreen() +{ + lcd.setCursor(1,4); + lcd.println("comm. errors: "); + lcd.println(" rpm: "); + lcd.println(" speed: km/h"); + lcd.println(" throt: %"); + lcd.println(" load: "); + showData(0, obd.errors); +} + +void showData(byte pid, int value) +{ + static char buf[10]; + int res = sprintf(buf, "%6d", value); + switch (pid) { + case 0: lcd.setCursor(15,4); lcd.print(buf); break; + case PID_RPM: lcd.setCursor(15,5); lcd.print(buf); break; + case PID_SPEED: lcd.setCursor(15,6); lcd.print(buf); break; + case PID_THROTTLE: lcd.setCursor(15,7); lcd.print(buf); break; + case PID_ENGINE_LOAD: lcd.setCursor(15,8); lcd.print(buf); break; + } +} -- cgit v1.2.3