new hardware

This commit is contained in:
2022-03-21 08:48:30 +01:00
parent c6dbbd4a02
commit eb976bcb6c
169 changed files with 172343 additions and 129089 deletions

View File

@@ -3,61 +3,10 @@
#include <Wire.h> //include Wire.h library
void VextOn(void)
{
//Heltec.begin(true,false,true);
pinMode(Vext, OUTPUT);
digitalWrite(Vext, LOW);
}
void initBoard(void)
{
Serial.begin(115200);
Serial.flush();
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");
}
delay(2000);
log_i("Init serial: OK");
}

View File

@@ -6,19 +6,19 @@ void scanI2C(void);
#define HAS_DISPLAY
#define HAS_SSD1322
#define OLED_CS 14
#define OLED_DC 17
#define OLED_RST 23
#define OLED_MOSI 27
#define OLED_SCK 5
#define OLED_CS gpio_num_t(26)
#define OLED_DC gpio_num_t(27)
#define OLED_RST gpio_num_t(25)
#define OLED_MOSI gpio_num_t(14)
#define OLED_SCK gpio_num_t(13)
#define ADC_SDA 4
#define ADC_SCL 15
#define DAC1_OUT 26
#define MEAS_CHANNEL 0
#define BUTTON1 18
#define BUTTON2 15
#define BUTTON3 32
#define BUTTON4 33
#define BUTTON5 13
#define BUTTON1 21
#define BUTTON2 32
#define BUTTON3 35
#define BUTTON4 34
#define BUTTON5 39

View File

