Skip to content

Commit

Permalink
New hardware setup for OBP60 V2
Browse files Browse the repository at this point in the history
  • Loading branch information
norbert-walter committed Jan 6, 2024
1 parent 0db52fe commit a97a69c
Show file tree
Hide file tree
Showing 29 changed files with 939 additions and 188 deletions.
101 changes: 61 additions & 40 deletions lib/obp60task/OBP60Extensions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifdef BOARD_OBP60S3

#include <Arduino.h>
#include <FastLED.h> // Driver for WS2812 RGB LED
#include <PCF8574.h> // Driver for PCF8574 output modul from Horter
#include <Wire.h> // I2C
#include <RTClib.h> // Driver for DS1388 RTC
#include "SunRise.h" // Lib for sunrise and sunset calculation
#include "Pagedata.h"
#include "OBP60Hardware.h"
Expand All @@ -18,68 +22,85 @@
#include "DSEG7Classic-BoldItalic42pt7b.h"
#include "DSEG7Classic-BoldItalic60pt7b.h"

MCP23017 mcp = MCP23017(MCP23017_I2C_ADDR);

// SPI pin definitions for E-Ink display
GxIO_Class io(SPI, OBP_SPI_CS, OBP_SPI_DC, OBP_SPI_RST); // SPI, CS, DC, RST
GxEPD_Class display(io, OBP_SPI_RST, OBP_SPI_BUSY); // io, RST, BUSY

// Horter I2C moduls
PCF8574 pcf8574_Out(PCF8574_I2C_ADDR1); // First digital output modul PCF8574 from Horter

// RTC DS1388
RTC_DS1388 ds1388;

// Define the array of leds
CRGB fled[NUM_FLASH_LED]; // Flash LED
CRGB backlight[NUM_BACKLIGHT_LED]; // Backlight

// Global vars
int outA = 0; // Outport Byte A
int outB = 0; // Outport Byte B
bool blinkingLED = false; // Enable / disable blinking flash LED
bool statusLED = false; // Actual status of flash LED on/off
int uvDuration = 0; // Under voltage duration in n x 100ms

void MCP23017Init()
void hardwareInit()
{
mcp.init();
mcp.portMode(MCP23017Port::A, 0b00110000); // Port A, 0 = out, 1 = in
mcp.portMode(MCP23017Port::B, 0b11110000); // Port B, 0 = out, 1 = in

// Extension Port A set defaults
setPortPin(OBP_DIGITAL_OUT1, false); // PA0
setPortPin(OBP_DIGITAL_OUT2, false); // PA1
setPortPin(OBP_FLASH_LED, false); // PA2
setPortPin(OBP_BACKLIGHT_LED, false); // PA3
setPortPin(OBP_POWER_50, true); // PA6
setPortPin(OBP_POWER_33, true); // PA7

// Extension Port B set defaults
setPortPin(PB0, false); // PB0
setPortPin(PB1, false); // PB1
setPortPin(PB2, false); // PB2
setPortPin(PB3, false); // PB3
}
// Init power rail 5.0V
setPortPin(OBP_POWER_50, true);

