From dd0ff7eb2985061f6c79968aabd7a0dbe252d908 Mon Sep 17 00:00:00 2001 From: Willem Oldemans Date: Mon, 14 Jun 2021 21:53:53 +0200 Subject: [PATCH] added dynamic display (full buffer) + DAC + ADC --- MilliOhmMeter_FW/platformio.ini | 2 +- MilliOhmMeter_FW/src/board.cpp | 49 +++++++++++++++++- MilliOhmMeter_FW/src/board.h | 19 +++++-- MilliOhmMeter_FW/src/display.cpp | 34 ++++++------- MilliOhmMeter_FW/src/main.cpp | 2 +- MilliOhmMeter_FW/src/measure.cpp | 87 ++++++++++++++++++++++++++++---- MilliOhmMeter_FW/src/measure.h | 8 ++- 7 files changed, 165 insertions(+), 36 deletions(-) diff --git a/MilliOhmMeter_FW/platformio.ini b/MilliOhmMeter_FW/platformio.ini index 9a0cff7..b1afae4 100644 --- a/MilliOhmMeter_FW/platformio.ini +++ b/MilliOhmMeter_FW/platformio.ini @@ -14,4 +14,4 @@ board = heltec_wifi_lora_32_V2 framework = arduino monitor_speed = 115200 lib_ldf_mode = deep+ -lib_deps = +lib_deps = adafruit/Adafruit ADS1X15@^2.2.0 diff --git a/MilliOhmMeter_FW/src/board.cpp b/MilliOhmMeter_FW/src/board.cpp index 0888a09..85884cb 100644 --- a/MilliOhmMeter_FW/src/board.cpp +++ b/MilliOhmMeter_FW/src/board.cpp @@ -1,11 +1,13 @@ #include "board.h" #include "Arduino.h" +#include //include Wire.h library + void VextOn(void) { //Heltec.begin(true,false,true); - pinMode(Vext,OUTPUT); - digitalWrite(Vext, LOW); + pinMode(Vext, OUTPUT); + digitalWrite(Vext, LOW); } void initBoard(void) @@ -15,4 +17,47 @@ void initBoard(void) Serial.print("Init Board: "); VextOn(); Serial.println("OK"); +} + +void scanI2C(void) +{ + byte error, address; //variable for error and I2C address + int nDevices; + + Serial.println("Scanning..."); + + nDevices = 0; + for (address = 1; address < 127; address++) + { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + Wire.beginTransmission(address); + error = Wire.endTransmission(); + + if (error == 0) + { + Serial.print("I2C device found at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.print(address, HEX); + Serial.println(" !"); + nDevices++; + } + else if (error == 4) + { + Serial.print("Unknown error at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.println(address, HEX); + } + } + if (nDevices == 0) + { + Serial.println("No I2C devices found\n"); + } + else + { + Serial.println("done\n"); + } } \ No newline at end of file diff --git a/MilliOhmMeter_FW/src/board.h b/MilliOhmMeter_FW/src/board.h index 8961fac..56be262 100644 --- a/MilliOhmMeter_FW/src/board.h +++ b/MilliOhmMeter_FW/src/board.h @@ -1,12 +1,23 @@ #pragma once void initBoard(void); +void scanI2C(void); #define HAS_DISPLAY #define HAS_SSD1322 -//#define HAS_SSD1306 +#define OLED_CS 14 +#define OLED_DC 17 +#define OLED_RST 23 +#define OLED_MOSI 27 +#define OLED_SCK 5 -#define OLED_CS 14 -#define OLED_DC 17 -#define OLED_RST 4 \ No newline at end of file +#define ADC_SDA 4 +#define ADC_SCL 15 + +#define DAC1_OUT 26 + +#define BUTTON1 21 +#define BUTTON2 12 +#define BUTTON3 13 +#define BUTTON4 16 \ No newline at end of file diff --git a/MilliOhmMeter_FW/src/display.cpp b/MilliOhmMeter_FW/src/display.cpp index c599451..43af5bf 100644 --- a/MilliOhmMeter_FW/src/display.cpp +++ b/MilliOhmMeter_FW/src/display.cpp @@ -12,13 +12,11 @@ c_onScreenButton wifiState("Wifi", 9, LocRight, &getWifiState); std::vector MainScreen; -#ifdef HAS_SSD1322 - #define FONT8 u8g2_font_helvR08_tf #define FONT16 u8g2_font_7x14_tf #define FONT24 u8g2_font_freedoomr25_tn //u8g2_font_logisoso24_tf -U8G2_SSD1322_NHD_256X64_2_4W_HW_SPI display(U8G2_R0, OLED_CS, OLED_DC, OLED_RST); +U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI display(U8G2_R0, OLED_CS, OLED_DC, OLED_RST); void initMainScreenButtons(void) { @@ -117,6 +115,10 @@ void c_onScreenButton::drawButton() //hide button return; } + display.setDrawColor(0); + display.drawBox(_xpos, _ypos, _width, _height); + display.setDrawColor(1); + if (getState()) { @@ -134,7 +136,7 @@ void c_onScreenButton::drawButton() display.setDrawColor(1); } -String showValue(String designator, double value, String unit) +String showValue(String designator, float value, String unit) { String text; text.clear(); @@ -162,11 +164,6 @@ void drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, ui void drawMeasurementValues() { - // display.setFont(FONT16); - // display.drawStr(0, 12, showValue("m:", getMin(), "").c_str()); - // display.drawStr(80, 12, showValue("r:", getRms(), "").c_str()); - // display.drawStr(160, 12, showValue("m:", getMax(), "").c_str()); - drawDashedHLine(0,12,220); drawDashedVLine(40,12,33); display.setFont(FONT8); @@ -175,7 +172,9 @@ void drawMeasurementValues() display.setFont(FONT24); - display.drawStr(60, 45, showValue("", getValue(), "").c_str()); + //display.drawStr(60, 45, String(getValue()).c_str()); + display.setCursor(60,45); + display.printf("%4.2f",getValue()); uint16_t stringwidth = display.getStrWidth(showValue("", getValue(), "").c_str()); display.setFont(u8g2_font_8x13_t_symbols); display.drawUTF8(60+stringwidth+3 , 43, "mΩ"); @@ -213,12 +212,13 @@ void handleDisplay() uint64_t currentmillis = millis(); if (currentmillis - lastDisplayTime > SCREENREFRESH) { - do - { - drawMainScreen(); - } while (display.nextPage()); + display.clearBuffer(); + + // do + // { + drawMainScreen(); + // } while (display.nextPage()); + display.sendBuffer(); lastDisplayTime = millis(); } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/MilliOhmMeter_FW/src/main.cpp b/MilliOhmMeter_FW/src/main.cpp index 8262b46..6929bf2 100644 --- a/MilliOhmMeter_FW/src/main.cpp +++ b/MilliOhmMeter_FW/src/main.cpp @@ -25,5 +25,5 @@ void loop() handleLeds(); handleMeasure(); - Serial.printf("T=%4.2f\n", (double)(micros() - looptime)/1000); + Serial.printf("T=%4.2fms\n", (double)(micros() - looptime)/1000); } \ No newline at end of file diff --git a/MilliOhmMeter_FW/src/measure.cpp b/MilliOhmMeter_FW/src/measure.cpp index 849b5e3..59cb32d 100644 --- a/MilliOhmMeter_FW/src/measure.cpp +++ b/MilliOhmMeter_FW/src/measure.cpp @@ -1,20 +1,86 @@ #include "measure.h" -void initMeasure( void ) -{ +Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ -} -void handleMeasure (void ) -{ +uint64_t lastMeasurement = 0; +uint64_t lastDacWrite = 0; +bool dacDir = false; +int deg = 0; +float ADCvolts = 0; + +void handleADC(void) +{ + uint64_t currentmillis = millis(); + if (currentmillis - lastMeasurement > MEASUREMENTINTERVAL) + { + ADCvolts = ads.computeVolts(ads.readADC_SingleEnded(0)); + + lastMeasurement = currentmillis; + } } -measureMode getMeasureMode( void ) +void handleDAC(void) +{ + uint64_t currentmillis = millis(); + if (currentmillis - lastDacWrite > DACINTERVAL) + { + if (deg >= 254) + { + dacDir = false; + deg = 254; + } + if (deg <= 0) + { + dacDir = true; + deg = 0; + } + + dacWrite(DAC1_OUT, deg); + + if (dacDir) + { + deg += DACSTEP; + } + else + { + deg -= DACSTEP; + } + + + lastDacWrite = currentmillis; + } +} + +void initMeasure(void) +{ + Serial.print("init measurements:ADC="); + if (!ads.begin()) + { + Serial.println("Failed to initialize ADS."); + } + else + { + Serial.println("OK"); + } + lastMeasurement = millis(); + lastDacWrite = millis(); + + pinMode(BUTTON3, INPUT_PULLUP); +} + +void handleMeasure(void) +{ + handleADC(); + handleDAC(); +} + +measureMode getMeasureMode(void) { return measureMode::mA200; } -double getMin( void) +double getMin(void) { return 14.4; } @@ -34,9 +100,10 @@ uint8_t getBar(void) return 68; } -double getValue(void) +float getValue(void) { - return 144.8; + Serial.printf("ADCvolts:%4.2fV\n",ADCvolts); + return ADCvolts; } bool getErrorState(void) @@ -46,7 +113,7 @@ bool getErrorState(void) bool getOkState(void) { - return true; + return digitalRead(BUTTON3); } bool getOpenState(void) diff --git a/MilliOhmMeter_FW/src/measure.h b/MilliOhmMeter_FW/src/measure.h index 4ffeb2c..94b82ec 100644 --- a/MilliOhmMeter_FW/src/measure.h +++ b/MilliOhmMeter_FW/src/measure.h @@ -1,6 +1,12 @@ #pragma once #include "Arduino.h" +#include "board.h" +#include + +#define MEASUREMENTINTERVAL 10 //ms +#define DACINTERVAL 10 //ms +#define DACSTEP 8 //ticks enum measureMode { @@ -23,7 +29,7 @@ double getMin( void); double getMax(void); double getRms(void); uint8_t getBar(void); -double getValue(void); +float getValue(void); bool getErrorState(void); bool getOkState(void);