@@ -2,11 +2,11 @@
std::vector<c_button *> buttonlist;
c_button button1(BUTTON1, 1);
c_button button2(BUTTON2, 2);
c_button button3(BUTTON3, 3);
c_button button4(BUTTON4, 4);
c_button button5(BUTTON5, 5);
// c_button button1(BUTTON1, 1);
// c_button button2(BUTTON2, 2);
// c_button button3(BUTTON3, 3);
// c_button button4(BUTTON4, 4);
// c_button button5(BUTTON5, 5);
void buttonbegin(c_button *thisbutton)
{
@@ -16,13 +16,13 @@ void buttonbegin(c_button *thisbutton)
void initButtons(void)
{
Serial.print("Init buttons:");
buttonbegin(&button1);
buttonbegin(&button2);
buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
Serial.println("OK");
log_i("Init buttons:");
// buttonbegin(&button1);
// buttonbegin(&button2);
// buttonbegin(&button3);
// buttonbegin(&button4);
// buttonbegin(&button5);
log_i("OK");
}
void handleButtons(void)

View File

@@ -1,134 +1,41 @@
#include "display.h"
#include "u8g2_esp32_hal.h"
u8g2_t display;
u8g2_esp32_hal_t u8g2_esp32_hal;
uint64_t lastDisplayTime = 0;
U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI display(U8G2_R0, OLED_CS, OLED_DC, OLED_RST);
U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI* getDisplay(void)
u8g2_t* getDisplay(void)
{
return &display;
}
void c_onScreenButton::begin(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, uint16_t radius)
{
Serial.printf("%s", _name.c_str());
_xpos = xpos;
_width = width;
_ypos = ypos;
_radius = radius;
_height = height;
if (_location == LocBottom)
{
Serial.print(":Calc_yTpos");
_yTpos = display.getHeight() - 1 - (CONTROLSLINE_H / 2) + (display.getMaxCharHeight() / 2);
Serial.print(":Calc_xTpos");
_xTpos = _xpos + (_width / 2) - (display.getStrWidth(_name.c_str()) / 2);
}
if (_location == LocRight)
{
Serial.print(":Calc_yTpos");
_yTpos = _ypos + display.getMaxCharHeight() - 2;
Serial.print(":Calc_xTpos");
_xTpos = _xpos + (_width / 2) - (display.getStrWidth(_name.c_str()) / 2);
}
Serial.print(":OK | ");
}
void c_onScreenButton::drawButton()
{
display.setFont(FONT8);
//Serial.printf("drawbutton:%s(x=%u, y=%u, w=%u, h=%u)\n", _name.c_str(), _xpos, _ypos, _width, _height);
if (!_visible)
{
//hide button
return;
}
display.setDrawColor(0);
display.drawBox(_xpos, _ypos, _width, _height);
display.setDrawColor(1);
if (getState())
{
display.drawRBox(_xpos, _ypos, _width, _height, _radius);
display.setDrawColor(0);
display.drawStr(_xTpos, _yTpos, _name.c_str());
}
else
{
display.setDrawColor(1);
display.drawRFrame(_xpos, _ypos, _width, _height, _radius);
display.drawStr(_xTpos, _yTpos, _name.c_str());
}
display.setDrawColor(1);
}
String showValue(String designator, float value, String unit)
{
String text;
text.clear();
if (designator != "")
{
text = designator;
}
text += value;
text += unit;
return text;
}
void drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)
{
uint16_t radius = height / 2;
uint16_t doubleRadius = radius << 1;
display.setColorIndex(1);
display.drawRFrame(x, y, width, height, radius);
uint16_t maxProgressWidth = (width - doubleRadius + 1) * progress / 100;
display.drawRBox(x + 1, y + 2, maxProgressWidth, height - 3, radius);
}
void drawDashedHLine(uint16_t x, uint16_t y, uint16_t len)
{
for(int i = 0; i < len; i+=2)
{
display.drawPixel(x+i, y);
}
}
void drawDashedVLine(uint16_t x, uint16_t y, uint16_t len)
{
for(int i = 0; i < len; i+=2)
{
display.drawPixel(x, y+i);
}
}
uint16_t getDisplayWidth(void)
{
return display.getWidth();
}
uint16_t getDisplayHeight(void)
{
return display.getHeight();
}
uint64_t lastDisplayTime = 0;
void initDisplay()
{
display.setFont(FONT8);
display.begin();
display.clearBuffer();
log_i("init display");
u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.clk = OLED_SCK;
u8g2_esp32_hal.mosi = OLED_MOSI;
u8g2_esp32_hal.cs = OLED_CS;
u8g2_esp32_hal.dc = OLED_DC;
u8g2_esp32_hal.reset = OLED_RST;
u8g2_esp32_hal_init(u8g2_esp32_hal);
//u8g2_Setup_ssd1322_nhd_256x64_f(&display, U8G2_R2, u8g2_esp32_spi_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8g2_Setup_ssd1322_nhd_256x64_1(&display, U8G2_R2, u8x8_byte_arduino_4wire_sw_spi, u8x8_gpio_and_delay_arduino);
u8x8_SetPin_4Wire_SW_SPI(u8g2_GetU8x8(&display), OLED_SCK, OLED_MOSI, OLED_CS, OLED_DC, OLED_RST);
u8g2_SetFont(&display, FONT8);
u8g2_InitDisplay(&display);
clearDisplay();
lastDisplayTime = millis();
log_i("init display: OK");
}
void handleDisplay()
@@ -136,9 +43,7 @@ void handleDisplay()
uint64_t currentmillis = millis();
if (currentmillis - lastDisplayTime > SCREENREFRESH)
{
display.sendBuffer();
u8g2_SendBuffer(&display);
lastDisplayTime = millis();
}
@@ -146,5 +51,15 @@ void handleDisplay()
void clearDisplay(void)
{
display.clearBuffer();
u8g2_ClearBuffer(&display);
}
uint16_t getDisplayWidth(void)
{
return u8g2_GetDisplayWidth(&display);
}
uint16_t getDisplayHeight(void)
{
return u8g2_GetDisplayWidth(&display);
}

View File