void setPortPin(uint pin, bool value){
if(pin <=7){
outA &= ~(1 << pin); // Clear bit
outA |= (value << pin); // Set bit
mcp.writeRegister(MCP23017Register::GPIO_A, outA);
// Init RGB LEDs
FastLED.addLeds<WS2812B, OBP_FLASH_LED, GRB>(fled, NUM_FLASH_LED);
FastLED.addLeds<WS2812B, OBP_BACKLIGHT_LED, GRB>(backlight, NUM_BACKLIGHT_LED);
FastLED.setBrightness(255);
fled[0] = CRGB::Red;
FastLED.show();

// Init PCF8574 digital outputs
Wire.setClock(I2C_SPEED); // Set I2C clock on 10 kHz
if(pcf8574_Out.begin()){ // Initialize PCF8574
pcf8574_Out.write8(255); // Clear all outputs
}
else{
pin = pin - 8;
outB &= ~(1 << pin); // Clear bit
outB |= (value << pin); // Set bit
mcp.writeRegister(MCP23017Register::GPIO_B, outB);

// Init DS1388 RTC
if(ds1388.begin()){
uint year = ds1388.now().year();
if(year < 2023){
ds1388.adjust(DateTime(__DATE__, __TIME__)); // Set date and time from PC file time
}
}
}

void setPortPin(uint pin, bool value){
pinMode(pin, OUTPUT);
digitalWrite(pin, value);
}

void togglePortPin(uint pin){
if(pin <=7){
outA ^= (1 << pin); // Set bit
mcp.writeRegister(MCP23017Register::GPIO_A, outA);
pinMode(pin, OUTPUT);
digitalWrite(pin, !digitalRead(pin));
}

void setFlashLED(bool status){
statusLED = status;
FastLED.setBrightness(255); // Brightness for flash LED
if(statusLED == true){
fled[0] = CRGB::Red; // Backlight LED on in red
}
else{
pin = pin - 8;
outB ^= (1 << pin); // Set bit
mcp.writeRegister(MCP23017Register::GPIO_B, outB);
fled[0] = CRGB::Black; // Backlight LED off
}
FastLED.show();
}

void blinkingFlashLED(){
if(blinkingLED == true){
togglePortPin(OBP_FLASH_LED);
statusLED != statusLED;
FastLED.setBrightness(255); // Brightness for flash LED
if(statusLED == true){
fled[0] = CRGB::Red; // Backlight LED on in red
}
else{
fled[0] = CRGB::Black; // Backlight LED off
}
FastLED.show();
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/obp60task/OBP60Extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#include <GxIO/GxIO_SPI/GxIO_SPI.h> // GxEPD lip for SPI display communikation
#include <GxIO/GxIO.h> // GxEPD lip for SPI

// Extension port
void MCP23017Init();

// E-Ink display
extern GxEPD_Class display; // E-Ink display functions

Expand All @@ -27,12 +24,15 @@ extern const GFXfont DSEG7Classic_BoldItalic42pt7b;
extern const GFXfont DSEG7Classic_BoldItalic60pt7b;

// Gloabl functions
void hardwareInit();

void setPortPin(uint pin, bool value); // Set port pin for extension port

void togglePortPin(uint pin); // Toggle extension port pin

void blinkingFlashLED(); // Blinking function for LED on extension port
void setBlinkingLED(bool on); // Set blinking LED active
void setFlashLED(bool status); // Set flash LED
void blinkingFlashLED(); // Blinking function for flash LED
void setBlinkingLED(bool on); // Set blinking flash LED active

void buzzer(uint frequency, uint duration); // Buzzer function
void setBuzzerPower(uint power); // Set buzzer power
Expand Down
94 changes: 38 additions & 56 deletions lib/obp60task/OBP60Hardware.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// General hardware definitions
// CAN bus pin definitions see obp60task.h
// CAN and RS485 bus pin definitions see obp60task.h

// Direction pin for RS485 NMEA0183
#define OBP_DIRECTION_PIN 27
// SeaTalk
#define OBP_SEATALK_TX 2
#define OBP_SEATALK_RX 15
// I2C (MCP23017, BME280, BMP280, SHT21)
#define OBP_I2C_SDA 21
#define OBP_I2C_SCL 22
// Extension Port MCP23017
#define MCP23017_I2C_ADDR 0x20 // Addr. 0 is 0x20
#define OBP_DIRECTION_PIN 18
// I2C
#define I2C_SPEED 10000UL // 10kHz clock speed on I2C bus
#define OBP_I2C_SDA 47
#define OBP_I2C_SCL 21
// DS1388 RTC
#define DS1388_I2C_ADDR 0xD0 // Addr. 0xD0
// BME280
#define BME280_I2C_ADDR 0x76 // Addr. 0x76 (0x77)
// BMP280
Expand All @@ -26,13 +24,15 @@
#define INA226_I2C_ADDR1 0x41 // Addr. 0x41 (fix A0 = 5V, A1 = GND) for battery
#define INA226_I2C_ADDR2 0x44 // Addr. 0x44 (fix A0 = GND, A1 = 5V) for solar panels
#define INA226_I2C_ADDR3 0x45 // Addr. 0x45 (fix A0 = 5V, A1 = 5V) for generator
// Horter modules
#define PCF8574_I2C_ADDR1 0x20 // First digital out module
// SPI (E-Ink display, Extern Bus)
#define OBP_SPI_CS 5
#define OBP_SPI_DC 17
#define OBP_SPI_RST 16
#define OBP_SPI_BUSY 4
#define OBP_SPI_CLK 18
#define OBP_SPI_DIN 23
#define OBP_SPI_CS 39
#define OBP_SPI_DC 40
#define OBP_SPI_RST 41
#define OBP_SPI_BUSY 42
#define OBP_SPI_CLK 38
#define OBP_SPI_DIN 48
#define SHOW_TIME 6000 // Show time for logo and WiFi QR code
#define FULL_REFRESH_TIME 600 // Refresh cycle time in [s][600...3600] for full display update (very important healcy function)
#define MAX_PAGE_NUMBER 10 // Max number of pages for show data
Expand All @@ -42,54 +42,36 @@
#define FONT4 "DSEG7Classic_BoldItalic80pt7b"

// GPS (NEO-6M, NEO-M8N)
#define OBP_GPS_TX 35 // Read only GPS data
// TTP229 Touch Pad Controller (!!No I2C!!)
#define TTP_SDO 25
#define TTP_SCL 33
#define OBP_GPS_RX 2
#define OBP_GPS_TX 1
// 1Wire (DS18B20)
#define OBP_1WIRE 32 // External 1Wire
#define OBP_1WIRE 6 // External 1Wire
// Buzzer
#define OBP_BUZZER 19
#define OBP_BUZZER 16
#define TONE1 1500 // 1500Hz
#define TONE2 2500 // 2500Hz
#define TONE3 3500 // 3500Hz
#define TONE4 4000 // 4000Hz
// Analog Input
#define OBP_ANALOG0 34 // Voltage power supplay
#define OBP_ANALOG1 36 // Analog In 1
#define OBP_ANALOG2 39 // Analog In 2
#define OBP_ANALOG0 4 // Voltage power supplay
#define MIN_VOLTAGE 9.0 // Min voltage for under voltage detection (then goto deep sleep)
#define POWER_FAIL_TIME 2 // in [ms] Accept min voltage until 2 x 1ms (for under voltage gaps by engine start)
// Extension Port PA
#define PA0 0 // Digital Out 1
#define PA1 1 // Digital Out 2
#define PA2 2 // Flash LED
#define PA3 3 // Backlight LEDs
#define PA4 4 // Digital In 1
#define PA5 5 // Digital In 2
#define PA6 6 // Power Rail 5.0V
#define PA7 7 // Power Rail 3.3V
// Extension Port PB
#define PB0 8 // Extension Connector
#define PB1 9 // Extension Connector
#define PB2 10 // Extension Connector
#define PB3 11 // Extension Connector
#define PB4 12 // Extension Connector
#define PB5 13 // Extension Connector
#define PB6 14 // Extension Connector
#define PB7 15 // Extension Connector
// Touch buttons
#define TOUCHTHRESHOLD 50000// Touch sensitivity, lower values more sensitiv
#define TP1 14 // Left outside
#define TP2 13
#define TP3 12
#define TP4 11
#define TP5 10
#define TP6 9 // Right ouside

// Flash LED (1x WS2812B)
#define NUM_FLASH_LED 1 // Number of flash LED
#define OBP_FLASH_LED 7 // GPIO port
// Backlight LEDs (6x WS2812B)
#define NUM_BACKLIGHT_LED 6 // Numebr of Backlight LEDs
#define OBP_BACKLIGHT_LED 15 // GPIO port
// Power Rail
#define OBP_POWER_50 5

// Extension Port Digital Out
#define OBP_DIGITAL_OUT1 PA0
#define OBP_DIGITAL_OUT2 PA1
// Extension Port LED
#define OBP_FLASH_LED PA2
// Extension Port Backlight LEDs
#define OBP_BACKLIGHT_LED PA3
// Extension Port Digital In
#define OBP_DIGITAL_IN1 PA4
#define OBP_DIGITAL_IN2 PA5
// Extension Port Power Rails
#define OBP_POWER_50 PA6
#define OBP_POWER_33 PA7

74 changes: 51 additions & 23 deletions lib/obp60task/OBP60Keypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

// Global vars

// Routine for TTP229-BSF (!!! IC use not a I2C bus !!!)
//
// 8 Key Mode
//
// Key number {0, 1, 2, 3, 4, 5, 6, 7, 8}
// Scan code {0, 8, 7, 1, 6, 2, 5, 3, 4}
int keyposition[9] = {0, 3, 5, 7, 8, 6, 4, 2, 1}; // Position of key in raw data, 0 = nothing touched
// Touch keypad over ESP32 touch sensor inputs

int keypad[9]; // Raw data array from TTP229
int key; // Value of key [0|1], 0 = touched, 1 = not touched
int keycode = 0; // Keycode of pressed key [0...8], 0 = nothing touched
Expand All @@ -27,32 +22,65 @@ long starttime = 0; // Start time point for pressed key

int readKeypad() {
int keystatus = 0; // Status of key [0...11], 0 = processed, 1...8 = key 1..8, 9 = right swipe , 10 = left swipe, 11 keys disabled

pinMode(TTP_SDO, INPUT);
pinMode(TTP_SCL, OUTPUT);
keycode = 0;
// Read key code from raw data

// Read key code
if(touchRead(14) > TOUCHTHRESHOLD){ // Touch pad 1
keypad[1] = 1;
}
else{
keypad[1] = 0;
}
if(touchRead(13) > TOUCHTHRESHOLD){ // Touch pad 2
keypad[2] = 1;
}
else{
keypad[2] = 0;
}
if(touchRead(12) > TOUCHTHRESHOLD){ // Touch pad 3
keypad[3] = 1;
}
else{
keypad[3] = 0;
}
if(touchRead(11) > TOUCHTHRESHOLD){ // Touch pad 4
keypad[4] = 1;
}
else{
keypad[4] = 0;
}
if(touchRead(10) > TOUCHTHRESHOLD){ // Touch pad 5
keypad[5] = 1;
}
else{
keypad[5] = 0;
}
if(touchRead(9) > TOUCHTHRESHOLD){ // Touch pad 6
keypad[6] = 1;
}
else{
keypad[6] = 0;
}
// Nothing touched
if(keypad[1] == 0 && keypad[2] == 0 && keypad[3] == 0 && keypad[4] == 0 && keypad[5] == 0 && keypad[6] == 0){
keypad[0] = 1;
}
else{
keypad[0] = 0;
}

for (int i = 0; i < 9; i++) {
digitalWrite(TTP_SCL, LOW);
// delay(1); // 0ms clock
delayMicroseconds(100);
keypad[i] = digitalRead(TTP_SDO);
if(i > 0){
// Invert keypad
// Convert keypad to keycode
if(keypad[i] == 1){
key = 0;
key = 1;
}
else{
key = 1;
key = 0;
}
keycode += key * i;
}
digitalWrite(TTP_SCL, HIGH);
// delay(1); // 0ms clock
delayMicroseconds(100);
}
// Remapping keycode
keycode = keyposition[keycode];

// Detect short keynumber
if (keycode > 0 ){
Expand Down
5 changes: 3 additions & 2 deletions lib/obp60task/OBPSensorTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ void underVoltageDetection(){
}
if(millis() > starttime + POWER_FAIL_TIME){
Timer1.stop(); // Stop Timer1
/*
setPortPin(OBP_BACKLIGHT_LED, false); // Backlight Off
setPortPin(OBP_FLASH_LED, false); // Flash LED Off
setPortPin(OBP_POWER_33, false); // Power rail 3.3V Off
setFlashLED(false); // Flash LED Off
*/
buzzer(TONE4, 20); // Buzzer tone 4kHz 20ms
setPortPin(OBP_POWER_50, false); // Power rail 5.0V Off
// Shutdown EInk display
Expand Down
Loading

0 comments on commit a97a69c

Please sign in to comment.