@@ -2,25 +2,15 @@
#include "board.h"
#include <U8g2lib.h>
#include <U8x8lib.h>
#include "U8g2lib.h"
#include "measure.h"
#include "connect.h"
#include "image.h"
#define CONTROLSTRIP_YPOS 40
#define CONTROLSLINE_H 14
#define CONTROLSLINE_W 64
#define SCREENWIDTH 256
#define CONTROLLOFFSET 0
#define CONTROLRADIUS 6
#define INDICATORWIDTH 29
#define INDICATORHEIGHT 13
#define INDICATORRADIUS 3
#define SCREENREFRESH 20
#define SCREENWIDTH 256
#define FONT8 u8g2_font_helvR08_tf
#define FONT16 u8g2_font_7x14_tf
@@ -28,73 +18,6 @@
typedef enum
{
LocTop,
LocRight,
LocBottom,
LocLeft,
locNone
}e_buttonLoc;
class c_onScreenButton
{
uint16_t _xpos;
uint16_t _ypos;
uint16_t _xTpos;
uint16_t _yTpos;
uint16_t _radius;
uint16_t _width;
uint16_t _height;
const String _name;
const uint8_t _index;
e_buttonLoc _location;
bool _state;
bool _visible;
bool (*const _stateFn)() = NULL;
public:
c_onScreenButton(String name, uint8_t index, e_buttonLoc location) : _xpos(1),
_width(1),
_name(name),
_index(index),
_location(location),
_stateFn(NULL)
{
_visible = false;
}
c_onScreenButton(String name, uint8_t index, e_buttonLoc location, bool (*stateFn)()) : _xpos(1),
_width(1),
_name(name),
_index(index),
_location(location),
_stateFn(stateFn)
{
_visible = false;
}
void drawButton();
void begin(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, uint16_t radius);
void setState(bool state) { _state = state; }
bool getState(void)
{
if (_stateFn != NULL)
{
//Serial.printf("%s: call stateFn\n",_name.c_str());
return _stateFn();
}
return _state;
}
void setVisible(bool state) { _visible = state; }
bool getVisible(void) { return _visible; }
uint8_t getIndex(void) { return _index; }
e_buttonLoc getLocation(void) { return _location; }
};
void initDisplay(void);
void handleDisplay(void);
@@ -105,6 +28,6 @@ String showValue(String designator, float value, String unit);
uint16_t getDisplayWidth(void);
uint16_t getDisplayHeight(void);
U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI *getDisplay(void);
u8g2_t* getDisplay(void);
void drawDashedVLine(uint16_t x, uint16_t y, uint16_t len);
void drawDashedHLine(uint16_t x, uint16_t y, uint16_t len);

View File

@@ -0,0 +1,104 @@
#include "draw.h"
void c_onScreenButton::begin(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, uint16_t radius)
{
log_d("%s", _name.c_str());
_xpos = xpos;
_width = width;
_ypos = ypos;
_radius = radius;
_height = height;
if (_location == LocBottom)
{
log_d(":Calc_yTpos");
_yTpos = getDisplayHeight() - 1 - (CONTROLSLINE_H / 2) + ( u8g2_GetMaxCharHeight(getDisplay()) / 2);
log_d(":Calc_xTpos");
_xTpos = _xpos + (_width / 2) - (u8g2_GetStrWidth(getDisplay(), _name.c_str()) / 2);
}
if (_location == LocRight)
{
log_d(":Calc_yTpos");
_yTpos = _ypos + getDisplayHeight() - 2;
log_d(":Calc_xTpos");
_xTpos = _xpos + (_width / 2) - (u8g2_GetStrWidth(getDisplay(), _name.c_str()) / 2);
}
log_d(":OK | ");
}
void c_onScreenButton::drawButton()
{
u8g2_SetFont(getDisplay(), FONT8);
log_d("drawbutton:%s(x=%u, y=%u, w=%u, h=%u)\n", _name.c_str(), _xpos, _ypos, _width, _height);
if (!_visible)
{
//hide button
return;
}
u8g2_SetDrawColor(getDisplay(),0);
u8g2_DrawBox(getDisplay(), _xpos, _ypos, _width, _height);
u8g2_SetDrawColor(getDisplay(),1);
if (getState())
{
u8g2_DrawRBox(getDisplay(), _xpos, _ypos, _width, _height, _radius);
u8g2_SetDrawColor(getDisplay(),0);
u8g2_DrawStr(getDisplay(), _xTpos, _yTpos, _name.c_str());
}
else
{
u8g2_SetDrawColor(getDisplay(),1);
u8g2_DrawRFrame(getDisplay(), _xpos, _ypos, _width, _height, _radius);
u8g2_DrawStr(getDisplay(), _xTpos, _yTpos, _name.c_str());
}
u8g2_SetDrawColor(getDisplay(),1);
}
String showValue(String designator, float value, String unit)
{
String text;
text.clear();
if (designator != "")
{
text = designator;
}
text += value;
text += unit;
return text;
}
void drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)
{
uint16_t radius = height / 2;
uint16_t doubleRadius = radius << 1;
u8g2_SetDrawColor(getDisplay(), 1);
u8g2_DrawRFrame(getDisplay(), x, y, width, height, radius);
uint16_t maxProgressWidth = (width - doubleRadius + 1) * progress / 100;
u8g2_DrawRBox(getDisplay(), x + 1, y + 2, maxProgressWidth, height - 3, radius);
}
void drawDashedHLine(uint16_t x, uint16_t y, uint16_t len)
{
for(int i = 0; i < len; i+=2)
{
u8g2_DrawPixel(getDisplay(), x+i, y);
}
}
void drawDashedVLine(uint16_t x, uint16_t y, uint16_t len)
{
for(int i = 0; i < len; i+=2)
{
u8g2_DrawPixel(getDisplay(), x, y+i);
}
}

View File

@@ -0,0 +1,98 @@
#pragma once
#include "board.h"
#include "U8g2lib.h"
#include "display.h"
#include "buttons.h"
// Display layout defines
#define CONTROLSTRIP_YPOS 40
#define CONTROLSLINE_H 14
#define CONTROLSLINE_W 64
#define CONTROLLOFFSET 0
#define CONTROLRADIUS 6
#define INDICATORWIDTH 29
#define INDICATORHEIGHT 13
#define INDICATORRADIUS 3
typedef enum
{
LocTop,
LocRight,
LocBottom,
LocLeft,
locNone
}e_buttonLoc;
class c_onScreenButton
{
uint16_t _xpos;
uint16_t _ypos;
uint16_t _xTpos;
uint16_t _yTpos;
uint16_t _radius;
uint16_t _width;
uint16_t _height;
const String _name;
const uint8_t _index;
e_buttonLoc _location;
bool _state;
bool _visible;
bool (*const _stateFn)() = NULL;
c_button _physButton;
public:
c_onScreenButton(String name, uint8_t index, e_buttonLoc location, uint8_t pin) :
_xpos(1),
_width(1),
_name(name),
_index(index),
_location(location),
_stateFn(NULL),
_physButton(pin, index)
{
_visible = false;
}
c_onScreenButton(String name, uint8_t index, e_buttonLoc location, bool (*stateFn)()) :
_xpos(1),
_width(1),
_name(name),
_index(index),
_location(location),
_stateFn(stateFn),
_physButton(-1, index)
{
_visible = false;
}
void drawButton();
void begin(uint16_t xpos, uint16_t ypos, uint16_t width, uint16_t height, uint16_t radius);
void setState(bool state) { _state = state; }
bool getState(void)
{
if (_stateFn != NULL)
{
//log_if("%s: call stateFn\n",_name.c_str());
return _stateFn();
}
return _state;
}
void handle()
{
if( _physButton.getPin() != -1)
{
_physButton.read();
if(_physButton.changed())
{
log_i("button %d = %d",_physButton.getIndex(), _physButton.read());
}
}
}
void setVisible(bool state) { _visible = state; }
bool getVisible(void) { return _visible; }
uint8_t getIndex(void) { return _index; }
e_buttonLoc getLocation(void) { return _location; }
};

View File

@@ -1,10 +1,10 @@
#include "gui.h"
c_onScreenButton ma20("20m", (uint8_t)mA20, LocBottom);
c_onScreenButton ma200("200m", (uint8_t)mA200, LocBottom);
c_onScreenButton ma1000("1A", (uint8_t)mA1000, LocBottom);
c_onScreenButton mauto("Auto", (uint8_t)mAuto, LocBottom);
c_onScreenButton bsetup("Conf", 5, LocBottom);
c_onScreenButton ma20("20m", (uint8_t)mA20, LocBottom, BUTTON1);
c_onScreenButton ma200("200m", (uint8_t)mA200, LocBottom, BUTTON2);
c_onScreenButton ma1000("1A", (uint8_t)mA1000, LocBottom, BUTTON3);
c_onScreenButton mauto("Auto", (uint8_t)mAuto, LocBottom, BUTTON4);
c_onScreenButton bsetup("Conf", 5, LocBottom, BUTTON5);
c_onScreenButton errorState("ER", 6, LocRight, &getErrorState);
c_onScreenButton okState("OK", 7, LocRight, &getOkState);
c_onScreenButton openState("Open", 8, LocRight, &getOpenState);
@@ -16,7 +16,7 @@ displayState CurrentGuiState;
void initGui(void)
{
Serial.print("Init GUI: ");
log_i("Init GUI: ");
uint16_t screenwidth = getDisplayWidth() - 1;
uint16_t buttonwidth = ((screenwidth / mLast) - CONTROLLOFFSET * 2) - 1;
uint16_t currentWidth = 0;
@@ -39,7 +39,7 @@ void initGui(void)
wifiState.begin(IndicatorXpos, currentYpos += (INDICATORHEIGHT - 1), INDICATORWIDTH, INDICATORHEIGHT, INDICATORRADIUS);
//fill vector
Serial.print("Store");
log_i("Store");
MainScreen.push_back(ma20);
MainScreen.push_back(ma200);
MainScreen.push_back(ma1000);
@@ -58,7 +58,7 @@ void initGui(void)
CurrentGuiState = mainscreen;
Serial.println("OK");
log_i("OK");
}
void drawMainSceenButtons()
@@ -80,17 +80,17 @@ void drawMainscreenValues()
drawDashedHLine(0, 12, 220);
drawDashedVLine(40, 12, 33);
getDisplay()->setFont(FONT8);
getDisplay()->drawStr(5, 8, "Fs:100Hz LP");
u8g2_SetFont(getDisplay(),FONT8);
u8g2_DrawStr(getDisplay(), 5, 8, "Fs:100Hz LP");
getDisplay()->setFont(FONT24);
u8g2_SetFont(getDisplay(), FONT24);
//display.drawStr(60, 45, String(getValue()).c_str());
getDisplay()->setCursor(60, 45);
getDisplay()->printf("%4.2f", getValue());
uint16_t stringwidth = getDisplay()->getStrWidth(showValue("", getValue(), "").c_str());
getDisplay()->setFont(u8g2_font_8x13_t_symbols);
getDisplay()->drawUTF8(60 + stringwidth + 3, 43, "mΩ");
u8g2_DrawStr(getDisplay(), 60, 45, String(getValue()).c_str());
//u8g2_DrawStr(getDisplay(), 60, 45);
uint16_t stringwidth = u8g2_GetStrWidth(getDisplay(), showValue("", getValue(), "").c_str());
u8g2_SetFont(getDisplay(), u8g2_font_8x13_t_symbols);
u8g2_DrawUTF8(getDisplay(), 60 + stringwidth + 3, 43, "mΩ");
//drawProgressBar(0, 40, 127, 5, getBar());
}
@@ -100,6 +100,14 @@ void drawMainScreen()
drawMainscreenValues();
}
void handleGUIButtons(void)
{
for (auto &&i : MainScreen)
{
i.handle();
}
}
void handleGui(void)
{
clearDisplay();

View File

@@ -1,6 +1,7 @@
#pragma once
#include "Arduino.h"
#include "draw.h"
#include "display.h"
#include "measure.h"
@@ -12,5 +13,6 @@ typedef enum
void initGui(void);
void handleGui(void);
void handleGUIButtons(void);
displayState getDisplayState(void);

View File

@@ -1,24 +0,0 @@
#include "leds.h"
uint64_t lastLedTimer = 0;
bool ledBlinkState = false;
void initLeds(void)
{
Serial.print("Init Leds: ");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, ledBlinkState );
lastLedTimer = millis();
Serial.println("OK");
}
void handleLeds(void)
{
uint64_t currentTime = millis();
if(currentTime - lastLedTimer > LEDINTERVAL)
{
digitalWrite(LED_BUILTIN, ledBlinkState);
ledBlinkState = !ledBlinkState;
lastLedTimer = currentTime;
}
}

View File

@@ -1,10 +0,0 @@
#pragma once
#include "Arduino.h"
#include "board.h"
#define LEDINTERVAL 1000
void initLeds(void);
void handleLeds(void);

View File

@@ -1,7 +1,6 @@
#include <Arduino.h>
#include "board.h"
#include "display.h"
#include "leds.h"
#include "measure.h"
#include "buttons.h"
#include "gui.h"
@@ -14,7 +13,6 @@ void setup()
initBoard();
initButtons();
initDisplay();
initLeds();
initMeasure();
initGui();
@@ -25,10 +23,10 @@ void loop()
{
// put your main code here, to run repeatedly:
looptime = micros();
handleLeds();
handleMeasure();
handleButtons();
handleGui();
handleGUIButtons();
handleDisplay(); //make sure to update the display last (writes buffer to the screen)
Serial.printf("T=%4.2fms\n", (double)(micros() - looptime)/1000);
log_i("T=%4.2fms\n", (double)(micros() - looptime)/1000);
}

View File

@@ -1,11 +1,12 @@
#include "measure.h"
Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
Adafruit_ADS1115 ads; /* Use this for the 12-bit version */
uint64_t lastMeasurement = 0;
uint64_t lastDacWrite = 0;
bool dacDir = false;
int deg = 0;
TwoWire wire();
float ADCvolts = 0;
@@ -14,44 +15,39 @@ void handleADC(void)
uint64_t currentmillis = millis();
if (currentmillis - lastMeasurement > MEASUREMENTINTERVAL)
{
ADCvolts = ads.computeVolts(ads.readADC_SingleEnded(0));
ADCvolts = ads.computeVolts(ads.readADC_SingleEnded(MEAS_CHANNEL));
lastMeasurement = currentmillis;
}
}
void handleDAC(void)
void initMeasure(void)
{
uint64_t currentmillis = millis();
if (currentmillis - lastDacWrite > DACINTERVAL)
log_i("init ADC");
if(!Wire.setPins(ADC_SDA, ADC_SCL))
{
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;
log_e("FAILED to set i2c pins");
return;
}
if (!ads.begin())
{
log_e("Failed to initialize ADS.");
return;
}
else
{
log_i("init ADC OK");
}
lastMeasurement = millis();
}
void handleMeasure(void)
{
handleADC();
}
measureMode getMeasureMode(void)
{
return measureMode::mA200;
@@ -79,7 +75,7 @@ uint8_t getBar(void)
float getValue(void)
{
Serial.printf("ADCvolts:%4.2fV\n",ADCvolts);
log_i("ADCvolts:%4.2fV\n",ADCvolts);
return ADCvolts;
}
@@ -96,27 +92,4 @@ bool getOkState(void)
bool getOpenState(void)
{
return false;
}
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();
}
}

View File

@@ -0,0 +1,152 @@
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "board.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "u8g2_esp32_hal.h"
static const char *TAG = "u8g2_hal";
static const unsigned int I2C_TIMEOUT_MS = 1000;
static spi_device_handle_t handle_spi; // SPI handle.
static u8g2_esp32_hal_t u8g2_esp32_hal; // HAL state data.
#undef ESP_ERROR_CHECK
#define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { ESP_LOGE("err", "esp_err_t = %d", rc); assert(0 && #x);} } while(0);
/*
* Initialze the ESP32 HAL.
*/
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param) {
u8g2_esp32_hal = u8g2_esp32_hal_param;
} // u8g2_esp32_hal_init
/*
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
* to handle SPI communications.
*/
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
ESP_LOGD(TAG, "spi_byte_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
switch(msg) {
case U8X8_MSG_BYTE_SET_DC:
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.dc, arg_int);
}
break;
case U8X8_MSG_BYTE_INIT: {
if (u8g2_esp32_hal.clk == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.mosi == U8G2_ESP32_HAL_UNDEFINED ||
u8g2_esp32_hal.cs == U8G2_ESP32_HAL_UNDEFINED) {
break;
}
spi_bus_config_t bus_config;
memset(&bus_config, 0, sizeof(bus_config));
bus_config.sclk_io_num = OLED_SCK; // CLK
bus_config.mosi_io_num = OLED_MOSI; // MOSI
bus_config.miso_io_num = -1; // MISO
bus_config.quadwp_io_num = -1; // Not used
bus_config.quadhd_io_num = -1; // Not used
ESP_LOGI(TAG, "... Initializing bus.");
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));
spi_device_interface_config_t dev_config;
dev_config.address_bits = 0;
dev_config.command_bits = 0;
dev_config.dummy_bits = 0;
dev_config.mode = 0;
dev_config.duty_cycle_pos = 0;
dev_config.cs_ena_posttrans = 0;
dev_config.cs_ena_pretrans = 0;
dev_config.clock_speed_hz = 10000;
dev_config.spics_io_num = u8g2_esp32_hal.cs;
dev_config.flags = 0;
dev_config.queue_size = 200;
dev_config.pre_cb = NULL;
dev_config.post_cb = NULL;
ESP_LOGI(TAG, "... Adding device bus.");
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &handle_spi));
break;
}
case U8X8_MSG_BYTE_SEND: {
spi_transaction_t trans_desc;
trans_desc.addr = 0;
trans_desc.cmd = 0;
trans_desc.flags = 0;
trans_desc.length = 8 * arg_int; // Number of bits NOT number of bytes.
trans_desc.rxlength = 0;
trans_desc.tx_buffer = arg_ptr;
trans_desc.rx_buffer = NULL;
ESP_LOGI(TAG, "... Transmitting %d bytes.", arg_int);
ESP_ERROR_CHECK(spi_device_transmit(handle_spi, &trans_desc));
break;
}
}
return 0;
} // u8g2_esp32_spi_byte_cb
/*
* HAL callback function as prescribed by the U8G2 library. This callback is invoked
* to handle callbacks for GPIO and delay functions.
*/
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
ESP_LOGD(TAG, "gpio_and_delay_cb: Received a msg: %d, arg_int: %d, arg_ptr: %p", msg, arg_int, arg_ptr);
switch(msg) {
// Initialize the GPIO and DELAY HAL functions. If the pins for DC and RESET have been
// specified then we define those pins as GPIO outputs.
case U8X8_MSG_GPIO_AND_DELAY_INIT: {
uint64_t bitmask = 0;
if (u8g2_esp32_hal.dc != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull<<u8g2_esp32_hal.dc);
}
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull<<u8g2_esp32_hal.reset);
}
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
bitmask = bitmask | (1ull<<u8g2_esp32_hal.cs);
}
if (bitmask==0) {
break;
}
gpio_config_t gpioConfig;
gpioConfig.pin_bit_mask = bitmask;
gpioConfig.mode = GPIO_MODE_OUTPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
gpioConfig.pull_down_en = GPIO_PULLDOWN_ENABLE;
gpioConfig.intr_type = GPIO_INTR_DISABLE;
gpio_config(&gpioConfig);
break;
}
// Set the GPIO reset pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_RESET:
if (u8g2_esp32_hal.reset != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.reset, arg_int);
}
break;
// Set the GPIO client select pin to the value passed in through arg_int.
case U8X8_MSG_GPIO_CS:
if (u8g2_esp32_hal.cs != U8G2_ESP32_HAL_UNDEFINED) {
gpio_set_level(u8g2_esp32_hal.cs, arg_int);
}
break;
// Delay for the number of milliseconds passed in through arg_int.
case U8X8_MSG_DELAY_MILLI:
vTaskDelay(arg_int/portTICK_PERIOD_MS);
break;
}
return 0;
} // u8g2_esp32_gpio_and_delay_cb

View File

@@ -0,0 +1,38 @@
/*
* u8g2_esp32_hal.h
*
* Created on: Feb 12, 2017
* Author: kolban
*/
#ifndef U8G2_ESP32_HAL_H_
#define U8G2_ESP32_HAL_H_
#include "clib/u8g2.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#define U8G2_ESP32_HAL_UNDEFINED (-1)
#define I2C_MASTER_NUM I2C_NUM_1 // I2C port number for master dev
#define I2C_MASTER_TX_BUF_DISABLE 0 // I2C master do not need buffer
#define I2C_MASTER_RX_BUF_DISABLE 0 // I2C master do not need buffer
#define I2C_MASTER_FREQ_HZ 50000 // I2C master clock frequency
#define ACK_CHECK_EN 0x1 // I2C master will check ack from slave
#define ACK_CHECK_DIS 0x0 // I2C master will not check ack from slave
typedef struct {
int clk;
int16_t mosi;
gpio_num_t cs;
gpio_num_t reset;
gpio_num_t dc;
} u8g2_esp32_hal_t ;
#define U8G2_ESP32_HAL_DEFAULT {U8G2_ESP32_HAL_UNDEFINED, U8G2_ESP32_HAL_UNDEFINED, gpio_num_t(U8G2_ESP32_HAL_UNDEFINED), gpio_num_t(U8G2_ESP32_HAL_UNDEFINED), gpio_num_t(U8G2_ESP32_HAL_UNDEFINED) }
void u8g2_esp32_hal_init(u8g2_esp32_hal_t u8g2_esp32_hal_param);
uint8_t u8g2_esp32_spi_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8g2_esp32_i2c_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8g2_esp32_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
#endif /* U8G2_ESP32_HAL_H_ */