From bd9214cd5495523f9c18cbdf8ceea3159816266f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 09:58:39 -0400 Subject: [PATCH 01/81] Added the new libraries --- SSD1306Display.h | 405 +++++++++++++++++++++++++++++++++++++++-------- font.h | 12 +- images.h | 5 +- 3 files changed, 350 insertions(+), 72 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index c59866db..0d968f31 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -4,84 +4,353 @@ #if defined(ESP8266) #include + #include "font.h" #include "images.h" #define LCD_STD 0 // Standard LCD #define LCD_I2C 1 -class SSD1306Display : public SSD1306{ -public: - SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) : SSD1306(_addr, _sda, _scl) { - cx = 0; - cy = 0; - for(byte i=0;i=0&&idx= 0 && idx < NUM_CUSTOM_ICONS) custom_chars[idx] = ptr; + } + void setAutoDisplay(bool v) { auto_display = v; } + + private: + bool auto_display = true; + uint8_t cx, cy; + uint8_t fontWidth, fontHeight; + PGM_P custom_chars[NUM_CUSTOM_ICONS]; }; +// #elif defined(OSPI) +#else +#include +#include +#include + +#include "font.h" +#include "images.h" + +#define LCD_STD 0 // Standard LCD +#define LCD_I2C 1 + +#define BLACK 0 +#define WHITE 1 + +unsigned char init_command[] = { +#if 0 + // OLED_CMD_SET_HORI_ADDR_MODE + 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, + 0xD5, 0x80, 0xDA, 0x12, 0x81, 0xFF, + 0xA4, 0xDB, 0x40, 0x20, 0x00, 0x00, 0x10, 0x8D, + 0x14, 0x2E, 0xA6, 0xAF +#endif + +#if 1 + // OLED_CMD_SET_PAGE_ADDR_MODE + 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, 0xD5, + 0x80, 0xDA, 0x12, 0x81, 0xFF, 0xA4, 0xDB, 0x40, 0x20, + 0x02, 0x00, 0x10, 0x8D, 0x14, 0x2E, 0xA6, 0xAF #endif +}; + +// Header Values +#define JUMPTABLE_BYTES 4 + +#define JUMPTABLE_LSB 1 +#define JUMPTABLE_SIZE 2 +#define JUMPTABLE_WIDTH 3 +#define JUMPTABLE_START 4 + +#define WIDTH_POS 0 +#define HEIGHT_POS 1 +#define FIRST_CHAR_POS 2 +#define CHAR_NUM_POS 3 + +class SSD1306Display { + public: + SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) { + cx = 0; + cy = 0; + for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) custom_chars[i] = 0; + + int b; + for (b = 0; b < 1024; b++) { + frame[b] = 0x00; + } + + i2cd = wiringPiI2CSetup(_addr); + int i; + for (i = 0; i < sizeof(init_command); i++) { + unsigned int control = 0x00; // Co = 0, D/C = 0 + wiringPiI2CWriteReg8(i2cd, control, init_command[i]); + } + } + + void init() {} // Dummy function to match ESP8266 + void begin() { + flipScreenVertically(); + setFont(Monospaced_plain_13); + fontWidth = 8; + fontHeight = 16; + } + + void setFont(const uint8_t *f) { font = (uint8_t *)f; } + + void flipScreenVertically() { + unsigned char command[3] = {0x00, 0x20, 0x00}; + for (int i = 0; i < sizeof(command); i++) { + wiringPiI2CWriteReg8(i2cd, 0x00, command[i]); + } + } + + void display() { + unsigned char page_command[3]; + + for (int _page = 0; _page < 8; _page++) { + page_command[0] = 0x00; + page_command[1] = 0x10; + page_command[2] = 0xB0 + _page; -#endif // SSD1306_DISPLAY_H + for (int i = 0; i < sizeof(page_command); i++) { + wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); + } + for (int col = 0; col < 128; col++) { + wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); + } + } + } + void clear() { + int b; + for (b = 0; b < 1024; b++) { + frame[b] = 0x00; + } + + display(); + } + + void setColor(uint8_t color) { this->color = color; } + + void drawPixel(uint8_t x, uint8_t y) { + if (x >= 128 || y >= 64) return; + + if (color == WHITE) { + frame[x + (y / 8) * 128] |= 1 << (y % 8); + } else { + frame[x + (y / 8) * 128] &= ~(1 << (y % 8)); + } + } + + void fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h) { + for (int _x = x; _x < x + w; _x++) { + for (int _y = y; _y < y + h; _y++) { + drawPixel(_x, _y); + } + } + } + + void clear(int start, int end) { + setColor(BLACK); + fillRect(0, (start + 1) * fontHeight, 128, (end - start + 1) * fontHeight); + setColor(WHITE); + } + + void print(const char *s) { + while (*s) { + write(*s); + s++; + } + } + + uint8_t type() { return LCD_I2C; } + void noBlink() { /*no support*/ } + void blink() { /*no support*/ } + void setCursor(uint8_t col, int8_t row) { + /* assume 4 lines, the middle two lines + are row 0 and 1 */ + cy = (row + 1) * fontHeight; + cx = col * fontWidth; + } + void noBacklight() { /*no support*/ } + void backlight() { /*no support*/ } + void drawXbm(int x, int y, int w, int h, const char *data) { + for (int i = 0; i < w; i++) { + for (int j = 0; j < h; j++) { + if (data[i + (j / 8) * w] & (1 << (j % 8))) { + drawPixel(x + i, y + j); + } + } + } + } + + void drawChar(int x, int y, char c) { + uint8_t textHeight = font[HEIGHT_POS]; + uint8_t firstChar = font[FIRST_CHAR_POS]; + uint8_t numChars = font[CHAR_NUM_POS]; + uint16_t sizeOfJumpTable = numChars * JUMPTABLE_BYTES; + + if (c < firstChar || c >= firstChar + numChars) return; + + // 4 Bytes per char code + uint8_t charCode = c - firstChar; + + uint8_t msbJumpToChar = + font[JUMPTABLE_START + + charCode * JUMPTABLE_BYTES]; // MSB \ JumpAddress + uint8_t lsbJumpToChar = font[JUMPTABLE_START + charCode * JUMPTABLE_BYTES + + JUMPTABLE_LSB]; // LSB / + uint8_t charByteSize = font[JUMPTABLE_START + charCode * JUMPTABLE_BYTES + + JUMPTABLE_SIZE]; // Size + uint8_t currentCharWidth = + font[JUMPTABLE_START + (c - firstChar) * JUMPTABLE_BYTES + + JUMPTABLE_WIDTH]; // Width + + // Test if the char is drawable + if (!(msbJumpToChar == 255 && lsbJumpToChar == 255)) { + // Get the position of the char data + uint16_t charDataPosition = JUMPTABLE_START + sizeOfJumpTable + + ((msbJumpToChar << 8) + lsbJumpToChar); + int _y = y; + int _x = x; + + setColor(WHITE); + + for (int b = 0; b < charByteSize; b++) { + for (int i = 0; i < 8; i++) { + if (font[charDataPosition + b] & (1 << i)) { + drawPixel(_x, _y); + } + + _y++; + if (_y >= y + textHeight) { + _y = y; + _x++; + break; + } + } + } + } + } + + void drawString(int x, int y, const char *text) { + int _x = x; + int _y = y; + + while (*text) { + if (*text == '\n') { + _x = x; + _y += fontHeight; + } else { + drawChar(_x, _y, *text); + _x += fontWidth; + } + + text++; + } + } + + uint8_t write(uint8_t c) { + setColor(BLACK); + fillRect(cx, cy, fontWidth, fontHeight); + setColor(WHITE); + char cc[2] = {(char)c, 0}; + + if (c < NUM_CUSTOM_ICONS && custom_chars[c] != 0) { + drawXbm(cx, cy, fontWidth, fontHeight, (const char *)custom_chars[c]); + } else { + drawString(cx, cy, cc); + } + cx += fontWidth; + if (auto_display) display(); // todo: not very efficient + return 1; + } + + uint8_t write(const char *s) { + uint8_t nc = strlen(s); + setColor(BLACK); + fillRect(cx, cy, fontWidth * nc, fontHeight); + setColor(WHITE); + drawString(cx, cy, s); + cx += fontWidth * nc; + if (auto_display) display(); // todo: not very efficient + return nc; + } + + void createChar(uint8_t idx, const char *ptr) { + if (idx >= 0 && idx < NUM_CUSTOM_ICONS) custom_chars[idx] = ptr; + } + void setAutoDisplay(bool v) { auto_display = v; } + + private: + bool auto_display = false; + uint8_t cx, cy = 0; + uint8_t fontWidth, fontHeight; + const char *custom_chars[NUM_CUSTOM_ICONS]; + uint8_t frame[1024]; + int i2cd; + bool color; + uint8_t *font; +}; +#endif +#endif // SSD1306_DISPLAY_H diff --git a/font.h b/font.h index b92e17d5..e6758b80 100644 --- a/font.h +++ b/font.h @@ -1,6 +1,14 @@ -#if defined(ESP8266) // Created by http://oleddisplay.squix.ch/ Consider a donation // In case of problems make sure that you are using the font file with the correct version! +#ifndef FONT_H +#define FONT_H + +#if not defined(ESP8266) +#include +#define PROGMEM +#define byte uint8_t +#endif + const byte Monospaced_plain_13[] PROGMEM = { 0x08, // Width: 8 0x11, // Height: 17 @@ -458,4 +466,4 @@ const byte Monospaced_plain_13[] PROGMEM = { 0x00,0x00,0x00,0xC0,0x80,0x00,0x08,0x87,0x00,0x00,0xFC,0x00,0x00,0x38,0x00,0x08,0x07,0x00,0xC0,0x01 // 255 }; -#endif +#endif \ No newline at end of file diff --git a/images.h b/images.h index e9f87106..823182d3 100644 --- a/images.h +++ b/images.h @@ -32,7 +32,9 @@ enum { #endif -#if defined(ESP8266) +#if not defined(ESP8266) +#define PROGMEM +#endif #define WiFi_Logo_width 60 #define WiFi_Logo_height 36 @@ -236,5 +238,4 @@ const char _iconimage_pswitch[] PROGMEM = { #endif -#endif From ed68589f0343644cefd666e1a5de3aa5cee9ad0d Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:13:43 -0400 Subject: [PATCH 02/81] moved lcd functions outside of the arduino def in opensprinkler.h --- OpenSprinkler.h | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 0fa441a6..5d23fed2 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -46,7 +46,6 @@ #include #include #include - #include "SSD1306Display.h" #include "espconnect.h" #else // for AVR #include @@ -63,6 +62,14 @@ #include "etherport.h" #endif // end of headers +#if defined(USE_LCD) + #include "LiquidCrystal.h" +#endif + +#if defined(USE_SSD1306) + #include "SSD1306Display.h" +#endif + #if defined(ARDUINO) #if defined(ESP8266) extern ESP8266WebServer *update_server; @@ -197,12 +204,10 @@ class OpenSprinkler { public: // data members -#if defined(ESP8266) +#if defined(USE_SSD1306) static SSD1306Display lcd; // 128x64 OLED display -#elif defined(ARDUINO) +#elif defined(USE_LCD) static LiquidCrystal lcd; // 16x2 character LCD -#else - // todo: LCD define for RPI/BBB #endif #if defined(OSPI) @@ -325,6 +330,14 @@ class OpenSprinkler { static int8_t send_http_request(const char* server, uint16_t port, char* p, void(*callback)(char*)=NULL, uint16_t timeout=5000); static int8_t send_http_request(char* server_with_port, char* p, void(*callback)(char*)=NULL, uint16_t timeout=5000); // -- LCD functions +#if defined(USE_DISPLAY) + static void lcd_print_time(time_t t); // print current time + static void lcd_print_ip(const byte *ip, byte endian); // print ip + static void lcd_print_mac(const byte *mac); // print mac + static void lcd_print_screen(char c); // print station bits of the board selected by display_board + static void lcd_print_version(byte v); // print version number +#endif + #if defined(ARDUINO) // LCD functions for Arduino #if defined(ESP8266) static void lcd_print_pgm(PGM_P str); // ESP8266 does not allow PGM_P followed by PROGMEM @@ -333,11 +346,6 @@ class OpenSprinkler { static void lcd_print_pgm(PGM_P PROGMEM str); // print a program memory string static void lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line); #endif - static void lcd_print_time(time_t t); // print current time - static void lcd_print_ip(const byte *ip, byte endian); // print ip - static void lcd_print_mac(const byte *mac); // print mac - static void lcd_print_screen(char c); // print station bits of the board selected by display_board - static void lcd_print_version(byte v); // print version number static String time2str(uint32_t t) { uint16_t h = hour(t); @@ -384,10 +392,16 @@ class OpenSprinkler { static byte state; #endif +#endif // LCD functions for Arduino + private: +#if defined(USE_DISPLAY) static void lcd_print_option(int i); // print an option to the lcd static void lcd_print_2digit(int v); // print a integer in 2 digits static void lcd_start(); +#endif + +#if defined(ARDUINO) // LCD functions static byte button_read_busy(byte pin_butt, byte waitmode, byte butt, byte is_holding); #if defined(ESP8266) From 98a0ed4e67406bd3c3116375090e9f448ffb499d Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:13:55 -0400 Subject: [PATCH 03/81] made sure it compiles still for esp8266 --- SSD1306Display.h | 3 +-- defines.h | 8 ++++++++ images.h | 9 +++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 0d968f31..798e1063 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -80,8 +80,7 @@ class SSD1306Display : public SSD1306 { PGM_P custom_chars[NUM_CUSTOM_ICONS]; }; -// #elif defined(OSPI) -#else +#elif defined(OSPI) #include #include #include diff --git a/defines.h b/defines.h index 8c758b26..037e4a60 100755 --- a/defines.h +++ b/defines.h @@ -322,6 +322,8 @@ enum { #define digitalReadExt digitalRead #define digitalWriteExt digitalWrite + #define USE_DISPLAY + #define USE_LCD #elif defined(ESP8266) // for ESP8266 #define OS_HW_VERSION (OS_HW_VERSION_BASE+30) @@ -404,6 +406,9 @@ enum { #define V2_PIN_SENSOR1 3 // sensor 1 #define V2_PIN_SENSOR2 10 // sensor 2 + #define USE_DISPLAY + #define USE_SSD1306 + #elif defined(OSPI) // for OSPi #define OS_HW_VERSION OSPI_HW_VERSION_BASE @@ -422,6 +427,9 @@ enum { #define PIN_FREE_LIST {5,6,7,8,9,10,11,12,13,16,18,19,20,21,23,24,25,26} // free GPIO pins #define ETHER_BUFFER_SIZE 16384 + #define USE_DISPLAY + #define USE_SSD1306 + #elif defined(OSBO) // for OSBo #define OS_HW_VERSION OSBO_HW_VERSION_BASE diff --git a/images.h b/images.h index 823182d3..a6c9a004 100644 --- a/images.h +++ b/images.h @@ -32,7 +32,9 @@ enum { #endif -#if not defined(ESP8266) +#if defined(USE_SSD1306) + +#if not defined(ARDUINO) #define PROGMEM #endif @@ -139,7 +141,7 @@ const char _iconimage_pswitch[] PROGMEM = { */ -#elif defined(ARDUINO) +#elif defined(USE_LCD) const char _iconimage_connected[] PROGMEM = { B00000, @@ -234,8 +236,7 @@ const char _iconimage_pswitch[] PROGMEM = { */ -#else - #endif +#endif \ No newline at end of file From 231414338c6cae6b0dd4da447fab7cfa8144e5db Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:29:45 -0400 Subject: [PATCH 04/81] moving more code to use the display defines --- OpenSprinkler.cpp | 92 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 4879905a..aea80b7a 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -74,8 +74,13 @@ extern char tmp_buffer[]; extern char ether_buffer[]; extern ProgramData pd; -#if defined(ESP8266) +#if defined(USE_SSD1306) SSD1306Display OpenSprinkler::lcd(0x3c, SDA, SCL); +#elif defined(USE_LCD) + LiquidCrystal OpenSprinkler::lcd; +#endif + +#if defined(ESP8266) byte OpenSprinkler::state = OS_STATE_INITIAL; byte OpenSprinkler::prev_station_bits[MAX_NUM_BOARDS]; IOEXP* OpenSprinkler::expanders[MAX_NUM_BOARDS/2]; @@ -90,13 +95,11 @@ extern ProgramData pd; byte OpenSprinkler::wifi_channel=255; byte OpenSprinkler::wifi_testmode = 0; #elif defined(ARDUINO) - LiquidCrystal OpenSprinkler::lcd; extern SdFat sd; #else #if defined(OSPI) byte OpenSprinkler::pin_sr_data = PIN_SR_DATA; #endif - // todo future: LCD define for Linux-based systems #endif /** Option json names (stored in PROGMEM to reduce RAM usage) */ @@ -738,12 +741,12 @@ void OpenSprinkler::update_dev() { /** Initialize LCD */ void OpenSprinkler::lcd_start() { -#if defined(ESP8266) +#if defined(USE_SSD1306) // initialize SSD1306 lcd.init(); lcd.begin(); flash_screen(); -#else +#elif defined(USE_LCD) // initialize 16x2 character LCD // turn on lcd lcd.init(1, PIN_LCD_RS, 255, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7, 0,0,0,0); @@ -976,27 +979,28 @@ void OpenSprinkler::begin() { baseline_current = 0; #endif - +#endif +#if defined(USE_DISPLAY) lcd_start(); - #if defined(ESP8266) + #if defined(USE_SSD1306) lcd.createChar(ICON_ETHER_CONNECTED, _iconimage_ether_connected); lcd.createChar(ICON_ETHER_DISCONNECTED, _iconimage_ether_disconnected); - #else + lcd.createChar(ICON_WIFI_CONNECTED, _iconimage_wifi_connected); + lcd.createChar(ICON_WIFI_DISCONNECTED, _iconimage_wifi_disconnected); + #elif defined(USE_LCD) lcd.createChar(ICON_ETHER_CONNECTED, _iconimage_connected); lcd.createChar(ICON_ETHER_DISCONNECTED, _iconimage_disconnected); #endif + lcd.createChar(ICON_REMOTEXT, _iconimage_remotext); lcd.createChar(ICON_RAINDELAY, _iconimage_raindelay); lcd.createChar(ICON_RAIN, _iconimage_rain); lcd.createChar(ICON_SOIL, _iconimage_soil); +#endif +#if defined(ARDUINO) #if defined(ESP8266) - - /* create custom characters */ - lcd.createChar(ICON_WIFI_CONNECTED, _iconimage_wifi_connected); - lcd.createChar(ICON_WIFI_DISCONNECTED, _iconimage_wifi_disconnected); - lcd.setCursor(0,0); lcd.print(F("Init file system")); lcd.setCursor(0,1); @@ -2445,21 +2449,45 @@ void OpenSprinkler::lcd_print_2digit(int v) /** print time to a given line */ void OpenSprinkler::lcd_print_time(time_t t) { -#if defined(ESP8266) +#if defined(USE_SSD1306) lcd.setAutoDisplay(false); #endif lcd.setCursor(0, 0); lcd_print_2digit(hour(t)); + + #if defined(ARDUINO) lcd_print_pgm(PSTR(":")); + #else + lcd.print(':'); + #endif + lcd_print_2digit(minute(t)); + + #if defined(ARDUINO) lcd_print_pgm(PSTR(" ")); + #else + lcd.print(' '); + #endif + // each weekday string has 3 characters + ending 0 lcd_print_pgm(days_str+4*weekday_today()); + + #if defined(ARDUINO) lcd_print_pgm(PSTR(" ")); + #else + lcd.print(' '); + #endif + lcd_print_2digit(month(t)); + + #if defined(ARDUINO) lcd_print_pgm(PSTR("-")); + #else + lcd.print('-'); + #endif + lcd_print_2digit(day(t)); -#if defined(ESP8266) +#if defined(USE_SSD1306) lcd.display(); lcd.setAutoDisplay(true); #endif @@ -2467,15 +2495,22 @@ void OpenSprinkler::lcd_print_time(time_t t) /** print ip address */ void OpenSprinkler::lcd_print_ip(const byte *ip, byte endian) { -#if defined(ESP8266) +#if defined(USE_SSD1306) lcd.clear(0, 1); -#else +#elif defined(USE_LCD) lcd.clear(); #endif lcd.setCursor(0, 0); for (byte i=0; i<4; i++) { lcd.print(endian ? (int)ip[3-i] : (int)ip[i]); - if(i<3) lcd_print_pgm(PSTR(".")); + + if(i<3) { + #if defined(ARDUINO) + lcd_print_pgm(PSTR(".")); + #else + lcd.print('.'); + #endif + } } } @@ -2483,21 +2518,36 @@ void OpenSprinkler::lcd_print_ip(const byte *ip, byte endian) { void OpenSprinkler::lcd_print_mac(const byte *mac) { lcd.setCursor(0, 0); for(byte i=0; i<6; i++) { - if(i) lcd_print_pgm(PSTR("-")); + if(i) { + #if defined(ARDUINO) + lcd_print_pgm(PSTR("-")); + #else + lcd.print('-'); + #endif + } + lcd.print((mac[i]>>4), HEX); lcd.print((mac[i]&0x0F), HEX); if(i==4) lcd.setCursor(0, 1); } if(useEth) { + #if defined(ARDUINO) lcd_print_pgm(PSTR(" (Ether MAC)")); + #else + lcd.print(F(" (Ether MAC)")); + #endif } else { + #if defined(ARDUINO) lcd_print_pgm(PSTR(" (WiFi MAC)")); + #else + lcd.print(F(" (WiFi MAC)")); + #endif } } /** print station bits */ void OpenSprinkler::lcd_print_screen(char c) { -#if defined(ESP8266) +#if defined(USE_SSD1306) lcd.setAutoDisplay(false); // reduce screen drawing time by turning off display() when drawing individual characters #endif lcd.setCursor(0, 1); @@ -2609,7 +2659,7 @@ void OpenSprinkler::lcd_print_screen(char c) { } } #endif -#if defined(ESP8266) +#if defined(USE_SSD1306) lcd.display(); lcd.setAutoDisplay(true); #endif From c3b32bbccae3ff8029964e4143ed8f421bb59729 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:31:40 -0400 Subject: [PATCH 05/81] added wiringpi --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index ae257c8d..eaf9077e 100755 --- a/build.sh +++ b/build.sh @@ -14,7 +14,7 @@ if [ "$1" == "demo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev echo "Compiling demo firmware..." - g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto + g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -wiringPi elif [ "$1" == "osbo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev From 22a59645b005cbc88620550b1edfce9bcbba5369 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:33:21 -0400 Subject: [PATCH 06/81] fix buildscript --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index eaf9077e..3ada5089 100755 --- a/build.sh +++ b/build.sh @@ -14,7 +14,7 @@ if [ "$1" == "demo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev echo "Compiling demo firmware..." - g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -wiringPi + g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lwiringPi elif [ "$1" == "osbo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev From 994b457755ebc307c12a62400c667a6f58a22c49 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:53:09 -0400 Subject: [PATCH 07/81] added dummy defines for i2c --- defines.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/defines.h b/defines.h index 037e4a60..f00afbf8 100755 --- a/defines.h +++ b/defines.h @@ -427,6 +427,9 @@ enum { #define PIN_FREE_LIST {5,6,7,8,9,10,11,12,13,16,18,19,20,21,23,24,25,26} // free GPIO pins #define ETHER_BUFFER_SIZE 16384 + #define SDA 0 + #define SCL 0 + #define USE_DISPLAY #define USE_SSD1306 From 02bcab6929d3542bc921cba483763a40f768fa52 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:59:41 -0400 Subject: [PATCH 08/81] fixed building --- OpenSprinkler.cpp | 2 +- build.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index aea80b7a..64a906b3 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -737,7 +737,7 @@ void OpenSprinkler::update_dev() { } #endif // end network init functions -#if defined(ARDUINO) +#if defined(USE_DISPLAY) /** Initialize LCD */ void OpenSprinkler::lcd_start() { diff --git a/build.sh b/build.sh index 3ada5089..bf5f24a9 100755 --- a/build.sh +++ b/build.sh @@ -45,7 +45,7 @@ else fi echo "Compiling ospi firmware..." - g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lwiringPi $GPIOLIB fi if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/OpenSprinkler.sh ]; then From da5b82d08fc3a299e2cdffde24c5b98b4098098c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:02:59 -0400 Subject: [PATCH 09/81] added flash_screen --- OpenSprinkler.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 64a906b3..6f2ba8c1 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2922,7 +2922,7 @@ void OpenSprinkler::lcd_set_brightness(byte value) { } #endif // end of LCD and button functions -#if defined(ESP8266) +#if defined(SSD1306) #include "images.h" void OpenSprinkler::flash_screen() { lcd.setCursor(0, -1); @@ -2948,6 +2948,10 @@ void OpenSprinkler::set_screen_led(byte status) { lcd.setColor(WHITE); } +#endif + +#if defined(ESP8266) + void OpenSprinkler::reset_to_ap() { iopts[IOPT_WIFI_MODE] = WIFI_MODE_AP; iopts_save(); From 1b93e178f7598c3a95dfc70f834cf733e80cf1f4 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:14:53 -0400 Subject: [PATCH 10/81] added to defines --- OpenSprinkler.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 5d23fed2..e785cd50 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -336,6 +336,12 @@ class OpenSprinkler { static void lcd_print_mac(const byte *mac); // print mac static void lcd_print_screen(char c); // print station bits of the board selected by display_board static void lcd_print_version(byte v); // print version number + + #if defined(USE_SSD1306) + static void flash_screen(); + static void toggle_screen_led(); + static void set_screen_led(byte status); + #endif #endif #if defined(ARDUINO) // LCD functions for Arduino @@ -379,9 +385,6 @@ class OpenSprinkler { static IOEXP *expanders[]; static RCSwitch rfswitch; static void detect_expanders(); - static void flash_screen(); - static void toggle_screen_led(); - static void set_screen_led(byte status); static byte get_wifi_mode() { if (useEth) return WIFI_MODE_STA; else return wifi_testmode ? WIFI_MODE_STA : iopts[IOPT_WIFI_MODE];} static byte wifi_testmode; static String wifi_ssid, wifi_pass; From 21f0b344d3353446cf1e1edb30c72a8c0897e9f1 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:21:05 -0400 Subject: [PATCH 11/81] added inline keyword --- SSD1306Display.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 798e1063..72c02617 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -94,7 +94,7 @@ class SSD1306Display : public SSD1306 { #define BLACK 0 #define WHITE 1 -unsigned char init_command[] = { +inline unsigned char init_command[] = { #if 0 // OLED_CMD_SET_HORI_ADDR_MODE 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, From 37b2b97c14fc6464144b0b459a98ab65590963bc Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:44:13 -0400 Subject: [PATCH 12/81] fixed inline issue and fixed the def issue with the screen commands --- Makefile | 2 +- OpenSprinkler.cpp | 2 +- SSD1306Display.cpp | 22 ++++++++++++++++++++++ SSD1306Display.h | 2 +- build.sh | 6 +++--- 5 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 SSD1306Display.cpp diff --git a/Makefile b/Makefile index ccf68d15..90d62fc9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LD=$(CXX) LIBS=pthread mosquitto LDFLAGS=$(addprefix -l,$(LIBS)) BINARY=OpenSprinkler -SOURCES=main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp +SOURCES=main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp HEADERS=$(wildcard *.h) OBJECTS=$(SOURCES:.cpp=.o) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 6f2ba8c1..395fe760 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2922,7 +2922,7 @@ void OpenSprinkler::lcd_set_brightness(byte value) { } #endif // end of LCD and button functions -#if defined(SSD1306) +#if defined(USE_SSD1306) #include "images.h" void OpenSprinkler::flash_screen() { lcd.setCursor(0, -1); diff --git a/SSD1306Display.cpp b/SSD1306Display.cpp new file mode 100644 index 00000000..f509473f --- /dev/null +++ b/SSD1306Display.cpp @@ -0,0 +1,22 @@ +#if defined(ESP8266) + +#elif defined(OSPI) + +unsigned char init_command[] = { +#if 0 + // OLED_CMD_SET_HORI_ADDR_MODE + 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, + 0xD5, 0x80, 0xDA, 0x12, 0x81, 0xFF, + 0xA4, 0xDB, 0x40, 0x20, 0x00, 0x00, 0x10, 0x8D, + 0x14, 0x2E, 0xA6, 0xAF +#endif + +#if 1 + // OLED_CMD_SET_PAGE_ADDR_MODE + 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, 0xD5, + 0x80, 0xDA, 0x12, 0x81, 0xFF, 0xA4, 0xDB, 0x40, 0x20, + 0x02, 0x00, 0x10, 0x8D, 0x14, 0x2E, 0xA6, 0xAF +#endif +}; + +#endif \ No newline at end of file diff --git a/SSD1306Display.h b/SSD1306Display.h index 72c02617..fc8df2ec 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -94,7 +94,7 @@ class SSD1306Display : public SSD1306 { #define BLACK 0 #define WHITE 1 -inline unsigned char init_command[] = { +extern unsigned char init_command[] = { #if 0 // OLED_CMD_SET_HORI_ADDR_MODE 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, diff --git a/build.sh b/build.sh index bf5f24a9..691e9997 100755 --- a/build.sh +++ b/build.sh @@ -14,12 +14,12 @@ if [ "$1" == "demo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev echo "Compiling demo firmware..." - g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lwiringPi + g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto -lwiringPi elif [ "$1" == "osbo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev echo "Compiling osbo firmware..." - g++ -o OpenSprinkler -DOSBO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto + g++ -o OpenSprinkler -DOSBO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto else echo "Installing required libraries..." apt-get update @@ -45,7 +45,7 @@ else fi echo "Compiling ospi firmware..." - g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lwiringPi $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto -lwiringPi $GPIOLIB fi if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/OpenSprinkler.sh ]; then From a911876e9409828a748c5e4841da3390e4afe7f3 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:49:47 -0400 Subject: [PATCH 13/81] fixed ssd1306 rpi --- SSD1306Display.h | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index fc8df2ec..6190524a 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -94,22 +94,7 @@ class SSD1306Display : public SSD1306 { #define BLACK 0 #define WHITE 1 -extern unsigned char init_command[] = { -#if 0 - // OLED_CMD_SET_HORI_ADDR_MODE - 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, - 0xD5, 0x80, 0xDA, 0x12, 0x81, 0xFF, - 0xA4, 0xDB, 0x40, 0x20, 0x00, 0x00, 0x10, 0x8D, - 0x14, 0x2E, 0xA6, 0xAF -#endif - -#if 1 - // OLED_CMD_SET_PAGE_ADDR_MODE - 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, 0xD5, - 0x80, 0xDA, 0x12, 0x81, 0xFF, 0xA4, 0xDB, 0x40, 0x20, - 0x02, 0x00, 0x10, 0x8D, 0x14, 0x2E, 0xA6, 0xAF -#endif -}; +extern unsigned char init_command[]; // Header Values #define JUMPTABLE_BYTES 4 @@ -232,7 +217,7 @@ class SSD1306Display { } void noBacklight() { /*no support*/ } void backlight() { /*no support*/ } - void drawXbm(int x, int y, int w, int h, const char *data) { + void drawXbm(int x, int y, int w, int h, const uint8_t *data) { for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { if (data[i + (j / 8) * w] & (1 << (j % 8))) { @@ -242,6 +227,16 @@ class SSD1306Display { } } + void fillCircle(int x0, int y0, int r) { + for (int y = -r; y <= r; y++) { + for (int x = -r; x <= r; x++) { + if (x * x + y * y <= r * r) { + drawPixel(x0 + x, y0 + y); + } + } + } + } + void drawChar(int x, int y, char c) { uint8_t textHeight = font[HEIGHT_POS]; uint8_t firstChar = font[FIRST_CHAR_POS]; From c9323bbe224278a88a89a0034ef2f0cb2c9b8386 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:52:23 -0400 Subject: [PATCH 14/81] moved back to char --- SSD1306Display.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 6190524a..96267fe8 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -217,7 +217,7 @@ class SSD1306Display { } void noBacklight() { /*no support*/ } void backlight() { /*no support*/ } - void drawXbm(int x, int y, int w, int h, const uint8_t *data) { + void drawXbm(int x, int y, int w, int h, const char *data) { for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { if (data[i + (j / 8) * w] & (1 << (j % 8))) { From c52dea1fd7ade4489128196bb3c6866a815babcb Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:56:36 -0400 Subject: [PATCH 15/81] updated init display --- SSD1306Display.cpp | 8 ++++++++ SSD1306Display.h | 12 ++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/SSD1306Display.cpp b/SSD1306Display.cpp index f509473f..c44b42ba 100644 --- a/SSD1306Display.cpp +++ b/SSD1306Display.cpp @@ -19,4 +19,12 @@ unsigned char init_command[] = { #endif }; +void init_display(int i2cd) { + int i; + for (i = 0; i < sizeof(init_command); i++) { + unsigned int control = 0x00; // Co = 0, D/C = 0 + wiringPiI2CWriteReg8(i2cd, control, init_command[i]); + } +} + #endif \ No newline at end of file diff --git a/SSD1306Display.h b/SSD1306Display.h index 96267fe8..75d1fd6d 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -94,7 +94,7 @@ class SSD1306Display : public SSD1306 { #define BLACK 0 #define WHITE 1 -extern unsigned char init_command[]; +extern void init_display( int i2cd ); // Header Values #define JUMPTABLE_BYTES 4 @@ -122,11 +122,7 @@ class SSD1306Display { } i2cd = wiringPiI2CSetup(_addr); - int i; - for (i = 0; i < sizeof(init_command); i++) { - unsigned int control = 0x00; // Co = 0, D/C = 0 - wiringPiI2CWriteReg8(i2cd, control, init_command[i]); - } + init_display(i2cd); } void init() {} // Dummy function to match ESP8266 @@ -227,6 +223,10 @@ class SSD1306Display { } } + void drawXbm(int x, int y, int w, int h, const uint8_t *data) { + drawXbm(x, y, w, h, (const char *)data); + } + void fillCircle(int x0, int y0, int r) { for (int y = -r; y <= r; y++) { for (int x = -r; x <= r; x++) { From 1c518a5e732ff2487dcfa8aaa81b6fb2f7964a33 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:58:30 -0400 Subject: [PATCH 16/81] included the header in cpp file --- SSD1306Display.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SSD1306Display.cpp b/SSD1306Display.cpp index c44b42ba..7442255e 100644 --- a/SSD1306Display.cpp +++ b/SSD1306Display.cpp @@ -2,6 +2,8 @@ #elif defined(OSPI) +#include "SSD1306Display.h" + unsigned char init_command[] = { #if 0 // OLED_CMD_SET_HORI_ADDR_MODE From 16ead0ba35eee770590fff6a3926baeb216b2cb5 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:24:04 -0400 Subject: [PATCH 17/81] fix the bitmap rendering --- SSD1306Display.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 75d1fd6d..9c83b93b 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -213,11 +213,20 @@ class SSD1306Display { } void noBacklight() { /*no support*/ } void backlight() { /*no support*/ } - void drawXbm(int x, int y, int w, int h, const char *data) { - for (int i = 0; i < w; i++) { - for (int j = 0; j < h; j++) { - if (data[i + (j / 8) * w] & (1 << (j % 8))) { - drawPixel(x + i, y + j); + void drawXbm(int x, int y, int w, int h, const char *xbm) { + int xbmWidth = (w + 7) / 8; + uint8_t data = 0; + + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) { + if (j & 7) { + data >>= 1; + } else { + data = xbm[(i * xbmWidth) + (j / 8)]; + } + + if (data & 0x01) { + drawPixel(x + j, y + i); } } } From 54667bfa2c644b326ad1f88eac09328b860eaf35 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:07:38 -0400 Subject: [PATCH 18/81] added wiringpi to install --- build.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/build.sh b/build.sh index 691e9997..a5cd95e2 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,23 @@ #!/bin/bash +function download_wiringpi { + echo "Downloading WiringPi..." + if [ $(arch) == "aarch64" ]; then + wget https://github.com/WiringPi/WiringPi/releases/download/3.6/wiringpi_3.6_arm64.deb -O wiringpi.deb + else + wget https://github.com/WiringPi/WiringPi/releases/download/3.6/wiringpi_3.6_armhf.deb -O wiringpi.deb + fi + + if [ $? -ne 0 ]; then + echo "Failed to download WiringPi" + exit 1 + fi + + echo "Installing WiringPi..." + dpkg -i wiringpi.deb + rm wiringpi.deb +} + while getopts ":s" opt; do case $opt in s) @@ -12,7 +30,11 @@ echo "Building OpenSprinkler..." if [ "$1" == "demo" ]; then echo "Installing required libraries..." + + dpkg -i wiringpi_3.6_arm64.deb + rm wiringpi_3.6_arm64.deb apt-get install -y libmosquitto-dev + download_wiringpi echo "Compiling demo firmware..." g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto -lwiringPi elif [ "$1" == "osbo" ]; then @@ -28,6 +50,7 @@ else apt-get install -y libi2c-dev apt-get install -y libssl-dev apt-get install -y libgpiod-dev + download_wiringpi if ! command -v raspi-gpio &> /dev/null then echo "Command raspi-gpio is required and is not installed" From b6634e7e09156205b365e2885de4ce77af4d7f8f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:49:43 -0400 Subject: [PATCH 19/81] change impl --- SSD1306Display.h | 54 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 9c83b93b..5d68c2e9 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -143,21 +143,51 @@ class SSD1306Display { } void display() { - unsigned char page_command[3]; + // sendCommand(COLUMNADDR); + // sendCommand(x_offset); + // sendCommand(x_offset + (this->width() - 1)); + + // sendCommand(PAGEADDR); + // sendCommand(0x0); + + // for (uint16_t i=0; i < displayBufferSize; i++) { + // _wire->beginTransmission(this->_address); + // _wire->write(0x40); + // for (uint8_t x = 0; x < 16; x++) { + // _wire->write(buffer[i]); + // i++; + // } + // i--; + // _wire->endTransmission(); + // } + + wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address + + wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address + + for (int i = 0; i < 1024; i++) { + wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); + } - for (int _page = 0; _page < 8; _page++) { - page_command[0] = 0x00; - page_command[1] = 0x10; - page_command[2] = 0xB0 + _page; - for (int i = 0; i < sizeof(page_command); i++) { - wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); - } + // unsigned char page_command[3]; - for (int col = 0; col < 128; col++) { - wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); - } - } + // for (int _page = 0; _page < 8; _page++) { + // page_command[0] = 0x00; + // page_command[1] = 0x10; + // page_command[2] = 0xB0 + _page; + + // for (int i = 0; i < sizeof(page_command); i++) { + // wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); + // } + + // for (int col = 0; col < 128; col++) { + // wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); + // } + // } } void clear() { From 24d41bf9b1ef1f22b305ac5d3e0e6d5120f2c34c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:01:40 -0400 Subject: [PATCH 20/81] add state machine --- build.sh | 6 ++++++ main.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index a5cd95e2..87928fb9 100755 --- a/build.sh +++ b/build.sh @@ -18,6 +18,12 @@ function download_wiringpi { rm wiringpi.deb } +function enable_i2c { + sudo raspi-config nonint do_i2c 1 + sudo dtparam i2c_baudrate=400000 + sudo modprobe i2c-dev +} + while getopts ":s" opt; do case $opt in s) diff --git a/main.cpp b/main.cpp index eb0c25a6..66d8313e 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,6 @@ Wiznet5500lwIP w5500(PIN_ETHER_CS); // W5500 lwip for wired Ether lwipEth eth; bool useEth = false; // tracks whether we are using WiFi or wired Ether connection - static uint16_t led_blink_ms = LED_FAST_BLINK; #else EthernetServer *m_server = NULL; EthernetClient *m_client = NULL; @@ -52,6 +51,10 @@ EthernetClient *m_client = 0; #endif +#if defined(USE_SSD1306) + static uint16_t led_blink_ms = LED_FAST_BLINK; +#endif + void push_message(int type, uint32_t lval=0, float fval=0.f, const char* sval=NULL); void manual_start_program(byte, byte); void remote_http_callback(char*); @@ -393,6 +396,50 @@ void do_setup() { os.mqtt.init(); os.status.req_mqtt_restart = true; } + +// ====== UI defines ====== +static char ui_anim_chars[3] = {'.', 'o', 'O'}; + +#define UI_STATE_DEFAULT 0 +#define UI_STATE_DISP_IP 1 +#define UI_STATE_DISP_GW 2 +#define UI_STATE_RUNPROG 3 + +static byte ui_state = UI_STATE_DEFAULT; +static byte ui_state_runprog = 0; + +bool ui_confirm(PGM_P str) { + os.lcd_print_line_clear_pgm(str, 0); + os.lcd_print_line_clear_pgm(PSTR("(B1:No, B3:Yes)"), 1); + byte button; + ulong start = millis(); + do { + button = os.button_read(BUTTON_WAIT_NONE); + if((button&BUTTON_MASK)==BUTTON_3 && (button&BUTTON_FLAG_DOWN)) return true; + if((button&BUTTON_MASK)==BUTTON_1 && (button&BUTTON_FLAG_DOWN)) return false; + delay(10); + } while(millis() - start < 2500); + return false; +} + +void ui_state_machine() { + + // to avoid ui_state_machine taking too much computation time + // we run it only every UI_STATE_MACHINE_INTERVAL ms + static uint32_t last_usm = 0; + if(millis() - last_usm <= UI_STATE_MACHINE_INTERVAL) { return; } + last_usm = millis(); + + // process screen led + static ulong led_toggle_timeout = 0; + if(led_blink_ms) { + if(millis()>led_toggle_timeout) { + os.toggle_screen_led(); + led_toggle_timeout = millis() + led_blink_ms; + } + } +} + #endif void turn_on_station(byte sid, ulong duration); @@ -596,6 +643,8 @@ void do_loop() } } } + + ui_state_machine(); #endif // Process Ethernet packets // Start up MQTT when we have a network connection From 39559ff521e2cf0e834c5c9b24319a63685ce330 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:04:46 -0400 Subject: [PATCH 21/81] commented out code --- main.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 66d8313e..ab96c239 100644 --- a/main.cpp +++ b/main.cpp @@ -408,19 +408,19 @@ static char ui_anim_chars[3] = {'.', 'o', 'O'}; static byte ui_state = UI_STATE_DEFAULT; static byte ui_state_runprog = 0; -bool ui_confirm(PGM_P str) { - os.lcd_print_line_clear_pgm(str, 0); - os.lcd_print_line_clear_pgm(PSTR("(B1:No, B3:Yes)"), 1); - byte button; - ulong start = millis(); - do { - button = os.button_read(BUTTON_WAIT_NONE); - if((button&BUTTON_MASK)==BUTTON_3 && (button&BUTTON_FLAG_DOWN)) return true; - if((button&BUTTON_MASK)==BUTTON_1 && (button&BUTTON_FLAG_DOWN)) return false; - delay(10); - } while(millis() - start < 2500); - return false; -} +// bool ui_confirm(PGM_P str) { +// os.lcd_print_line_clear_pgm(str, 0); +// os.lcd_print_line_clear_pgm(PSTR("(B1:No, B3:Yes)"), 1); +// byte button; +// ulong start = millis(); +// do { +// button = os.button_read(BUTTON_WAIT_NONE); +// if((button&BUTTON_MASK)==BUTTON_3 && (button&BUTTON_FLAG_DOWN)) return true; +// if((button&BUTTON_MASK)==BUTTON_1 && (button&BUTTON_FLAG_DOWN)) return false; +// delay(10); +// } while(millis() - start < 2500); +// return false; +// } void ui_state_machine() { From 16d581500c5e7febb5e5c5cdc7e7906e33fb45cb Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:18:43 -0400 Subject: [PATCH 22/81] add in screen --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index ab96c239..2a4927e6 100644 --- a/main.cpp +++ b/main.cpp @@ -968,7 +968,7 @@ void do_loop() // activate/deactivate valves os.apply_all_station_bits(); -#if defined(ARDUINO) +#if defined(HAS_DISPLAY) // process LCD display if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } From dbb21fee1fd911c6130b7cddefba5664c0c28e90 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:19:31 -0400 Subject: [PATCH 23/81] fix def check --- main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 2a4927e6..89f91cf0 100644 --- a/main.cpp +++ b/main.cpp @@ -968,10 +968,9 @@ void do_loop() // activate/deactivate valves os.apply_all_station_bits(); -#if defined(HAS_DISPLAY) +#if defined(USE_DISPLAY) // process LCD display if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } - #endif // handle reboot request From 4efd03a7ee46ad54e2a5b9fe7f6296dd7683994c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:27:59 -0400 Subject: [PATCH 24/81] try write block --- SSD1306Display.h | 8 +++++--- wiringPiI2C.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 wiringPiI2C.h diff --git a/SSD1306Display.h b/SSD1306Display.h index 5d68c2e9..248a3e90 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -168,9 +168,11 @@ class SSD1306Display { wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address - for (int i = 0; i < 1024; i++) { - wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); - } + // for (int i = 0; i < 1024; i++) { + // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); + // } + + wiringPiI2CWriteBlockData(i2cd, 0x40, frame, sizeof(frame); // unsigned char page_command[3]; diff --git a/wiringPiI2C.h b/wiringPiI2C.h new file mode 100644 index 00000000..a7b0804f --- /dev/null +++ b/wiringPiI2C.h @@ -0,0 +1,48 @@ +/* + * wiringPiI2C.h: + * Simplified I2C access routines + * Copyright (c) 2013-2024 Gordon Henderson and contributors + *********************************************************************** + * This file is part of wiringPi: + * https://github.com/WiringPi/WiringPi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern int wiringPiI2CRead (int fd) ; +extern int wiringPiI2CReadReg8 (int fd, int reg) ; +extern int wiringPiI2CReadReg16 (int fd, int reg) ; +extern int wiringPiI2CReadBlockData (int fd, int reg, uint8_t *values, uint8_t size); //Interface 3.3 +extern int wiringPiI2CRawRead (int fd, uint8_t *values, uint8_t size); //Interface 3.3 + +extern int wiringPiI2CWrite (int fd, int data) ; +extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ; +extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ; +extern int wiringPiI2CWriteBlockData (int fd, int reg, const uint8_t *values, uint8_t size); //Interface 3.3 +extern int wiringPiI2CRawWrite (int fd, const uint8_t *values, uint8_t size); //Interface 3.3 + +extern int wiringPiI2CSetupInterface (const char *device, int devId) ; +extern int wiringPiI2CSetup (const int devId) ; + +#ifdef __cplusplus +} +#endif \ No newline at end of file From 086e2a9bedcd352ad0a170e20a1ee02ca9a2d1ce Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:28:39 -0400 Subject: [PATCH 25/81] fixed code --- SSD1306Display.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 248a3e90..fd100d5d 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -172,7 +172,7 @@ class SSD1306Display { // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); // } - wiringPiI2CWriteBlockData(i2cd, 0x40, frame, sizeof(frame); + wiringPiI2CWriteBlockData(i2cd, 0x40, frame, sizeof(frame)); // unsigned char page_command[3]; From 29f79bc76bd84984946168c10aaac5ac2bb05504 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:32:16 -0400 Subject: [PATCH 26/81] chunk write --- SSD1306Display.h | 9 ++++++++- main.cpp | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index fd100d5d..056910f0 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -172,7 +172,14 @@ class SSD1306Display { // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); // } - wiringPiI2CWriteBlockData(i2cd, 0x40, frame, sizeof(frame)); + uint frameSize = sizeof(frame); + uint8_t *frame_ptr = frame; + while (frameSize > 0) { + int chunkSize = frameSize > 255 ? 255 : frameSize; + wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); + frame_ptr += chunkSize; + frameSize -= chunkSize; + } // unsigned char page_command[3]; diff --git a/main.cpp b/main.cpp index 89f91cf0..90ce9f60 100644 --- a/main.cpp +++ b/main.cpp @@ -970,7 +970,7 @@ void do_loop() #if defined(USE_DISPLAY) // process LCD display - if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } + // if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } #endif // handle reboot request From 3253fa6420c0388eadd8ea53007c4080d87ffb4a Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:33:02 -0400 Subject: [PATCH 27/81] fixed size --- SSD1306Display.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 056910f0..cedf15ca 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -172,7 +172,7 @@ class SSD1306Display { // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); // } - uint frameSize = sizeof(frame); + int frameSize = sizeof(frame); uint8_t *frame_ptr = frame; while (frameSize > 0) { int chunkSize = frameSize > 255 ? 255 : frameSize; From 359a75a77e36d5685f8fad849d515a5f541513ad Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:35:48 -0400 Subject: [PATCH 28/81] display chunk size --- SSD1306Display.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index cedf15ca..64ab4391 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -142,6 +142,8 @@ class SSD1306Display { } } + #define DISPLAY_CHUNK_SIZE 16 + void display() { // sendCommand(COLUMNADDR); // sendCommand(x_offset); @@ -175,7 +177,7 @@ class SSD1306Display { int frameSize = sizeof(frame); uint8_t *frame_ptr = frame; while (frameSize > 0) { - int chunkSize = frameSize > 255 ? 255 : frameSize; + int chunkSize = frameSize > DISPLAY_CHUNK_SIZE ? DISPLAY_CHUNK_SIZE : frameSize; wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); frame_ptr += chunkSize; frameSize -= chunkSize; From 5a3ac79632755471ec2d9c34378b5ed4f85859c1 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:41:47 -0400 Subject: [PATCH 29/81] update display --- SSD1306Display.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 64ab4391..8282f0bd 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -142,7 +142,7 @@ class SSD1306Display { } } - #define DISPLAY_CHUNK_SIZE 16 + #define DISPLAY_CHUNK_SIZE 32 void display() { // sendCommand(COLUMNADDR); @@ -178,7 +178,7 @@ class SSD1306Display { uint8_t *frame_ptr = frame; while (frameSize > 0) { int chunkSize = frameSize > DISPLAY_CHUNK_SIZE ? DISPLAY_CHUNK_SIZE : frameSize; - wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); + chunkSize = wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); frame_ptr += chunkSize; frameSize -= chunkSize; } From d16775da5cdf9bae02fd505f6f9933f0d91eba51 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:44:51 -0400 Subject: [PATCH 30/81] revert display --- SSD1306Display.h | 73 +++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 8282f0bd..b58672f7 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -145,60 +145,43 @@ class SSD1306Display { #define DISPLAY_CHUNK_SIZE 32 void display() { - // sendCommand(COLUMNADDR); - // sendCommand(x_offset); - // sendCommand(x_offset + (this->width() - 1)); - - // sendCommand(PAGEADDR); - // sendCommand(0x0); - - // for (uint16_t i=0; i < displayBufferSize; i++) { - // _wire->beginTransmission(this->_address); - // _wire->write(0x40); - // for (uint8_t x = 0; x < 16; x++) { - // _wire->write(buffer[i]); - // i++; - // } - // i--; - // _wire->endTransmission(); - // } - wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address + // wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address + // wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address + // wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address + // wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address + // wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address - // for (int i = 0; i < 1024; i++) { - // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); - // } + // // for (int i = 0; i < 1024; i++) { + // // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); + // // } - int frameSize = sizeof(frame); - uint8_t *frame_ptr = frame; - while (frameSize > 0) { - int chunkSize = frameSize > DISPLAY_CHUNK_SIZE ? DISPLAY_CHUNK_SIZE : frameSize; - chunkSize = wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); - frame_ptr += chunkSize; - frameSize -= chunkSize; - } + // int frameSize = sizeof(frame); + // uint8_t *frame_ptr = frame; + // while (frameSize > 0) { + // int chunkSize = frameSize > DISPLAY_CHUNK_SIZE ? DISPLAY_CHUNK_SIZE : frameSize; + // chunkSize = wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); + // frame_ptr += chunkSize; + // frameSize -= chunkSize; + // } - // unsigned char page_command[3]; + unsigned char page_command[3]; - // for (int _page = 0; _page < 8; _page++) { - // page_command[0] = 0x00; - // page_command[1] = 0x10; - // page_command[2] = 0xB0 + _page; + for (int _page = 0; _page < 8; _page++) { + page_command[0] = 0x00; + page_command[1] = 0x10; + page_command[2] = 0xB0 + _page; - // for (int i = 0; i < sizeof(page_command); i++) { - // wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); - // } + for (int i = 0; i < sizeof(page_command); i++) { + wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); + } - // for (int col = 0; col < 128; col++) { - // wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); - // } - // } + for (int col = 0; col < 128; col++) { + wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); + } + } } void clear() { From cdefdff8624ae9ca0711384d11d24a8b48bf8b23 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:10:50 -0400 Subject: [PATCH 31/81] revert --- SSD1306Display.h | 43 +++++++------------------------------------ 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index b58672f7..87edc3e9 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -142,45 +142,16 @@ class SSD1306Display { } } - #define DISPLAY_CHUNK_SIZE 32 - void display() { + wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address - // wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address - // wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address - // wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address - - // wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address - // wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address - - // // for (int i = 0; i < 1024; i++) { - // // wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); - // // } - - // int frameSize = sizeof(frame); - // uint8_t *frame_ptr = frame; - // while (frameSize > 0) { - // int chunkSize = frameSize > DISPLAY_CHUNK_SIZE ? DISPLAY_CHUNK_SIZE : frameSize; - // chunkSize = wiringPiI2CWriteBlockData(i2cd, 0x40, frame_ptr, chunkSize); - // frame_ptr += chunkSize; - // frameSize -= chunkSize; - // } - + wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address + wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address - unsigned char page_command[3]; - - for (int _page = 0; _page < 8; _page++) { - page_command[0] = 0x00; - page_command[1] = 0x10; - page_command[2] = 0xB0 + _page; - - for (int i = 0; i < sizeof(page_command); i++) { - wiringPiI2CWriteReg8(i2cd, 0x00, page_command[i]); - } - - for (int col = 0; col < 128; col++) { - wiringPiI2CWriteReg8(i2cd, 0x40, frame[_page * 128 + col]); - } + for (int i = 0; i < 1024; i++) { + wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); } } From 678efc0e491ff6586671ffa8f45fd20f78d04bf4 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:17:57 -0400 Subject: [PATCH 32/81] more progress towards adding a display --- OpenSprinkler.cpp | 42 ++++++++++-------------------------------- main.cpp | 2 +- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 395fe760..71d6a252 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2411,6 +2411,7 @@ void OpenSprinkler::raindelay_stop() { } /** LCD and button functions */ +#if defined(USE_DISPLAY) #if defined(ARDUINO) // AVR LCD and button functions /** print a program memory string */ #if defined(ESP8266) @@ -2440,6 +2441,11 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { for(; (16-cnt) >= 0; cnt ++) lcd_print_pgm(PSTR(" ")); } +#else +#define lcd_print_pgm(PGM_P PROGMEM str) lcd.print(str) +// #define lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) lcd.print(str) +#endif + void OpenSprinkler::lcd_print_2digit(int v) { lcd.print((int)(v/10)); @@ -2455,36 +2461,20 @@ void OpenSprinkler::lcd_print_time(time_t t) lcd.setCursor(0, 0); lcd_print_2digit(hour(t)); - #if defined(ARDUINO) lcd_print_pgm(PSTR(":")); - #else - lcd.print(':'); - #endif lcd_print_2digit(minute(t)); - #if defined(ARDUINO) lcd_print_pgm(PSTR(" ")); - #else - lcd.print(' '); - #endif // each weekday string has 3 characters + ending 0 lcd_print_pgm(days_str+4*weekday_today()); - #if defined(ARDUINO) lcd_print_pgm(PSTR(" ")); - #else - lcd.print(' '); - #endif lcd_print_2digit(month(t)); - #if defined(ARDUINO) lcd_print_pgm(PSTR("-")); - #else - lcd.print('-'); - #endif lcd_print_2digit(day(t)); #if defined(USE_SSD1306) @@ -2505,11 +2495,7 @@ void OpenSprinkler::lcd_print_ip(const byte *ip, byte endian) { lcd.print(endian ? (int)ip[3-i] : (int)ip[i]); if(i<3) { - #if defined(ARDUINO) lcd_print_pgm(PSTR(".")); - #else - lcd.print('.'); - #endif } } } @@ -2519,11 +2505,7 @@ void OpenSprinkler::lcd_print_mac(const byte *mac) { lcd.setCursor(0, 0); for(byte i=0; i<6; i++) { if(i) { - #if defined(ARDUINO) lcd_print_pgm(PSTR("-")); - #else - lcd.print('-'); - #endif } lcd.print((mac[i]>>4), HEX); @@ -2531,17 +2513,9 @@ void OpenSprinkler::lcd_print_mac(const byte *mac) { if(i==4) lcd.setCursor(0, 1); } if(useEth) { - #if defined(ARDUINO) lcd_print_pgm(PSTR(" (Ether MAC)")); - #else - lcd.print(F(" (Ether MAC)")); - #endif } else { - #if defined(ARDUINO) lcd_print_pgm(PSTR(" (WiFi MAC)")); - #else - lcd.print(F(" (WiFi MAC)")); - #endif } } @@ -2762,6 +2736,10 @@ void OpenSprinkler::lcd_print_option(int i) { } +#endif + +#if defined(ARDUINO) + /** Button functions */ /** wait for button */ byte OpenSprinkler::button_read_busy(byte pin_butt, byte waitmode, byte butt, byte is_holding) { diff --git a/main.cpp b/main.cpp index 90ce9f60..89f91cf0 100644 --- a/main.cpp +++ b/main.cpp @@ -970,7 +970,7 @@ void do_loop() #if defined(USE_DISPLAY) // process LCD display - // if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } + if (!ui_state) { os.lcd_print_screen(ui_anim_chars[(unsigned long)curr_time%3]); } #endif // handle reboot request From c6a6c077092d30a34c3685190eb3f4febd6a9a09 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:18:39 -0400 Subject: [PATCH 33/81] fix define --- OpenSprinkler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 71d6a252..0ae44def 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2442,7 +2442,7 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { } #else -#define lcd_print_pgm(PGM_P PROGMEM str) lcd.print(str) +#define lcd_print_pgm(PGM_P str) lcd.print(str) // #define lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) lcd.print(str) #endif From b8ccadbe24b7440e8742787030df0d3f668ad42c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:19:11 -0400 Subject: [PATCH 34/81] once again --- OpenSprinkler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 0ae44def..2ce9ae3f 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2442,7 +2442,7 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { } #else -#define lcd_print_pgm(PGM_P str) lcd.print(str) +#define lcd_print_pgm(PSTR(str)) lcd.print(str) // #define lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) lcd.print(str) #endif From 13e4956c003c870914f996d95e9886ee937389ea Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:20:51 -0400 Subject: [PATCH 35/81] int printing --- SSD1306Display.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SSD1306Display.h b/SSD1306Display.h index 87edc3e9..6f64a939 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -197,6 +197,10 @@ class SSD1306Display { } } + void print(int i) { print(sprintf("%d", i)); } + void print(float f) { print(sprintf("%f", f)); } + void print(int i, int base) { print(sprintf("%d", i, base)); } + uint8_t type() { return LCD_I2C; } void noBlink() { /*no support*/ } void blink() { /*no support*/ } From 95d9fb27375401e0da69391cddedd369ede25562 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:24:42 -0400 Subject: [PATCH 36/81] fixed buffer --- SSD1306Display.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 6f64a939..42d6be73 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -197,9 +197,19 @@ class SSD1306Display { } } - void print(int i) { print(sprintf("%d", i)); } - void print(float f) { print(sprintf("%f", f)); } - void print(int i, int base) { print(sprintf("%d", i, base)); } + void print(int i) { + char buf[10]; + print(sprintf(buf, "%d", i)); + } + void print(float f) { + char buf[100]; + print(sprintf(buf, "%f", f)); + } + + void print(int i, int base) { + char buf[10]; + print(sprintf(buf, "%d", i, base)); + } uint8_t type() { return LCD_I2C; } void noBlink() { /*no support*/ } From dda2ff9493257127997566380ceadcc82e87511f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:25:24 -0400 Subject: [PATCH 37/81] more define --- OpenSprinkler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 2ce9ae3f..9d063bf1 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2442,7 +2442,8 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { } #else -#define lcd_print_pgm(PSTR(str)) lcd.print(str) +#define PGSTR(s) s +#define lcd_print_pgm(str) lcd.print(str) // #define lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) lcd.print(str) #endif From c0cc3756dcaff0e223536ac47c21cfcf3d522366 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:20:31 -0400 Subject: [PATCH 38/81] Working lcd --- OpenSprinkler.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++---- OpenSprinkler.h | 6 ++++-- SSD1306Display.h | 41 ++++++++++++++++++++++++++++++++----- main.cpp | 2 +- 4 files changed, 89 insertions(+), 12 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 9d063bf1..4807fd7a 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -1694,8 +1694,9 @@ byte OpenSprinkler::weekday_today() { ulong wd = now_tz() / 86400L; return (wd+3) % 7; // Jan 1, 1970 is a Thursday #else - return 0; - // todo future: is this function needed for RPI/BBB? + time_t t = time(NULL); + struct tm *tm = localtime(&t); + return (tm->tm_wday+6) % 7; #endif } @@ -2440,11 +2441,48 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { } for(; (16-cnt) >= 0; cnt ++) lcd_print_pgm(PSTR(" ")); } - #else + +void OpenSprinkler::lcd_print_line_clear_pgm(const char *str, uint8_t line) { + lcd.setCursor(0, line); + uint8_t c; + int8_t cnt = 0; + while((c=*str++)!= '\0') { + lcd.print((char)c); + cnt++; + } + for(; (16-cnt) >= 0; cnt ++) lcd.print(' '); +} + #define PGSTR(s) s #define lcd_print_pgm(str) lcd.print(str) -// #define lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) lcd.print(str) +#define useEth 0 +#define HEX 16 + +int hour(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_hour; +} + +int minute(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_min; +} + +int second(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_sec; +} + +int day(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_mday; +} + +int month(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_mon+1; +} #endif void OpenSprinkler::lcd_print_2digit(int v) @@ -2459,6 +2497,7 @@ void OpenSprinkler::lcd_print_time(time_t t) #if defined(USE_SSD1306) lcd.setAutoDisplay(false); #endif +//TODO: Time lcd.setCursor(0, 0); lcd_print_2digit(hour(t)); @@ -2899,6 +2938,11 @@ void OpenSprinkler::lcd_set_brightness(byte value) { } #endif } + +#else +void OpenSprinkler::lcd_set_contrast() {} +void OpenSprinkler::lcd_set_brightness(byte value) {} + #endif // end of LCD and button functions #if defined(USE_SSD1306) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index e785cd50..4a0cf1e7 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -336,6 +336,8 @@ class OpenSprinkler { static void lcd_print_mac(const byte *mac); // print mac static void lcd_print_screen(char c); // print station bits of the board selected by display_board static void lcd_print_version(byte v); // print version number + static void lcd_set_brightness(byte value=1); + static void lcd_set_contrast(); #if defined(USE_SSD1306) static void flash_screen(); @@ -376,8 +378,6 @@ class OpenSprinkler { // -- UI functions -- static void ui_set_options(int oid); // ui for setting options (oid-> starting option index) - static void lcd_set_brightness(byte value=1); - static void lcd_set_contrast(); #if defined(ESP8266) static OTCConfig otc; @@ -395,6 +395,8 @@ class OpenSprinkler { static byte state; #endif +#else +static void lcd_print_line_clear_pgm(const char *str, byte line); #endif // LCD functions for Arduino private: diff --git a/SSD1306Display.h b/SSD1306Display.h index 42d6be73..e97b2b4b 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -84,6 +84,7 @@ class SSD1306Display : public SSD1306 { #include #include #include +#include #include "font.h" #include "images.h" @@ -198,17 +199,47 @@ class SSD1306Display { } void print(int i) { - char buf[10]; - print(sprintf(buf, "%d", i)); + char buf[100]; + snprintf(buf, 100, "%d", i); + print((const char *)buf); + } + + void print(unsigned int i) { + char buf[100]; + snprintf(buf, 100, "%u", i); + print((const char *)buf); } void print(float f) { char buf[100]; - print(sprintf(buf, "%f", f)); + snprintf(buf, 100, "%f", f); + print((const char *)buf); } + #define DEC 10 + #define HEX 16 + #define OCT 8 + #define BIN 2 + void print(int i, int base) { - char buf[10]; - print(sprintf(buf, "%d", i, base)); + char buf[100]; + switch (base) { + case DEC: + snprintf(buf, 100, "%d", i); + break; + case HEX: + snprintf(buf, 100, "%x", i); + break; + case OCT: + snprintf(buf, 100, "%o", i); + break; + case BIN: + snprintf(buf, 100, "%b", i); + break; + default: + snprintf(buf, 100, "%d", i); + break; + } + print((const char *)buf); } uint8_t type() { return LCD_I2C; } diff --git a/main.cpp b/main.cpp index 89f91cf0..5ab399a6 100644 --- a/main.cpp +++ b/main.cpp @@ -668,7 +668,7 @@ void do_loop() last_time = curr_time; if (os.button_timeout) os.button_timeout--; -#if defined(ARDUINO) +#if defined(USE_DISPLAY) if (!ui_state) os.lcd_print_time(curr_time); // print time #endif From d8a1d0b247f980a71891b1e1087efcacf99e7999 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 10:10:54 -0400 Subject: [PATCH 39/81] Compiling code --- OpenSprinkler.cpp | 41 ++++++++------------------------- OpenSprinkler.h | 26 +++++++++++---------- defines.h | 6 ++--- main.cpp | 58 ++++++++++------------------------------------- rpitime.h | 31 +++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 93 deletions(-) create mode 100644 rpitime.h diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 4807fd7a..e3bfb704 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2441,8 +2441,14 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { } for(; (16-cnt) >= 0; cnt ++) lcd_print_pgm(PSTR(" ")); } -#else +#else +void OpenSprinkler::lcd_print_pgm(const char *str) { + uint8_t c; + while((c=*str++)!= '\0') { + lcd.print((char)c); + } +} void OpenSprinkler::lcd_print_line_clear_pgm(const char *str, uint8_t line) { lcd.setCursor(0, line); uint8_t c; @@ -2455,34 +2461,6 @@ void OpenSprinkler::lcd_print_line_clear_pgm(const char *str, uint8_t line) { } #define PGSTR(s) s -#define lcd_print_pgm(str) lcd.print(str) -#define useEth 0 -#define HEX 16 - -int hour(time_t ct) { - struct tm *ti = gmtime(&ct); - return ti->tm_hour; -} - -int minute(time_t ct) { - struct tm *ti = gmtime(&ct); - return ti->tm_min; -} - -int second(time_t ct) { - struct tm *ti = gmtime(&ct); - return ti->tm_sec; -} - -int day(time_t ct) { - struct tm *ti = gmtime(&ct); - return ti->tm_mday; -} - -int month(time_t ct) { - struct tm *ti = gmtime(&ct); - return ti->tm_mon+1; -} #endif void OpenSprinkler::lcd_print_2digit(int v) @@ -2497,7 +2475,6 @@ void OpenSprinkler::lcd_print_time(time_t t) #if defined(USE_SSD1306) lcd.setAutoDisplay(false); #endif -//TODO: Time lcd.setCursor(0, 0); lcd_print_2digit(hour(t)); @@ -2778,8 +2755,6 @@ void OpenSprinkler::lcd_print_option(int i) { #endif -#if defined(ARDUINO) - /** Button functions */ /** wait for button */ byte OpenSprinkler::button_read_busy(byte pin_butt, byte waitmode, byte butt, byte is_holding) { @@ -2831,6 +2806,8 @@ byte OpenSprinkler::button_read(byte waitmode) return ret; } +#if defined(ARDUINO) + /** user interface for setting options during startup */ void OpenSprinkler::ui_set_options(int oid) { diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 4a0cf1e7..d2ce013a 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -60,6 +60,7 @@ #include #include #include "etherport.h" + #include "rpitime.h" #endif // end of headers #if defined(USE_LCD) @@ -110,6 +111,7 @@ extern bool useEth; #else extern EthernetServer *m_server; + extern bool useEth; #endif /** Non-volatile data structure */ @@ -344,16 +346,6 @@ class OpenSprinkler { static void toggle_screen_led(); static void set_screen_led(byte status); #endif -#endif - -#if defined(ARDUINO) // LCD functions for Arduino - #if defined(ESP8266) - static void lcd_print_pgm(PGM_P str); // ESP8266 does not allow PGM_P followed by PROGMEM - static void lcd_print_line_clear_pgm(PGM_P str, byte line); - #else - static void lcd_print_pgm(PGM_P PROGMEM str); // print a program memory string - static void lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line); - #endif static String time2str(uint32_t t) { uint16_t h = hour(t); @@ -378,6 +370,16 @@ class OpenSprinkler { // -- UI functions -- static void ui_set_options(int oid); // ui for setting options (oid-> starting option index) +#endif + +#if defined(ARDUINO) // LCD functions for Arduino + #if defined(ESP8266) + static void lcd_print_pgm(PGM_P str); // ESP8266 does not allow PGM_P followed by PROGMEM + static void lcd_print_line_clear_pgm(PGM_P str, byte line); + #else + static void lcd_print_pgm(PGM_P PROGMEM str); // print a program memory string + static void lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line); + #endif #if defined(ESP8266) static OTCConfig otc; @@ -396,6 +398,7 @@ class OpenSprinkler { #endif #else +static void lcd_print_pgm(const char *str); static void lcd_print_line_clear_pgm(const char *str, byte line); #endif // LCD functions for Arduino @@ -404,11 +407,10 @@ static void lcd_print_line_clear_pgm(const char *str, byte line); static void lcd_print_option(int i); // print an option to the lcd static void lcd_print_2digit(int v); // print a integer in 2 digits static void lcd_start(); + static byte button_read_busy(byte pin_butt, byte waitmode, byte butt, byte is_holding); #endif #if defined(ARDUINO) // LCD functions - static byte button_read_busy(byte pin_butt, byte waitmode, byte butt, byte is_holding); - #if defined(ESP8266) static void parse_otc_config(); static void latch_boost(); diff --git a/defines.h b/defines.h index f00afbf8..731433c8 100755 --- a/defines.h +++ b/defines.h @@ -420,9 +420,9 @@ enum { #define PIN_SENSOR1 14 #define PIN_SENSOR2 23 #define PIN_RFTX 15 // RF transmitter pin - //#define PIN_BUTTON_1 23 // button 1 - //#define PIN_BUTTON_2 24 // button 2 - //#define PIN_BUTTON_3 25 // button 3 + #define PIN_BUTTON_1 23 // button 1 + #define PIN_BUTTON_2 24 // button 2 + #define PIN_BUTTON_3 25 // button 3 #define PIN_FREE_LIST {5,6,7,8,9,10,11,12,13,16,18,19,20,21,23,24,25,26} // free GPIO pins #define ETHER_BUFFER_SIZE 16384 diff --git a/main.cpp b/main.cpp index 5ab399a6..0291c336 100644 --- a/main.cpp +++ b/main.cpp @@ -49,6 +49,7 @@ #else // header and defs for RPI/BBB EthernetServer *m_server = 0; EthernetClient *m_client = 0; + bool useEth = false; #endif #if defined(USE_SSD1306) @@ -113,7 +114,6 @@ void flow_poll() { /* End of RAH implementation of flow sensor */ } -#if defined(ARDUINO) // ====== UI defines ====== static char ui_anim_chars[3] = {'.', 'o', 'O'}; @@ -140,14 +140,13 @@ bool ui_confirm(PGM_P str) { } void ui_state_machine() { - // to avoid ui_state_machine taking too much computation time // we run it only every UI_STATE_MACHINE_INTERVAL ms static uint32_t last_usm = 0; if(millis() - last_usm <= UI_STATE_MACHINE_INTERVAL) { return; } last_usm = millis(); -#if defined(ESP8266) +#if defined(USE_SSD1306) // process screen led static ulong led_toggle_timeout = 0; if(led_blink_ms) { @@ -184,12 +183,16 @@ void ui_state_machine() { } else if (digitalReadExt(PIN_BUTTON_2)==0) { // if B2 is pressed while holding B1, display gateway IP os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); + #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.gatewayIP()); } else { os.lcd.print(WiFi.gatewayIP()); } #else { os.lcd.print(Ethernet.gatewayIP()); } #endif + #else + os.lcd.print("xxx.xxx.xxx.xxx"); + #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR("(gwip)")); ui_state = UI_STATE_DISP_IP; @@ -200,12 +203,16 @@ void ui_state_machine() { } else { // clicking B1: display device IP and port os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); + #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.localIP()); } else { os.lcd.print(WiFi.localIP()); } #else { os.lcd.print(Ethernet.localIP()); } #endif + #else + os.lcd.print("xxx.xxx.xxx.xxx"); + #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR(":")); uint16_t httpport = (uint16_t)(os.iopts[IOPT_HTTPPORT_1]<<8) + (uint16_t)os.iopts[IOPT_HTTPPORT_0]; @@ -293,9 +300,11 @@ void ui_state_machine() { } } + // ====================== // Setup Function // ====================== +#if defined(ARDUINO) void do_setup() { /* Clear WDT reset flag. */ #if defined(ESP8266) @@ -397,49 +406,6 @@ void do_setup() { os.status.req_mqtt_restart = true; } -// ====== UI defines ====== -static char ui_anim_chars[3] = {'.', 'o', 'O'}; - -#define UI_STATE_DEFAULT 0 -#define UI_STATE_DISP_IP 1 -#define UI_STATE_DISP_GW 2 -#define UI_STATE_RUNPROG 3 - -static byte ui_state = UI_STATE_DEFAULT; -static byte ui_state_runprog = 0; - -// bool ui_confirm(PGM_P str) { -// os.lcd_print_line_clear_pgm(str, 0); -// os.lcd_print_line_clear_pgm(PSTR("(B1:No, B3:Yes)"), 1); -// byte button; -// ulong start = millis(); -// do { -// button = os.button_read(BUTTON_WAIT_NONE); -// if((button&BUTTON_MASK)==BUTTON_3 && (button&BUTTON_FLAG_DOWN)) return true; -// if((button&BUTTON_MASK)==BUTTON_1 && (button&BUTTON_FLAG_DOWN)) return false; -// delay(10); -// } while(millis() - start < 2500); -// return false; -// } - -void ui_state_machine() { - - // to avoid ui_state_machine taking too much computation time - // we run it only every UI_STATE_MACHINE_INTERVAL ms - static uint32_t last_usm = 0; - if(millis() - last_usm <= UI_STATE_MACHINE_INTERVAL) { return; } - last_usm = millis(); - - // process screen led - static ulong led_toggle_timeout = 0; - if(led_blink_ms) { - if(millis()>led_toggle_timeout) { - os.toggle_screen_led(); - led_toggle_timeout = millis() + led_blink_ms; - } - } -} - #endif void turn_on_station(byte sid, ulong duration); diff --git a/rpitime.h b/rpitime.h new file mode 100644 index 00000000..49ff07e8 --- /dev/null +++ b/rpitime.h @@ -0,0 +1,31 @@ +#ifndef RPI_TIME_H +#define RPI_TIME_H + +#include + +static int hour(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_hour; +} + +static int minute(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_min; +} + +static int second(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_sec; +} + +static int day(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_mday; +} + +static int month(time_t ct) { + struct tm *ti = gmtime(&ct); + return ti->tm_mon+1; +} + +#endif \ No newline at end of file From d5b46bf056a96c4d7f55e69b2f3ae87f40eb2561 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 10:11:03 -0400 Subject: [PATCH 40/81] Fixed building with debug --- utils.cpp | 2 +- utils.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/utils.cpp b/utils.cpp index bde6ae3c..98b0f0c9 100644 --- a/utils.cpp +++ b/utils.cpp @@ -38,7 +38,7 @@ extern OpenSprinkler os; #else // RPI/BBB -static char* get_runtime_path() { +char* get_runtime_path() { static char path[PATH_MAX]; static byte query = 1; diff --git a/utils.h b/utils.h index c504f97a..171c24fe 100644 --- a/utils.h +++ b/utils.h @@ -81,6 +81,7 @@ void str2mac(const char *_str, byte mac[]); void initialiseEpoch(); #if defined(OSPI) unsigned int detect_rpi_rev(); + char* get_runtime_path(); #endif #endif From c99dfd76f5f5d9d814c6d78f32d00777cc384f7a Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:33:16 -0400 Subject: [PATCH 41/81] Working display and input --- OpenSprinkler.cpp | 37 +++++++++++++++++++++++++++++-------- SSD1306Display.h | 12 ++++++++---- defines.h | 8 ++++---- gpio.cpp | 1 + main.cpp | 14 ++++++++++++++ 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index e3bfb704..5ab67050 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -897,6 +897,12 @@ void OpenSprinkler::begin() { pinMode(PIN_SR_DATA, OUTPUT); #endif +#endif + +#if defined(OSPI) +pinModeExt(PIN_BUTTON_1, INPUT_PULLUP); +pinModeExt(PIN_BUTTON_2, INPUT_PULLUP); +pinModeExt(PIN_BUTTON_3, INPUT_PULLUP); #endif // Reset all stations @@ -2444,20 +2450,21 @@ void OpenSprinkler::lcd_print_line_clear_pgm(PGM_P PROGMEM str, byte line) { #else void OpenSprinkler::lcd_print_pgm(const char *str) { - uint8_t c; - while((c=*str++)!= '\0') { - lcd.print((char)c); - } + lcd.print(str); } void OpenSprinkler::lcd_print_line_clear_pgm(const char *str, uint8_t line) { - lcd.setCursor(0, line); + char buf[16]; uint8_t c; int8_t cnt = 0; - while((c=*str++)!= '\0') { - lcd.print((char)c); + while((c=*str++)!= '\0' && cnt<16) { + buf[cnt] = c; cnt++; } - for(; (16-cnt) >= 0; cnt ++) lcd.print(' '); + + for(int i=cnt; i<16; i++) buf[i] = ' '; + + lcd.setCursor(0, line); + lcd.print(buf); } #define PGSTR(s) s @@ -2504,6 +2511,7 @@ void OpenSprinkler::lcd_print_time(time_t t) void OpenSprinkler::lcd_print_ip(const byte *ip, byte endian) { #if defined(USE_SSD1306) lcd.clear(0, 1); + lcd.setAutoDisplay(false); #elif defined(USE_LCD) lcd.clear(); #endif @@ -2515,10 +2523,18 @@ void OpenSprinkler::lcd_print_ip(const byte *ip, byte endian) { lcd_print_pgm(PSTR(".")); } } + + #if defined(USE_SSD1306) + lcd.display(); + lcd.setAutoDisplay(true); + #endif } /** print mac address */ void OpenSprinkler::lcd_print_mac(const byte *mac) { + #if defined(USE_SSD1306) + lcd.setAutoDisplay(false); // reduce screen drawing time by turning off display() when drawing individual characters + #endif lcd.setCursor(0, 0); for(byte i=0; i<6; i++) { if(i) { @@ -2534,6 +2550,11 @@ void OpenSprinkler::lcd_print_mac(const byte *mac) { } else { lcd_print_pgm(PSTR(" (WiFi MAC)")); } + + #if defined(USE_SSD1306) + lcd.display(); + lcd.setAutoDisplay(true); + #endif } /** print station bits */ diff --git a/SSD1306Display.h b/SSD1306Display.h index e97b2b4b..07bdde02 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -192,10 +192,11 @@ class SSD1306Display { } void print(const char *s) { - while (*s) { - write(*s); - s++; - } + write(s); + } + + void print(char s) { + write(s); } void print(int i) { @@ -370,10 +371,13 @@ class SSD1306Display { uint8_t write(const char *s) { uint8_t nc = strlen(s); + bool temp_auto_display = auto_display; + auto_display = false; setColor(BLACK); fillRect(cx, cy, fontWidth * nc, fontHeight); setColor(WHITE); drawString(cx, cy, s); + auto_display = temp_auto_display; cx += fontWidth * nc; if (auto_display) display(); // todo: not very efficient return nc; diff --git a/defines.h b/defines.h index 731433c8..ef7be3a1 100755 --- a/defines.h +++ b/defines.h @@ -420,11 +420,11 @@ enum { #define PIN_SENSOR1 14 #define PIN_SENSOR2 23 #define PIN_RFTX 15 // RF transmitter pin - #define PIN_BUTTON_1 23 // button 1 - #define PIN_BUTTON_2 24 // button 2 - #define PIN_BUTTON_3 25 // button 3 + #define PIN_BUTTON_1 24 // button 1 + #define PIN_BUTTON_2 18 // button 2 + #define PIN_BUTTON_3 10 // button 3 - #define PIN_FREE_LIST {5,6,7,8,9,10,11,12,13,16,18,19,20,21,23,24,25,26} // free GPIO pins + #define PIN_FREE_LIST {5,6,7,8,9,11,12,13,16,19,20,21,23,25,26} // free GPIO pins #define ETHER_BUFFER_SIZE 16384 #define SDA 0 diff --git a/gpio.cpp b/gpio.cpp index aafc3a27..7f6723b5 100644 --- a/gpio.cpp +++ b/gpio.cpp @@ -188,6 +188,7 @@ byte digitalReadExt(byte pin) { #include #include #include +#include #define BUFFER_MAX 64 #define GPIO_MAX 64 diff --git a/main.cpp b/main.cpp index 0291c336..fbafdbe8 100644 --- a/main.cpp +++ b/main.cpp @@ -181,6 +181,9 @@ void ui_state_machine() { if(!ui_confirm(PSTR("Start 2s test?"))) {ui_state = UI_STATE_DEFAULT; break;} manual_start_program(255, 0); } else if (digitalReadExt(PIN_BUTTON_2)==0) { // if B2 is pressed while holding B1, display gateway IP + #if defined(USE_SSD1306) + os.lcd.setAutoDisplay(false); + #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); #if defined(Arduino) @@ -196,11 +199,18 @@ void ui_state_machine() { os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR("(gwip)")); ui_state = UI_STATE_DISP_IP; + #if defined(USE_SSD1306) + os.lcd.display(); + os.lcd.setAutoDisplay(true); + #endif } else { // if no other button is clicked, stop all zones if(!ui_confirm(PSTR("Stop all zones?"))) {ui_state = UI_STATE_DEFAULT; break;} reset_all_stations(); } } else { // clicking B1: display device IP and port + #if defined(USE_SSD1306) + os.lcd.setAutoDisplay(false); + #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); #if defined(Arduino) @@ -219,6 +229,10 @@ void ui_state_machine() { os.lcd.print(httpport); os.lcd_print_pgm(PSTR(" (ip:port)")); ui_state = UI_STATE_DISP_IP; + #if defined(USE_SSD1306) + os.lcd.display(); + os.lcd.setAutoDisplay(true); + #endif } break; case BUTTON_2: From 4a69a1eab5331877d0370ade6a1c30759f9d5197 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:33:06 -0400 Subject: [PATCH 42/81] update to use the bcm2835 and SSD1306_OLED_RPI lib --- SSD1306Display.cpp | 32 ---------------------- SSD1306Display.h | 67 +++++++++++++++++++++++----------------------- build.sh | 39 ++++++++++++++++++++++----- gpio.h | 4 +-- utils.cpp | 28 ------------------- utils.h | 4 +-- wiringPiI2C.h | 48 --------------------------------- 7 files changed, 69 insertions(+), 153 deletions(-) delete mode 100644 SSD1306Display.cpp delete mode 100644 wiringPiI2C.h diff --git a/SSD1306Display.cpp b/SSD1306Display.cpp deleted file mode 100644 index 7442255e..00000000 --- a/SSD1306Display.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#if defined(ESP8266) - -#elif defined(OSPI) - -#include "SSD1306Display.h" - -unsigned char init_command[] = { -#if 0 - // OLED_CMD_SET_HORI_ADDR_MODE - 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, - 0xD5, 0x80, 0xDA, 0x12, 0x81, 0xFF, - 0xA4, 0xDB, 0x40, 0x20, 0x00, 0x00, 0x10, 0x8D, - 0x14, 0x2E, 0xA6, 0xAF -#endif - -#if 1 - // OLED_CMD_SET_PAGE_ADDR_MODE - 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, 0xD5, - 0x80, 0xDA, 0x12, 0x81, 0xFF, 0xA4, 0xDB, 0x40, 0x20, - 0x02, 0x00, 0x10, 0x8D, 0x14, 0x2E, 0xA6, 0xAF -#endif -}; - -void init_display(int i2cd) { - int i; - for (i = 0; i < sizeof(init_command); i++) { - unsigned int control = 0x00; // Co = 0, D/C = 0 - wiringPiI2CWriteReg8(i2cd, control, init_command[i]); - } -} - -#endif \ No newline at end of file diff --git a/SSD1306Display.h b/SSD1306Display.h index 07bdde02..68093d40 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -83,8 +83,10 @@ class SSD1306Display : public SSD1306 { #elif defined(OSPI) #include #include -#include + +#include #include +#include "SSD1306_OLED.hpp" #include "font.h" #include "images.h" @@ -95,8 +97,6 @@ class SSD1306Display : public SSD1306 { #define BLACK 0 #define WHITE 1 -extern void init_display( int i2cd ); - // Header Values #define JUMPTABLE_BYTES 4 @@ -110,9 +110,9 @@ extern void init_display( int i2cd ); #define FIRST_CHAR_POS 2 #define CHAR_NUM_POS 3 -class SSD1306Display { +class SSD1306Display : public SSD1306 { public: - SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) { + SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) : SSD1306(128, 64) { cx = 0; cy = 0; for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) custom_chars[i] = 0; @@ -120,48 +120,47 @@ class SSD1306Display { int b; for (b = 0; b < 1024; b++) { frame[b] = 0x00; - } - - i2cd = wiringPiI2CSetup(_addr); - init_display(i2cd); + } } void init() {} // Dummy function to match ESP8266 - void begin() { - flipScreenVertically(); + + int begin() { + if (!OLEDSetBufferPtr(128, 64, frame, sizeof(frame))) return -1; + + const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_626; // bcm2835I2CClockDivider enum , see readme. + const uint8_t I2C_Address = 0x3C; + bool I2C_debug = false; + + // Check if Bcm28235 lib installed and print version. + if (!bcm2835_init()) { + printf("Error 1201: init bcm2835 library , Is it installed ?\r\n"); + return -1; + } + + // Turn on I2C bus (optionally it may already be on) + if (!OLED_I2C_ON()) { + printf("Error 1202: bcm2835_i2c_begin :Cannot start I2C, Running as root?\n"); + bcm2835_close(); // Close the library + return -1; + } + + OLEDbegin(I2C_Speed, I2C_Address, I2C_debug); // initialize the OLED setFont(Monospaced_plain_13); fontWidth = 8; fontHeight = 16; + + return 0; } void setFont(const uint8_t *f) { font = (uint8_t *)f; } - void flipScreenVertically() { - unsigned char command[3] = {0x00, 0x20, 0x00}; - for (int i = 0; i < sizeof(command); i++) { - wiringPiI2CWriteReg8(i2cd, 0x00, command[i]); - } - } - void display() { - wiringPiI2CWriteReg8(i2cd, 0x00, 0x21); // Column Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Column Start Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x7F); // Column End Address - - wiringPiI2CWriteReg8(i2cd, 0x00, 0x22); // Page Address - wiringPiI2CWriteReg8(i2cd, 0x00, 0x00); // Page Start Address - - for (int i = 0; i < 1024; i++) { - wiringPiI2CWriteReg8(i2cd, 0x40, frame[i]); - } + OLEDupdate(); } void clear() { - int b; - for (b = 0; b < 1024; b++) { - frame[b] = 0x00; - } - + OLEDclearBuffer(); display(); } @@ -353,7 +352,7 @@ class SSD1306Display { } } - uint8_t write(uint8_t c) { + size_t write(uint8_t c) { setColor(BLACK); fillRect(cx, cy, fontWidth, fontHeight); setColor(WHITE); diff --git a/build.sh b/build.sh index 87928fb9..2e5d61aa 100755 --- a/build.sh +++ b/build.sh @@ -18,6 +18,28 @@ function download_wiringpi { rm wiringpi.deb } +function install_bcm2835 { + echo "Installing bcm2835..." + curl -sL http://www.airspayce.com/mikem/bcm2835/bcm2835-1.75.tar.gz | tar xz + cd bcm2835-1.75 + ./configure + make + sudo make install + cd .. + rm -rf bcm2835-1.75 + +} + +function install_SSD1306_OLED_RPI { + echo "Installing SSD1306_OLED_RPI..." + curl -sL https://github.com/gavinlyonsrepo/SSD1306_OLED_RPI/archive/1.6.1.tar.gz | tar xz + cd SSD1306_OLED_RPI-1.6.1 + make + sudo make install + cd .. + rm -rf SSD1306_OLED_RPI-1.6.1 +} + function enable_i2c { sudo raspi-config nonint do_i2c 1 sudo dtparam i2c_baudrate=400000 @@ -36,18 +58,15 @@ echo "Building OpenSprinkler..." if [ "$1" == "demo" ]; then echo "Installing required libraries..." - - dpkg -i wiringpi_3.6_arm64.deb - rm wiringpi_3.6_arm64.deb apt-get install -y libmosquitto-dev download_wiringpi echo "Compiling demo firmware..." - g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto -lwiringPi + g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI elif [ "$1" == "osbo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev echo "Compiling osbo firmware..." - g++ -o OpenSprinkler -DOSBO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto + g++ -o OpenSprinkler -DOSBO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto else echo "Installing required libraries..." apt-get update @@ -63,6 +82,14 @@ else exit 0 fi + if [ ! -f /usr/local/lib/libbcm2835.a ]; then + install_bcm2835 + fi + + if [ ! -f /usr/lib/libSSD1306_OLED_RPI.so ]; then + install_SSD1306_OLED_RPI + fi + USEGPIO="" GPIOLIB="" @@ -74,7 +101,7 @@ else fi echo "Compiling ospi firmware..." - g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp SSD1306Display.cpp -lpthread -lmosquitto -lwiringPi $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB fi if [ ! "$SILENT" = true ] && [ -f OpenSprinkler.launch ] && [ ! -f /etc/init.d/OpenSprinkler.sh ]; then diff --git a/gpio.h b/gpio.h index 67f82566..a02dde0e 100644 --- a/gpio.h +++ b/gpio.h @@ -126,13 +126,13 @@ byte digitalReadExt(byte pin); #define INPUT 1 #if defined(OSPI) +#include #define INPUT_PULLUP 2 #else #define INPUT_PULLUP INPUT -#endif - #define HIGH 1 #define LOW 0 +#endif void pinMode(int pin, byte mode); void digitalWrite(int pin, byte value); diff --git a/utils.cpp b/utils.cpp index 98b0f0c9..565c7e79 100644 --- a/utils.cpp +++ b/utils.cpp @@ -86,16 +86,6 @@ char* get_filename_fullpath(const char *filename) { return fullpath; } -void delay(ulong howLong) -{ - struct timespec sleeper, dummy ; - - sleeper.tv_sec = (time_t)(howLong / 1000) ; - sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; - - nanosleep (&sleeper, &dummy) ; -} - void delayMicrosecondsHard (ulong howLong) { struct timeval tNow, tLong, tEnd ; @@ -109,24 +99,6 @@ void delayMicrosecondsHard (ulong howLong) gettimeofday (&tNow, NULL) ; } -void delayMicroseconds (ulong howLong) -{ - struct timespec sleeper ; - unsigned int uSecs = howLong % 1000000 ; - unsigned int wSecs = howLong / 1000000 ; - - /**/ if (howLong == 0) - return ; - else if (howLong < 100) - delayMicrosecondsHard (howLong) ; - else - { - sleeper.tv_sec = wSecs ; - sleeper.tv_nsec = (long)(uSecs * 1000L) ; - nanosleep (&sleeper, NULL) ; - } -} - static uint64_t epochMilli, epochMicro ; void initialiseEpoch() diff --git a/utils.h b/utils.h index 171c24fe..52cc2ac1 100644 --- a/utils.h +++ b/utils.h @@ -30,7 +30,7 @@ #include #include #include - + #include #endif #include "defines.h" @@ -73,8 +73,6 @@ void str2mac(const char *_str, byte mac[]); const char* get_data_dir(); void set_data_dir(const char *new_data_dir); char* get_filename_fullpath(const char *filename); - void delay(ulong ms); - void delayMicroseconds(ulong us); void delayMicrosecondsHard(ulong us); ulong millis(); ulong micros(); diff --git a/wiringPiI2C.h b/wiringPiI2C.h deleted file mode 100644 index a7b0804f..00000000 --- a/wiringPiI2C.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * wiringPiI2C.h: - * Simplified I2C access routines - * Copyright (c) 2013-2024 Gordon Henderson and contributors - *********************************************************************** - * This file is part of wiringPi: - * https://github.com/WiringPi/WiringPi/ - * - * wiringPi is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * wiringPi is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with wiringPi. - * If not, see . - *********************************************************************** - */ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -extern int wiringPiI2CRead (int fd) ; -extern int wiringPiI2CReadReg8 (int fd, int reg) ; -extern int wiringPiI2CReadReg16 (int fd, int reg) ; -extern int wiringPiI2CReadBlockData (int fd, int reg, uint8_t *values, uint8_t size); //Interface 3.3 -extern int wiringPiI2CRawRead (int fd, uint8_t *values, uint8_t size); //Interface 3.3 - -extern int wiringPiI2CWrite (int fd, int data) ; -extern int wiringPiI2CWriteReg8 (int fd, int reg, int data) ; -extern int wiringPiI2CWriteReg16 (int fd, int reg, int data) ; -extern int wiringPiI2CWriteBlockData (int fd, int reg, const uint8_t *values, uint8_t size); //Interface 3.3 -extern int wiringPiI2CRawWrite (int fd, const uint8_t *values, uint8_t size); //Interface 3.3 - -extern int wiringPiI2CSetupInterface (const char *device, int devId) ; -extern int wiringPiI2CSetup (const int devId) ; - -#ifdef __cplusplus -} -#endif \ No newline at end of file From c1f6fd23583a7e2bd7845ca826fcf5c608d976aa Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:37:10 -0400 Subject: [PATCH 43/81] faster oled --- SSD1306Display.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 68093d40..c29ea44e 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -128,7 +128,7 @@ class SSD1306Display : public SSD1306 { int begin() { if (!OLEDSetBufferPtr(128, 64, frame, sizeof(frame))) return -1; - const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_626; // bcm2835I2CClockDivider enum , see readme. + const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_148; const uint8_t I2C_Address = 0x3C; bool I2C_debug = false; From 0af86b030414317da20b17417074075455f6b9e5 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:45:48 -0400 Subject: [PATCH 44/81] check if wiringpi is installed already --- build.sh | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/build.sh b/build.sh index 2e5d61aa..0978066f 100755 --- a/build.sh +++ b/build.sh @@ -59,7 +59,19 @@ echo "Building OpenSprinkler..." if [ "$1" == "demo" ]; then echo "Installing required libraries..." apt-get install -y libmosquitto-dev - download_wiringpi + + if [ ! -f /usr/lib/libwiringPi.so ]; then + download_wiringpi + fi + + if [ ! -f /usr/local/lib/libbcm2835.a ]; then + install_bcm2835 + fi + + if [ ! -f /usr/lib/libSSD1306_OLED_RPI.so ]; then + install_SSD1306_OLED_RPI + fi + echo "Compiling demo firmware..." g++ -o OpenSprinkler -DDEMO -std=c++14 main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp etherport.cpp mqtt.cpp -lpthread -lmosquitto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI elif [ "$1" == "osbo" ]; then @@ -70,18 +82,18 @@ elif [ "$1" == "osbo" ]; then else echo "Installing required libraries..." apt-get update - apt-get install -y libmosquitto-dev - apt-get install -y raspi-gpio - apt-get install -y libi2c-dev - apt-get install -y libssl-dev - apt-get install -y libgpiod-dev - download_wiringpi + apt-get install -y libmosquitto-dev raspi-gpio libi2c-dev libssl-dev libgpiod-dev + if ! command -v raspi-gpio &> /dev/null then echo "Command raspi-gpio is required and is not installed" exit 0 fi + if [ ! -f /usr/lib/libwiringPi.so ]; then + download_wiringpi + fi + if [ ! -f /usr/local/lib/libbcm2835.a ]; then install_bcm2835 fi From 5dd6d35e4d8362db94a4457091306c708c38c39d Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:06:16 -0400 Subject: [PATCH 45/81] added brightness --- OpenSprinkler.cpp | 9 ++++----- SSD1306Display.h | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 5ab67050..748258f9 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2894,6 +2894,9 @@ void OpenSprinkler::ui_set_options(int oid) lcd.noBlink(); } +#else +#endif // end of LCD and button functions + /** Set LCD contrast (using PWM) */ void OpenSprinkler::lcd_set_contrast() { #ifdef PIN_LCD_CONTRAST @@ -2928,7 +2931,7 @@ void OpenSprinkler::lcd_set_brightness(byte value) { } } -#elif defined(ESP8266) +#elif defined(USE_SSD1306) if (value) {lcd.displayOn();lcd.setBrightness(255); } else { if(iopts[IOPT_LCD_DIMMING]==0) lcd.displayOff(); @@ -2937,11 +2940,7 @@ void OpenSprinkler::lcd_set_brightness(byte value) { #endif } -#else -void OpenSprinkler::lcd_set_contrast() {} -void OpenSprinkler::lcd_set_brightness(byte value) {} -#endif // end of LCD and button functions #if defined(USE_SSD1306) #include "images.h" diff --git a/SSD1306Display.h b/SSD1306Display.h index c29ea44e..fae953dc 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -164,6 +164,10 @@ class SSD1306Display : public SSD1306 { display(); } + void setBrightness(uint8_t brightness) { + OLEDContrast(brightness); + } + void setColor(uint8_t color) { this->color = color; } void drawPixel(uint8_t x, uint8_t y) { From a90eb410f19879b9aa922d35824674b59e463f23 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:09:33 -0400 Subject: [PATCH 46/81] added display on/off --- SSD1306Display.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SSD1306Display.h b/SSD1306Display.h index fae953dc..df3a54d1 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -168,6 +168,14 @@ class SSD1306Display : public SSD1306 { OLEDContrast(brightness); } + void displayOn() { + OLEDEnable(1); + } + + void displayOff() { + OLEDEnable(0); + } + void setColor(uint8_t color) { this->color = color; } void drawPixel(uint8_t x, uint8_t y) { From fde7b2b750b50a81574f26e96dab47d3ac3be3e1 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:19:24 -0400 Subject: [PATCH 47/81] added ip display --- main.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/main.cpp b/main.cpp index fbafdbe8..c9bc199e 100644 --- a/main.cpp +++ b/main.cpp @@ -186,16 +186,12 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); - #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.gatewayIP()); } else { os.lcd.print(WiFi.gatewayIP()); } #else { os.lcd.print(Ethernet.gatewayIP()); } #endif - #else - os.lcd.print("xxx.xxx.xxx.xxx"); - #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR("(gwip)")); ui_state = UI_STATE_DISP_IP; @@ -213,16 +209,12 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); - #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.localIP()); } else { os.lcd.print(WiFi.localIP()); } #else { os.lcd.print(Ethernet.localIP()); } #endif - #else - os.lcd.print("xxx.xxx.xxx.xxx"); - #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR(":")); uint16_t httpport = (uint16_t)(os.iopts[IOPT_HTTPPORT_1]<<8) + (uint16_t)os.iopts[IOPT_HTTPPORT_0]; From 276664b6f81e88857348517f421e62a4d6bee037 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:58:38 -0400 Subject: [PATCH 48/81] added ip address to rpi screen --- main.cpp | 17 +++++++++++++++++ utils.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 13 +++++++++++++ 3 files changed, 80 insertions(+) diff --git a/main.cpp b/main.cpp index c9bc199e..b8e6b9d6 100644 --- a/main.cpp +++ b/main.cpp @@ -186,12 +186,20 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); + #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.gatewayIP()); } else { os.lcd.print(WiFi.gatewayIP()); } #else { os.lcd.print(Ethernet.gatewayIP()); } #endif + #else + route_t route = get_route(); + char str[INET_ADDRSTRLEN]; + + inet_ntop(AF_INET, &(route.gateway), str, INET_ADDRSTRLEN); + os.lcd.print(str); + #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR("(gwip)")); ui_state = UI_STATE_DISP_IP; @@ -209,12 +217,21 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); + #if defined(Arduino) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.localIP()); } else { os.lcd.print(WiFi.localIP()); } #else { os.lcd.print(Ethernet.localIP()); } #endif + #else + route_t route = get_route(); + char str[INET_ADDRSTRLEN]; + in_addr_t ip = get_ip_address(route.iface); + + inet_ntop(AF_INET, &ip, str, INET_ADDRSTRLEN); + os.lcd.print(str); + #endif os.lcd.setCursor(0, 1); os.lcd_print_pgm(PSTR(":")); uint16_t httpport = (uint16_t)(os.iopts[IOPT_HTTPPORT_1]<<8) + (uint16_t)os.iopts[IOPT_HTTPPORT_0]; diff --git a/utils.cpp b/utils.cpp index 565c7e79..afa8d95e 100644 --- a/utils.cpp +++ b/utils.cpp @@ -155,6 +155,56 @@ unsigned int detect_rpi_rev() { } return rev; } + +route_t get_route() { + route_t route; + char iface[16]; + unsigned long dst, gw; + unsigned int flags, refcnt, use, metric, mask, mtu, window, irtt; + + FILE *filp; + char buf[512]; + char term; + filp = fopen("/proc/net/route", "r"); + if(filp) { + while(fgets(buf, sizeof(buf), filp) != NULL) { + if(sscanf(buf, "%s %lx %lx %X %d %d %d %lx %d %d %d", iface, &dst, &gw, &flags, &refcnt, &use, &metric, &mask, &mtu, &window, &irtt) == 11) { + if(flags & RTF_UP) { + if(dst==0) { + strcpy(route.iface, iface); + route.gateway = gw; + route.destination = dst; + } + } + } + } + fclose(filp); + } + return route; +} + +in_addr_t get_ip_address(char *iface) { + struct ifaddrs *ifaddr; + struct ifaddrs *ifa; + in_addr_t ip = 0; + if(getifaddrs(&ifaddr) == -1) { + return 0; + } + + ifa = ifaddr; + + while(ifa) { + if(ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { + if(strcmp(ifa->ifa_name, iface)==0) { + ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; + break; + } + } + ifa = ifa->ifa_next; + } + freeifaddrs(ifaddr); + return ip; +} #endif #endif diff --git a/utils.h b/utils.h index 52cc2ac1..6576f4bc 100644 --- a/utils.h +++ b/utils.h @@ -31,6 +31,10 @@ #include #include #include + #include + #include + #include + #include #endif #include "defines.h" @@ -80,6 +84,15 @@ void str2mac(const char *_str, byte mac[]); #if defined(OSPI) unsigned int detect_rpi_rev(); char* get_runtime_path(); + + struct route_t { + char iface[16]; + in_addr_t gateway; + in_addr_t destination; + }; + + route_t get_route(); + in_addr_t get_ip_address(char *iface); #endif #endif From 1da105d36491817b7917770d444e975a4d8680f6 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:46:08 -0400 Subject: [PATCH 49/81] changed led blink --- main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.cpp b/main.cpp index b8e6b9d6..a6aa916b 100644 --- a/main.cpp +++ b/main.cpp @@ -53,7 +53,11 @@ #endif #if defined(USE_SSD1306) + #if defined(ESP8266) static uint16_t led_blink_ms = LED_FAST_BLINK; + #else + static uint16_t led_blink_ms = 0; + #endif #endif void push_message(int type, uint32_t lval=0, float fval=0.f, const char* sval=NULL); From 45ce24cec219ba3d5c4f511c868aebd9db0251a6 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:41:36 -0400 Subject: [PATCH 50/81] update build --- build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index 80940e27..11ec5803 100755 --- a/build.sh +++ b/build.sh @@ -93,7 +93,7 @@ else echo "Installing required libraries..." apt-get update apt-get install -y libmosquitto-dev raspi-gpio libi2c-dev libssl-dev libgpiod-dev - + if ! command -v raspi-gpio &> /dev/null; then echo "Command raspi-gpio is required and is not installed" exit 0 @@ -125,7 +125,7 @@ else ws=$(ls external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) otf=$(ls external/OpenThings-Framework-Firmware-Library/*.cpp) - g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c SSD1306Display.cpp -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB fi if [ -f /etc/init.d/OpenSprinkler.sh ]; then From 0bc40b6301de4d330275720a8c4cb62c91413a41 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:44:53 -0400 Subject: [PATCH 51/81] fix make --- Makefile | 2 +- build.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 776a283b..6c48ca42 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LD=$(CXX) LIBS=pthread mosquitto ssl crypto LDFLAGS=$(addprefix -l,$(LIBS)) BINARY=OpenSprinkler -SOURCES=main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c SSD1306Display.cpp $(wildcard external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) $(wildcard external/OpenThings-Framework-Firmware-Library/*.cpp) +SOURCES=main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c $(wildcard external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) $(wildcard external/OpenThings-Framework-Firmware-Library/*.cpp) HEADERS=$(wildcard *.h) $(wildcard *.hpp) OBJECTS=$(addsuffix .o,$(basename $(SOURCES))) diff --git a/build.sh b/build.sh index 11ec5803..aef2bcab 100755 --- a/build.sh +++ b/build.sh @@ -125,7 +125,7 @@ else ws=$(ls external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) otf=$(ls external/OpenThings-Framework-Firmware-Library/*.cpp) - g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c SSD1306Display.cpp -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB fi if [ -f /etc/init.d/OpenSprinkler.sh ]; then From 402986e825083824f8ab5266158c325cb1ffbeb1 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:46:54 -0400 Subject: [PATCH 52/81] added rpi time library --- OpenSprinkler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 8aed7429..4d28706a 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -30,6 +30,7 @@ #include "utils.h" #include "gpio.h" #include "images.h" +#include "rpitime.h" #include "mqtt.h" #if defined(ARDUINO) // headers for Arduino From 5d54f43918b2dee32afb29ce9b02474b5737a13e Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:48:15 -0400 Subject: [PATCH 53/81] fixed header def --- OpenSprinkler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 4d28706a..d3541eb6 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -420,7 +420,7 @@ class OpenSprinkler { #else static void lcd_print_pgm(const char *str); -static void lcd_print_line_clear_pgm(const char *str, byte line); +static void lcd_print_line_clear_pgm(const char *str, unsigned char line); #endif // LCD functions for Arduino private: From 70ce4cf9bc3c09683e32aea0582eb18dc8f7c97d Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:52:50 -0400 Subject: [PATCH 54/81] fix if in header --- OpenSprinkler.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/OpenSprinkler.h b/OpenSprinkler.h index d3541eb6..6605d242 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -424,14 +424,14 @@ static void lcd_print_line_clear_pgm(const char *str, unsigned char line); #endif // LCD functions for Arduino private: -#if defined(USE_DISPLAY) +#if defined(USE_DISPLAY) // LCD functions static void lcd_print_option(int i); // print an option to the lcd static void lcd_print_2digit(int v); // print a integer in 2 digits static void lcd_start(); static unsigned char button_read_busy(unsigned char pin_butt, unsigned char waitmode, unsigned char butt, unsigned char is_holding); +#endif // LCD functions -#if defined(ARDUINO) // LCD functions - #if defined(ESP8266) +#if defined(ESP8266) static void latch_boost(); static void latch_open(unsigned char sid); static void latch_close(unsigned char sid); @@ -441,9 +441,8 @@ static void lcd_print_line_clear_pgm(const char *str, unsigned char line); static void latch_setzoneoutput_v2(unsigned char sid, unsigned char A, unsigned char K); static void latch_apply_all_station_bits(); static unsigned char prev_station_bits[]; - #endif -#endif // LCD functions - static unsigned char engage_booster; +#endif + static unsigned char engage_booster; #if defined(USE_OTF) static void parse_otc_config(); From a0cc92049539eb01b14b7a360a2698c9b6dafde8 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:56:19 -0400 Subject: [PATCH 55/81] update funcitons for pi --- OpenSprinkler.cpp | 4 ++++ main.cpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 58fdc5bb..0eb3afc3 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2688,11 +2688,15 @@ void OpenSprinkler::lcd_print_mac(const unsigned char *mac) { lcd.print((mac[i]&0x0F), HEX); if(i==4) lcd.setCursor(0, 1); } + #if defined(ARDUINO) if(useEth) { lcd_print_pgm(PSTR(" (Ether MAC)")); } else { lcd_print_pgm(PSTR(" (WiFi MAC)")); } + #else + lcd_print_pgm(PSTR(" (MAC)")); + #endif #if defined(USE_SSD1306) lcd.display(); diff --git a/main.cpp b/main.cpp index b4d07785..fb17fece 100644 --- a/main.cpp +++ b/main.cpp @@ -53,7 +53,7 @@ #endif unsigned long getNtpTime(); #else // header and defs for RPI/BBB - +bool useEth = false; #endif #if defined(USE_OTF) From 22d6a22760e3fb6ee84c24702a279355a459f702 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:07:35 -0400 Subject: [PATCH 56/81] added ui state machine --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index fb17fece..4a9056ab 100644 --- a/main.cpp +++ b/main.cpp @@ -629,6 +629,7 @@ void do_loop() #else // Process Ethernet packets for RPI/BBB if(otf) otf->loop(); + ui_state_machine(); #endif // Process Ethernet packets // Start up MQTT when we have a network connection From ceebf2196dce237b422f2f29e730c95d23b34ca0 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 10:02:55 -0400 Subject: [PATCH 57/81] add system idle --- OpenSprinkler.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 0eb3afc3..6e9fd2c7 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2794,9 +2794,12 @@ void OpenSprinkler::lcd_print_screen(char c) { lcd.write(status.network_fails>2?ICON_ETHER_DISCONNECTED:ICON_ETHER_CONNECTED); // if network failure detection is more than 2, display disconnect icon #endif -#if defined(ESP8266) - - if(useEth || (get_wifi_mode()==WIFI_MODE_STA && WiFi.status()==WL_CONNECTED && WiFi.localIP())) { +#if defined(USE_SSD1306) + #if defined(ESP8266) + if(useEth || (get_wifi_mode()==WIFI_MODE_STA && WiFi.status()==WL_CONNECTED && WiFi.localIP())) { + #else + { + #endif lcd.setCursor(0, -1); if(status.rain_delayed) { lcd.print(F(" ")); @@ -2808,17 +2811,21 @@ void OpenSprinkler::lcd_print_screen(char c) { lcd.print(F(" (System Idle) ")); } + #if defined(ESP8266) lcd.setCursor(2, 2); if(status.program_busy && !status.pause_state) { //lcd.print(F("Curr: ")); lcd.print(read_current()); lcd.print(F(" mA ")); } else { + #else + { + #endif lcd.clear(2, 2); } } -#endif -#if defined(USE_SSD1306) + + lcd.display(); lcd.setAutoDisplay(true); #endif From 5db69b48106799d36ad4635728d9d457a067a13a Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 10:54:56 -0400 Subject: [PATCH 58/81] update build script to prepare for switching to libi2c --- Makefile | 2 +- SSD1306Display.h | 2 -- build.sh | 32 +------------------------------- 3 files changed, 2 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 6c48ca42..7bc1b3ef 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CXX=g++ VERSION=OSPI CXXFLAGS=-std=gnu++14 -D$(VERSION) -DSMTP_OPENSSL -Wall -include string.h -Iexternal/TinyWebsockets/tiny_websockets_lib/include -Iexternal/OpenThings-Framework-Firmware-Library/ LD=$(CXX) -LIBS=pthread mosquitto ssl crypto +LIBS=pthread mosquitto ssl crypto i2c LDFLAGS=$(addprefix -l,$(LIBS)) BINARY=OpenSprinkler SOURCES=main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c $(wildcard external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) $(wildcard external/OpenThings-Framework-Firmware-Library/*.cpp) diff --git a/SSD1306Display.h b/SSD1306Display.h index ad0e8c6c..fe2c04ac 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -84,9 +84,7 @@ class SSD1306Display : public SSD1306{ #include #include -#include #include -#include "SSD1306_OLED.hpp" #include "font.h" #include "images.h" diff --git a/build.sh b/build.sh index aef2bcab..6a9d3a54 100755 --- a/build.sh +++ b/build.sh @@ -19,28 +19,6 @@ function download_wiringpi { rm wiringpi.deb } -function install_bcm2835 { - echo "Installing bcm2835..." - curl -sL http://www.airspayce.com/mikem/bcm2835/bcm2835-1.75.tar.gz | tar xz - cd bcm2835-1.75 - ./configure - make - sudo make install - cd .. - rm -rf bcm2835-1.75 - -} - -function install_SSD1306_OLED_RPI { - echo "Installing SSD1306_OLED_RPI..." - curl -sL https://github.com/gavinlyonsrepo/SSD1306_OLED_RPI/archive/1.6.1.tar.gz | tar xz - cd SSD1306_OLED_RPI-1.6.1 - make - sudo make install - cd .. - rm -rf SSD1306_OLED_RPI-1.6.1 -} - function enable_i2c { sudo raspi-config nonint do_i2c 1 sudo dtparam i2c_baudrate=400000 @@ -103,14 +81,6 @@ else download_wiringpi fi - if [ ! -f /usr/local/lib/libbcm2835.a ]; then - install_bcm2835 - fi - - if [ ! -f /usr/lib/libSSD1306_OLED_RPI.so ]; then - install_SSD1306_OLED_RPI - fi - USEGPIO="" GPIOLIB="" @@ -125,7 +95,7 @@ else ws=$(ls external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) otf=$(ls external/OpenThings-Framework-Firmware-Library/*.cpp) - g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -lbcm2835 -lrt -lSSD1306_OLED_RPI $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -li2c $GPIOLIB fi if [ -f /etc/init.d/OpenSprinkler.sh ]; then From d5b741ef6299b9cadcaaa182c4ff14de215e8441 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:00:53 -0400 Subject: [PATCH 59/81] switch to i2cdev for ssd1306 --- SSD1306Display.h | 201 ++++++++++++++++++++++++++++++++++++++++------- gpio.h | 4 +- utils.cpp | 28 +++++++ utils.h | 3 +- 4 files changed, 204 insertions(+), 32 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index fe2c04ac..dfac9426 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -80,14 +80,24 @@ class SSD1306Display : public SSD1306{ PGM_P custom_chars[NUM_CUSTOM_ICONS]; }; -#elif defined(OSPI) +#else #include #include #include +#include + + +extern "C" { + #include + #include +} #include "font.h" #include "images.h" +#include +#include +#include #define LCD_STD 0 // Standard LCD #define LCD_I2C 1 @@ -108,45 +118,96 @@ class SSD1306Display : public SSD1306{ #define FIRST_CHAR_POS 2 #define CHAR_NUM_POS 3 -class SSD1306Display : public SSD1306 { +class SSD1306Display { public: - SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) : SSD1306(128, 64) { + SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) { cx = 0; cy = 0; for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) custom_chars[i] = 0; - int b; - for (b = 0; b < 1024; b++) { - frame[b] = 0x00; - } + clear_buffer(); + } + + ~SSD1306Display() { + displayOff(); + close(file); } void init() {} // Dummy function to match ESP8266 int begin() { - if (!OLEDSetBufferPtr(128, 64, frame, sizeof(frame))) return -1; + // if (!OLEDSetBufferPtr(128, 64, frame, sizeof(frame))) return -1; + + // const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_148; + // const uint8_t I2C_Address = 0x3C; + // bool I2C_debug = false; + + // // Check if Bcm28235 lib installed and print version. + // if (!bcm2835_init()) { + // printf("Error 1201: init bcm2835 library , Is it installed ?\r\n"); + // return -1; + // } + + // // Turn on I2C bus (optionally it may already be on) + // if (!OLED_I2C_ON()) { + // printf("Error 1202: bcm2835_i2c_begin :Cannot start I2C, Running as root?\n"); + // bcm2835_close(); // Close the library + // return -1; + // } + + // OLEDbegin(I2C_Speed, I2C_Address, I2C_debug); // initialize the OLED + setFont(Monospaced_plain_13); + fontWidth = 8; + fontHeight = 16; - const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_148; - const uint8_t I2C_Address = 0x3C; - bool I2C_debug = false; + int adapter_nr = 1; /* probably dynamically determined */ + char filename[20]; - // Check if Bcm28235 lib installed and print version. - if (!bcm2835_init()) { - printf("Error 1201: init bcm2835 library , Is it installed ?\r\n"); - return -1; + snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); + file = open(filename, O_RDWR); + if (file < 0) { + /* ERROR HANDLING; you can check errno to see what went wrong */ + exit(1); } - // Turn on I2C bus (optionally it may already be on) - if (!OLED_I2C_ON()) { - printf("Error 1202: bcm2835_i2c_begin :Cannot start I2C, Running as root?\n"); - bcm2835_close(); // Close the library - return -1; + int addr = 0x3C; /* The I2C address */ + + if (ioctl(file, I2C_SLAVE, addr) < 0) { + /* ERROR HANDLING; you can check errno to see what went wrong */ + exit(1); } - OLEDbegin(I2C_Speed, I2C_Address, I2C_debug); // initialize the OLED - setFont(Monospaced_plain_13); - fontWidth = 8; - fontHeight = 16; + i2c_begin_transaction(0x00); + ssd1306_command(0xAE); + ssd1306_command(0xD5); + ssd1306_command(0x80); + ssd1306_command(0xA8); + unsigned char height = 64; + ssd1306_command(height-1); + ssd1306_command(0xD3); + ssd1306_command(0x00); + ssd1306_command(0x40); + ssd1306_command(0x8D); + ssd1306_command(0x14); + ssd1306_command(0x20); + ssd1306_command(0x00); + ssd1306_command(0xA1); + ssd1306_command(0xC8); + ssd1306_command(0xDA); + ssd1306_command(0x12); + ssd1306_command(0x81); + ssd1306_command(0xCF); + ssd1306_command(0xd9); + ssd1306_command(0xF1); + + ssd1306_command(0xDB); + ssd1306_command(0x40); + ssd1306_command(0xA4); + ssd1306_command(0xA6); + ssd1306_command(0x2E); + ssd1306_command(0xAF); + + i2c_end_transaction(); return 0; } @@ -154,24 +215,43 @@ class SSD1306Display : public SSD1306 { void setFont(const uint8_t *f) { font = (uint8_t *)f; } void display() { - OLEDupdate(); + i2c_begin_transaction(0x00); + ssd1306_command(0x22); + ssd1306_command(0x00); + ssd1306_command(0xFF); + ssd1306_command(0x21); + ssd1306_command(0x00); + + int width = 128; + ssd1306_command(width - 1); // Column end address + i2c_end_transaction(); + + i2c_begin_transaction(0x40); + + int b; + for (b = 0; b < 1024; b++) { + ssd1306_data(frame[b]); + } + + i2c_end_transaction(); } void clear() { - OLEDclearBuffer(); + clear_buffer(); display(); } void setBrightness(uint8_t brightness) { - OLEDContrast(brightness); + ssd1306_command(0x81); + ssd1306_command(brightness); } void displayOn() { - OLEDEnable(1); + ssd1306_command(0xAF); } void displayOff() { - OLEDEnable(0); + ssd1306_command(0xAE); } void setColor(uint8_t color) { this->color = color; } @@ -398,6 +478,7 @@ class SSD1306Display : public SSD1306 { void setAutoDisplay(bool v) { auto_display = v; } private: + int file = -1; bool auto_display = false; uint8_t cx, cy = 0; uint8_t fontWidth, fontHeight; @@ -406,6 +487,68 @@ class SSD1306Display : public SSD1306 { int i2cd; bool color; uint8_t *font; + + void clear_buffer() { + memset(frame, 0x00, sizeof(frame)); + } + + bool transaction = false; + unsigned char transaction_id = 0; + unsigned char transaction_buffer[32]; + unsigned char transaction_buffer_length = 0; + + int i2c_begin_transaction(unsigned char id) { + if (transaction) { + return -1; + } else { + transaction_id = id; + transaction = true; + memset(transaction_buffer, 0x00, sizeof(transaction_buffer)); + transaction_buffer_length = 0; + return 0; + } + } + + int i2c_send_transaction() { + return i2c_smbus_write_i2c_block_data(file, transaction_id, transaction_buffer_length, transaction_buffer); + } + + int i2c_end_transaction() { + if (transaction) { + transaction = false; + return i2c_send_transaction(); + } else { + return -1; + } + } + + int i2c_send(unsigned char reg, unsigned char data) { + if (transaction) { + if (reg != transaction_id) { + return -1; + } + + int res = 0; + if (transaction_buffer_length >= sizeof(transaction_buffer)) { + res = i2c_send_transaction(); + transaction_buffer_length = 0; + } + + transaction_buffer[transaction_buffer_length] = data; + transaction_buffer_length++; + return res; + } else { + return i2c_smbus_write_byte_data(file, reg, data); + } + } + + int ssd1306_command(unsigned char command) { + return i2c_send(0x00, command); + } + + int ssd1306_data(unsigned char value) { + return i2c_send(0x40, value); + } }; #endif diff --git a/gpio.h b/gpio.h index 9ec95a5c..843c7f59 100644 --- a/gpio.h +++ b/gpio.h @@ -126,13 +126,13 @@ unsigned char digitalReadExt(unsigned char pin); #define INPUT 1 #if defined(OSPI) -#include #define INPUT_PULLUP 2 #else #define INPUT_PULLUP INPUT +#endif + #define HIGH 1 #define LOW 0 -#endif void pinMode(int pin, unsigned char mode); void digitalWrite(int pin, unsigned char value); diff --git a/utils.cpp b/utils.cpp index e303401a..caad3f3d 100644 --- a/utils.cpp +++ b/utils.cpp @@ -87,6 +87,34 @@ char* get_filename_fullpath(const char *filename) { return fullpath; } +void delay(ulong howLong) +{ + struct timespec sleeper, dummy ; + + sleeper.tv_sec = (time_os_t)(howLong / 1000) ; + sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; + + nanosleep (&sleeper, &dummy) ; +} + +void delayMicroseconds (ulong howLong) +{ + struct timespec sleeper ; + unsigned int uSecs = howLong % 1000000 ; + unsigned int wSecs = howLong / 1000000 ; + + /**/ if (howLong == 0) + return ; + else if (howLong < 100) + delayMicrosecondsHard (howLong) ; + else + { + sleeper.tv_sec = wSecs ; + sleeper.tv_nsec = (long)(uSecs * 1000L) ; + nanosleep (&sleeper, NULL) ; + } +} + void delayMicrosecondsHard (ulong howLong) { struct timeval tNow, tLong, tEnd ; diff --git a/utils.h b/utils.h index f4fb561c..7fd21821 100644 --- a/utils.h +++ b/utils.h @@ -30,7 +30,6 @@ #include #include #include - #include #include #include #include @@ -77,6 +76,8 @@ void str2mac(const char *_str, unsigned char mac[]); const char* get_data_dir(); void set_data_dir(const char *new_data_dir); char* get_filename_fullpath(const char *filename); + void delay(ulong ms); + void delayMicroseconds(ulong us); void delayMicrosecondsHard(ulong us); ulong millis(); ulong micros(); From 85e6c178cd5659656530d212204f6287c301c387 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:11:07 -0400 Subject: [PATCH 60/81] update docker and build for demo --- Dockerfile | 4 ++-- OpenSprinkler.cpp | 3 ++- OpenSprinkler.h | 2 +- main.cpp | 4 ++++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3bc27516..e409a319 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ RUN make FROM base AS os-build ENV DEBIAN_FRONTEND=noninteractive -RUN apt-get update && apt-get install -y bash g++ make libmosquittopp-dev libssl-dev +RUN apt-get update && apt-get install -y bash g++ make libmosquittopp-dev libssl-dev libi2c-dev RUN rm -rf /var/lib/apt/lists/* COPY . /OpenSprinkler WORKDIR /OpenSprinkler @@ -34,7 +34,7 @@ RUN make VERSION=${BUILD_VERSION} FROM base ENV DEBIAN_FRONTEND=noninteractive -RUN apt-get update && apt-get install -y libstdc++6 libmosquittopp1 +RUN apt-get update && apt-get install -y libstdc++6 libmosquittopp1 libi2c0 RUN rm -rf /var/lib/apt/lists/* RUN mkdir /OpenSprinkler RUN mkdir -p /data/logs diff --git a/OpenSprinkler.cpp b/OpenSprinkler.cpp index 6e9fd2c7..7d3f2e84 100644 --- a/OpenSprinkler.cpp +++ b/OpenSprinkler.cpp @@ -2928,7 +2928,6 @@ void OpenSprinkler::lcd_print_option(int i) { } -#endif /** Button functions */ /** wait for button */ @@ -3124,6 +3123,8 @@ void OpenSprinkler::set_screen_led(unsigned char status) { #endif +#endif + #if defined(ESP8266) void OpenSprinkler::reset_to_ap() { diff --git a/OpenSprinkler.h b/OpenSprinkler.h index 6605d242..cc563118 100644 --- a/OpenSprinkler.h +++ b/OpenSprinkler.h @@ -30,7 +30,6 @@ #include "utils.h" #include "gpio.h" #include "images.h" -#include "rpitime.h" #include "mqtt.h" #if defined(ARDUINO) // headers for Arduino @@ -64,6 +63,7 @@ #include #include "OpenThingsFramework.h" #include "etherport.h" + #include "rpitime.h" #include "smtp.h" #endif // end of headers diff --git a/main.cpp b/main.cpp index 4a9056ab..cdf44e18 100644 --- a/main.cpp +++ b/main.cpp @@ -126,6 +126,7 @@ void flow_poll() { /* End of RAH implementation of flow sensor */ } +#if defined(USE_DISPLAY) // ====== UI defines ====== static char ui_anim_chars[3] = {'.', 'o', 'O'}; @@ -334,6 +335,7 @@ void ui_state_machine() { break; } } +#endif // ====================== @@ -629,7 +631,9 @@ void do_loop() #else // Process Ethernet packets for RPI/BBB if(otf) otf->loop(); +#if defined(USE_DISPLAY) ui_state_machine(); +#endif #endif // Process Ethernet packets // Start up MQTT when we have a network connection From f62af23dd123157a80d538a983be714683a856bc Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:18:28 -0400 Subject: [PATCH 61/81] fix typos --- main.cpp | 8 ++++---- opensprinkler_server.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index cdf44e18..eefcdd6a 100644 --- a/main.cpp +++ b/main.cpp @@ -199,7 +199,7 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); - #if defined(Arduino) + #if defined(ARDUINO) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.gatewayIP()); } else { os.lcd.print(WiFi.gatewayIP()); } @@ -230,7 +230,7 @@ void ui_state_machine() { #endif os.lcd.clear(0, 1); os.lcd.setCursor(0, 0); - #if defined(Arduino) + #if defined(ARDUINO) #if defined(ESP8266) if (useEth) { os.lcd.print(eth.localIP()); } else { os.lcd.print(WiFi.localIP()); } @@ -416,7 +416,7 @@ ISR(WDT_vect) #endif #else -void initalize_otf(); +void initialize_otf(); void do_setup() { initialiseEpoch(); // initialize time reference for millis() and micros() @@ -443,7 +443,7 @@ void do_setup() { os.mqtt.init(); os.status.req_mqtt_restart = true; - initalize_otf(); + initialize_otf(); } #endif diff --git a/opensprinkler_server.cpp b/opensprinkler_server.cpp index f2acfc5a..4e3b05db 100644 --- a/opensprinkler_server.cpp +++ b/opensprinkler_server.cpp @@ -2193,7 +2193,7 @@ void start_server_ap() { #endif #if defined(USE_OTF) && !defined(ARDUINO) -void initalize_otf() { +void initialize_otf() { if(!otf) return; static bool callback_initialized = false; From 080dccbebeee99b4749ed9a014fce36a1ea2f704 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:25:43 -0400 Subject: [PATCH 62/81] fix narrowing conversion of images --- SSD1306Display.h | 10 ++++++++++ images.h | 38 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index dfac9426..dc2376f9 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -72,6 +72,11 @@ class SSD1306Display : public SSD1306{ void createChar(unsigned char idx, PGM_P ptr) { if(idx>=0&&idx= 0 && idx < NUM_CUSTOM_ICONS) custom_chars[idx] = ptr; } + + void createChar(unsigned char idx, const unsigned char *ptr) { + createChar(idx, (const char *)ptr); + } + void setAutoDisplay(bool v) { auto_display = v; } private: diff --git a/images.h b/images.h index a6c9a004..67e376c5 100644 --- a/images.h +++ b/images.h @@ -40,7 +40,7 @@ enum { #define WiFi_Logo_width 60 #define WiFi_Logo_height 36 -const char WiFi_Logo_image[] PROGMEM = { +const unsigned char WiFi_Logo_image[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, @@ -67,56 +67,56 @@ const char WiFi_Logo_image[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; -const char _iconimage_wifi_connected[] PROGMEM = { +const unsigned char _iconimage_wifi_connected[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xA0, 0xA0, 0xA8, 0xA8, 0xAA, 0xAA, 0x00, 0x00, }; -const char _iconimage_wifi_disconnected[] PROGMEM = { +const unsigned char _iconimage_wifi_disconnected[] PROGMEM = { 0x00, 0x00, 0x00, 0x11, 0x0A, 0x04, 0x8A, 0x91, 0xA0, 0xA0, 0xA8, 0xA8, 0xAA, 0xAA, 0x00, 0x00, }; -const char _iconimage_remotext[] PROGMEM = { +const unsigned char _iconimage_remotext[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x62, 0x54, 0x48, 0x44, 0x22, 0x1F, 0x00, 0x00, }; -const char _iconimage_raindelay[] PROGMEM = { +const unsigned char _iconimage_raindelay[] PROGMEM = { 0x00, 0x00, 0x00, 0x1C, 0x22, 0x49, 0x49, 0x49, 0x59, 0x41, 0x41, 0x41, 0x22, 0x1C, 0x00, 0x00, }; -const char _iconimage_rain[] PROGMEM = { +const unsigned char _iconimage_rain[] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x18, 0x24, 0x3E, 0x00, 0x2A, 0x2A, 0x00, 0x2A, 0x2A, 0x00, 0x00, 0x00, }; -const char _iconimage_soil[] PROGMEM = { +const unsigned char _iconimage_soil[] PROGMEM = { 0x00, 0x00, 0x10, 0x10, 0x28, 0x28, 0x44, 0x44, 0x8A, 0x8A, 0x92, 0x44, 0x38, 0x00, 0xC6, 0x38, }; -const char _iconimage_ether_connected[] PROGMEM = { +const unsigned char _iconimage_ether_connected[] PROGMEM = { 0x00, 0x00, 0x00, 0x38, 0x28, 0x38, 0x10, 0x10, 0xFE, 0x44, 0x44, 0xEE, 0xAA, 0xEE, 0x00, 0x00, }; -const char _iconimage_ether_disconnected[] PROGMEM = { +const unsigned char _iconimage_ether_disconnected[] PROGMEM = { 0x00, 0x00, 0x11, 0x0A, 0x04, 0xEA, 0xB1, 0xE0, 0x40, 0xFE, 0x44, 0xEE, @@ -125,14 +125,14 @@ const char _iconimage_ether_disconnected[] PROGMEM = { /* -const char _iconimage_flow[] PROGMEM = { +const unsigned char _iconimage_flow[] PROGMEM = { 0x00, 0x00, 0x0F, 0x0F, 0x03, 0x0F, 0x0F, 0x03, 0x1B, 0x18, 0x18, 0x18, 0x78, 0x78, 0x00, 0x00, }; -const char _iconimage_pswitch[] PROGMEM = { +const unsigned char _iconimage_pswitch[] PROGMEM = { 0x00, 0x00, 0x1E, 0x12, 0x12, 0x12, 0x1E, 0x02, 0x22, 0x32, 0x22, 0x20, @@ -143,7 +143,7 @@ const char _iconimage_pswitch[] PROGMEM = { #elif defined(USE_LCD) -const char _iconimage_connected[] PROGMEM = { +const unsigned char _iconimage_connected[] PROGMEM = { B00000, B00000, B00000, @@ -154,7 +154,7 @@ const char _iconimage_connected[] PROGMEM = { B10101 }; -const char _iconimage_disconnected[] PROGMEM = { +const unsigned char _iconimage_disconnected[] PROGMEM = { B00000, B10100, B01000, @@ -165,7 +165,7 @@ const char _iconimage_disconnected[] PROGMEM = { B10101 }; -const char _iconimage_remotext[] PROGMEM = { +const unsigned char _iconimage_remotext[] PROGMEM = { B00000, B00000, B00000, @@ -176,7 +176,7 @@ const char _iconimage_remotext[] PROGMEM = { B11110 }; -const char _iconimage_raindelay[] PROGMEM = { +const unsigned char _iconimage_raindelay[] PROGMEM = { B00000, B01110, B10101, @@ -187,7 +187,7 @@ const char _iconimage_raindelay[] PROGMEM = { B01110 }; -const char _iconimage_rain[] PROGMEM = { +const unsigned char _iconimage_rain[] PROGMEM = { B00000, B00000, B00110, @@ -198,7 +198,7 @@ const char _iconimage_rain[] PROGMEM = { B10101 }; -const char _iconimage_soil[] PROGMEM = { +const unsigned char _iconimage_soil[] PROGMEM = { B00100, B00100, B01010, @@ -210,7 +210,7 @@ const char _iconimage_soil[] PROGMEM = { }; /* -const char _iconimage_flow[] PROGMEM = { +const unsigned char _iconimage_flow[] PROGMEM = { B00000, B00000, B00000, @@ -221,7 +221,7 @@ const char _iconimage_flow[] PROGMEM = { B00000 }; -const char _iconimage_pswitch[] PROGMEM = { +const unsigned char _iconimage_pswitch[] PROGMEM = { B00000, B11000, B10100, From cbedb78cd3b60db49e74b8cfed7537f65c7d669c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 15:27:27 -0400 Subject: [PATCH 63/81] remove wiring pi --- gpio.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/gpio.cpp b/gpio.cpp index 85b0a229..8e773121 100644 --- a/gpio.cpp +++ b/gpio.cpp @@ -188,7 +188,6 @@ unsigned char digitalReadExt(unsigned char pin) { #include #include #include -#include #define BUFFER_MAX 64 #define GPIO_MAX 64 From 228521045be85d0df95131047d0e1da028366f26 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:55:18 -0400 Subject: [PATCH 64/81] push to move dev to rpi --- SSD1306Display.h | 59 ++------------------------------ i2cd.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ utils.cpp | 45 +++++++++++++++++++++++++ utils.h | 11 ++++++ 4 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 i2cd.h diff --git a/SSD1306Display.h b/SSD1306Display.h index dc2376f9..0b304853 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -90,13 +90,8 @@ class SSD1306Display : public SSD1306{ #include #include -#include - -extern "C" { - #include - #include -} +#include "i2cd.h" #include "font.h" #include "images.h" @@ -484,7 +479,7 @@ class SSD1306Display { void createChar(unsigned char idx, const unsigned char *ptr) { createChar(idx, (const char *)ptr); } - + void setAutoDisplay(bool v) { auto_display = v; } private: @@ -502,56 +497,6 @@ class SSD1306Display { memset(frame, 0x00, sizeof(frame)); } - bool transaction = false; - unsigned char transaction_id = 0; - unsigned char transaction_buffer[32]; - unsigned char transaction_buffer_length = 0; - - int i2c_begin_transaction(unsigned char id) { - if (transaction) { - return -1; - } else { - transaction_id = id; - transaction = true; - memset(transaction_buffer, 0x00, sizeof(transaction_buffer)); - transaction_buffer_length = 0; - return 0; - } - } - - int i2c_send_transaction() { - return i2c_smbus_write_i2c_block_data(file, transaction_id, transaction_buffer_length, transaction_buffer); - } - - int i2c_end_transaction() { - if (transaction) { - transaction = false; - return i2c_send_transaction(); - } else { - return -1; - } - } - - int i2c_send(unsigned char reg, unsigned char data) { - if (transaction) { - if (reg != transaction_id) { - return -1; - } - - int res = 0; - if (transaction_buffer_length >= sizeof(transaction_buffer)) { - res = i2c_send_transaction(); - transaction_buffer_length = 0; - } - - transaction_buffer[transaction_buffer_length] = data; - transaction_buffer_length++; - return res; - } else { - return i2c_smbus_write_byte_data(file, reg, data); - } - } - int ssd1306_command(unsigned char command) { return i2c_send(0x00, command); } diff --git a/i2cd.h b/i2cd.h new file mode 100644 index 00000000..f9cfb2e0 --- /dev/null +++ b/i2cd.h @@ -0,0 +1,87 @@ +#ifndef I2CD_H +#define I2CD_H + +#include +#include + +extern "C" { +#include +#include +} + +class I2CDevice { +public: + I2CDevice() {} + + int begin(const char *bus, unsigned char addr) { + _file = open(bus, O_RDWR); + if (_file < 0) { + return _file; + } + + return ioctl(file, I2C_SLAVE, addr); + } + + int begin(unsigned char addr) { + return begin(getDefaultBus(), addr); + } + + int i2c_begin_transaction(unsigned char id) { + if (transaction) { + return -1; + } else { + transaction_id = id; + transaction = true; + memset(transaction_buffer, 0x00, sizeof(transaction_buffer)); + transaction_buffer_length = 0; + return 0; + } + } + + int i2c_send_transaction() { + return i2c_smbus_write_i2c_block_data( + file, transaction_id, transaction_buffer_length, transaction_buffer); + } + + int i2c_end_transaction() { + if (transaction) { + transaction = false; + return i2c_send_transaction(); + } else { + return -1; + } + } + + int i2c_send(unsigned char reg, unsigned char data) { + if (transaction) { + if (reg != transaction_id) { + return -1; + } + + int res = 0; + if (transaction_buffer_length >= sizeof(transaction_buffer)) { + res = i2c_send_transaction(); + transaction_buffer_length = 0; + } + + transaction_buffer[transaction_buffer_length] = data; + transaction_buffer_length++; + return res; + } else { + return i2c_smbus_write_byte_data(file, reg, data); + } + } + +private: + int _file = -1; + bool transaction = false; + unsigned char transaction_id = 0; + unsigned char transaction_buffer[32]; + unsigned char transaction_buffer_length = 0; + + const char *getDefaultBus() { + return NULL; + } +}; + +#endif // I2CD_H \ No newline at end of file diff --git a/utils.cpp b/utils.cpp index caad3f3d..aa5f1085 100644 --- a/utils.cpp +++ b/utils.cpp @@ -236,6 +236,51 @@ in_addr_t get_ip_address(char *iface) { } #endif +BoardType get_board_type() { + FILE *file = fopen("/proc/device-tree/compatible", "rb"); + if (file == NULL) { + return BoardType::Unknown; + } + + char buffer[100]; + + BoardType res = BoardType::Unknown; + + int total = fread(buffer, 1, sizeof(buffer), file); + + if (prefix("raspberrypi", buffer)) { + res = BoardType::RaspberryPi_Unknown; + const char *cpu_buf = buffer; + size_t index = 0; + + // model and cpu is seperated by a null byte + while (index < (total - 1) && cpu_buf[index]) { + index += 1; + } + + cpu_buf += index + 1; + + if (!strcmp("brcm,bcm2712", cpu_buf)) { + // Pi 5 + res = BoardType::RaspberryPi_bcm2712; + } else if (!strcmp("brcm,bcm2711", cpu_buf)) { + // Pi 4 + res = BoardType::RaspberryPi_bcm2711; + } else if (!strcmp("brcm,bcm2837", cpu_buf)) { + // Pi 3 / Pi Zero 2 + res = BoardType::RaspberryPi_bcm2837; + } else if (!strcmp("brcm,bcm2836", cpu_buf)) { + // Pi 2 + res = BoardType::RaspberryPi_bcm2836; + } else if (!strcmp("brcm,bcm2835", cpu_buf)) { + // Pi / Pi Zero + res = BoardType::RaspberryPi_bcm2835; + } + } + + return res; +} + #endif diff --git a/utils.h b/utils.h index 7fd21821..06578775 100644 --- a/utils.h +++ b/utils.h @@ -96,6 +96,17 @@ void str2mac(const char *_str, unsigned char mac[]); in_addr_t get_ip_address(char *iface); #endif + enum BoardType = { + Unknown; + RaspberryPi_Unknown; + RaspberryPi_bcm2712; + RaspberryPi_bcm2711; + RaspberryPi_bcm2837; + RaspberryPi_bcm2836; + RaspberryPi_bcm2835; + } + + BoardType get_board_type(); #endif #endif // _UTILS_H From d0a8efbf3fbcb8eb24294417511682400ad9d90b Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:00:05 -0400 Subject: [PATCH 65/81] fix get board type --- utils.cpp | 6 ++++++ utils.h | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/utils.cpp b/utils.cpp index aa5f1085..4768f17c 100644 --- a/utils.cpp +++ b/utils.cpp @@ -39,6 +39,8 @@ extern OpenSprinkler os; #else // RPI/BBB +#include + char* get_runtime_path() { static char path[PATH_MAX]; static unsigned char query = 1; @@ -236,6 +238,10 @@ in_addr_t get_ip_address(char *iface) { } #endif +bool prefix(const char *pre, const char *str) { + return strncmp(pre, str, strlen(pre)) == 0; +} + BoardType get_board_type() { FILE *file = fopen("/proc/device-tree/compatible", "rb"); if (file == NULL) { diff --git a/utils.h b/utils.h index 06578775..239cdd29 100644 --- a/utils.h +++ b/utils.h @@ -96,15 +96,15 @@ void str2mac(const char *_str, unsigned char mac[]); in_addr_t get_ip_address(char *iface); #endif - enum BoardType = { - Unknown; - RaspberryPi_Unknown; - RaspberryPi_bcm2712; - RaspberryPi_bcm2711; - RaspberryPi_bcm2837; - RaspberryPi_bcm2836; - RaspberryPi_bcm2835; - } + enum BoardType { + Unknown, + RaspberryPi_Unknown, + RaspberryPi_bcm2712, + RaspberryPi_bcm2711, + RaspberryPi_bcm2837, + RaspberryPi_bcm2836, + RaspberryPi_bcm2835, + }; BoardType get_board_type(); #endif From ac5e75de521ef9021ad4ae34d82520e1eef236aa Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:00:28 -0400 Subject: [PATCH 66/81] move gpio chip to get_board_type --- gpio.cpp | 51 +++++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/gpio.cpp b/gpio.cpp index 8e773121..c075e047 100644 --- a/gpio.cpp +++ b/gpio.cpp @@ -484,44 +484,27 @@ struct gpiod_line* gpio_lines[] = { NULL, NULL, NULL, NULL, NULL, }; -bool prefix(const char *pre, const char *str) { - return strncmp(pre, str, strlen(pre)) == 0; -} - int assert_gpiod_chip() { if( !chip ) { const char *chip_name = NULL; - FILE *file = fopen("/proc/device-tree/compatible", "rb"); - if (file != NULL) { - char buffer[100]; - - int total = fread(buffer, 1, sizeof(buffer), file); - - if (prefix("raspberrypi", buffer)) { - const char *cpu_buf = buffer; - size_t index = 0; - - // model and cpu is seperated by a null byte - while (index < (total - 1) && cpu_buf[index]) { - index += 1; - } - - cpu_buf += index + 1; - - if (!strcmp("brcm,bcm2712", cpu_buf)) { - // Pi 5 - chip_name = "pinctrl-rp1"; - } else if (!strcmp("brcm,bcm2711", cpu_buf)) { - // Pi 4 - chip_name = "pinctrl-bcm2711"; - } else if (!strcmp("brcm,bcm2837", cpu_buf) - || !strcmp("brcm,bcm2836", cpu_buf) - || !strcmp("brcm,bcm2835", cpu_buf)) { - // Pi 0-3 - chip_name = "pinctrl-bcm2835"; - } - } + switch (get_board_type()) { + case BoardType::RaspberryPi_bcm2712: + chip_name = "pinctrl-rp1"; + break; + case BoardType::RaspberryPi_bcm2711: + chip_name = "pinctrl-bcm2711"; + break; + case BoardType::RaspberryPi_bcm2837: + case BoardType::RaspberryPi_bcm2836: + case BoardType::RaspberryPi_bcm2835: + chip_name = "pinctrl-bcm2835"; + break; + case BoardType::Unknown: + case BoardType::RaspberryPi_Unknown: + default: + // Unknown chip + break; } From bb7addb0162880004fbce9829aa219150ffa602f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:01:10 -0400 Subject: [PATCH 67/81] move i2c device handling to another class --- SSD1306Display.h | 63 +++++++++++++----------------------------------- i2cd.h | 45 +++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 63 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 0b304853..7d8d7723 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -120,12 +120,15 @@ class SSD1306Display : public SSD1306{ class SSD1306Display { public: - SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) { + SSD1306Display(uint8_t addr, uint8_t _sda, uint8_t _scl) { cx = 0; cy = 0; + _addr = addr; for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) custom_chars[i] = 0; clear_buffer(); + + i2c = I2CDevice(); } ~SSD1306Display() { @@ -136,48 +139,13 @@ class SSD1306Display { void init() {} // Dummy function to match ESP8266 int begin() { - // if (!OLEDSetBufferPtr(128, 64, frame, sizeof(frame))) return -1; - - // const uint16_t I2C_Speed = BCM2835_I2C_CLOCK_DIVIDER_148; - // const uint8_t I2C_Address = 0x3C; - // bool I2C_debug = false; - - // // Check if Bcm28235 lib installed and print version. - // if (!bcm2835_init()) { - // printf("Error 1201: init bcm2835 library , Is it installed ?\r\n"); - // return -1; - // } - - // // Turn on I2C bus (optionally it may already be on) - // if (!OLED_I2C_ON()) { - // printf("Error 1202: bcm2835_i2c_begin :Cannot start I2C, Running as root?\n"); - // bcm2835_close(); // Close the library - // return -1; - // } - - // OLEDbegin(I2C_Speed, I2C_Address, I2C_debug); // initialize the OLED + i2c.begin(_addr); + setFont(Monospaced_plain_13); fontWidth = 8; fontHeight = 16; - int adapter_nr = 1; /* probably dynamically determined */ - char filename[20]; - - snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); - file = open(filename, O_RDWR); - if (file < 0) { - /* ERROR HANDLING; you can check errno to see what went wrong */ - exit(1); - } - - int addr = 0x3C; /* The I2C address */ - - if (ioctl(file, I2C_SLAVE, addr) < 0) { - /* ERROR HANDLING; you can check errno to see what went wrong */ - exit(1); - } - - i2c_begin_transaction(0x00); + i2c.begin_transaction(0x00); ssd1306_command(0xAE); ssd1306_command(0xD5); ssd1306_command(0x80); @@ -207,7 +175,7 @@ class SSD1306Display { ssd1306_command(0x2E); ssd1306_command(0xAF); - i2c_end_transaction(); + i2c.end_transaction(); return 0; } @@ -215,7 +183,7 @@ class SSD1306Display { void setFont(const uint8_t *f) { font = (uint8_t *)f; } void display() { - i2c_begin_transaction(0x00); + i2c.begin_transaction(0x00); ssd1306_command(0x22); ssd1306_command(0x00); ssd1306_command(0xFF); @@ -224,16 +192,16 @@ class SSD1306Display { int width = 128; ssd1306_command(width - 1); // Column end address - i2c_end_transaction(); + i2c.end_transaction(); - i2c_begin_transaction(0x40); + i2c.begin_transaction(0x40); int b; for (b = 0; b < 1024; b++) { ssd1306_data(frame[b]); } - i2c_end_transaction(); + i2c.end_transaction(); } void clear() { @@ -493,16 +461,19 @@ class SSD1306Display { bool color; uint8_t *font; + I2CDevice i2c; + unsigned char _addr; + void clear_buffer() { memset(frame, 0x00, sizeof(frame)); } int ssd1306_command(unsigned char command) { - return i2c_send(0x00, command); + return i2c.send(0x00, command); } int ssd1306_data(unsigned char value) { - return i2c_send(0x40, value); + return i2c.send(0x40, value); } }; #endif diff --git a/i2cd.h b/i2cd.h index f9cfb2e0..53d46f9a 100644 --- a/i2cd.h +++ b/i2cd.h @@ -7,6 +7,8 @@ extern "C" { #include #include +#include +#include "utils.h" } class I2CDevice { @@ -19,14 +21,12 @@ class I2CDevice { return _file; } - return ioctl(file, I2C_SLAVE, addr); + return ioctl(_file, I2C_SLAVE, addr); } - int begin(unsigned char addr) { - return begin(getDefaultBus(), addr); - } + int begin(unsigned char addr) { return begin(getDefaultBus(), addr); } - int i2c_begin_transaction(unsigned char id) { + int begin_transaction(unsigned char id) { if (transaction) { return -1; } else { @@ -38,21 +38,16 @@ class I2CDevice { } } - int i2c_send_transaction() { - return i2c_smbus_write_i2c_block_data( - file, transaction_id, transaction_buffer_length, transaction_buffer); - } - - int i2c_end_transaction() { + int end_transaction() { if (transaction) { transaction = false; - return i2c_send_transaction(); + return send_transaction(); } else { return -1; } } - int i2c_send(unsigned char reg, unsigned char data) { + int send(unsigned char reg, unsigned char data) { if (transaction) { if (reg != transaction_id) { return -1; @@ -60,7 +55,7 @@ class I2CDevice { int res = 0; if (transaction_buffer_length >= sizeof(transaction_buffer)) { - res = i2c_send_transaction(); + res = send_transaction(); transaction_buffer_length = 0; } @@ -68,7 +63,7 @@ class I2CDevice { transaction_buffer_length++; return res; } else { - return i2c_smbus_write_byte_data(file, reg, data); + return i2c_smbus_write_byte_data(_file, reg, data); } } @@ -79,8 +74,24 @@ class I2CDevice { unsigned char transaction_buffer[32]; unsigned char transaction_buffer_length = 0; - const char *getDefaultBus() { - return NULL; + const char *getDefaultBus() { + switch (get_board_type()) { + case BoardType::RaspberryPi_bcm2712: + case BoardType::RaspberryPi_bcm2711: + case BoardType::RaspberryPi_bcm2837: + case BoardType::RaspberryPi_bcm2836: + case BoardType::RaspberryPi_bcm2835: + return "/dev/i2c-1"; + case BoardType::Unknown: + case BoardType::RaspberryPi_Unknown: + default: + return "/dev/i2c-0"; + } + } + + int send_transaction() { + return i2c_smbus_write_i2c_block_data( + _file, transaction_id, transaction_buffer_length, transaction_buffer); } }; From f9689990da888d516caae5644b1e016f54ef8a10 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 11:28:33 -0400 Subject: [PATCH 68/81] remove magic numbers --- SSD1306Display.h | 386 ++++++++++++++++++++++++++++------------------- 1 file changed, 231 insertions(+), 155 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 7d8d7723..2c69174a 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -8,31 +8,31 @@ #include "font.h" #include "images.h" -#define LCD_STD 0 // Standard LCD +#define LCD_STD 0 // Standard LCD #define LCD_I2C 1 -class SSD1306Display : public SSD1306{ +class SSD1306Display : public SSD1306 { public: - SSD1306Display(uint8_t _addr, uint8_t _sda, uint8_t _scl) : SSD1306(_addr, _sda, _scl) { - cx = 0; - cy = 0; - for(unsigned char i=0;i=0&&idx= 0 && idx < NUM_CUSTOM_ICONS) + custom_chars[idx] = ptr; + } + + void createChar(unsigned char idx, const unsigned char *ptr) { + createChar(idx, (const char *)ptr); + } + + void setAutoDisplay(bool v) { auto_display = v; } + private: - bool auto_display = true; - uint8_t cx, cy; - uint8_t fontWidth, fontHeight; - PGM_P custom_chars[NUM_CUSTOM_ICONS]; + bool auto_display = true; + uint8_t cx, cy; + uint8_t fontWidth, fontHeight; + PGM_P custom_chars[NUM_CUSTOM_ICONS]; }; #else @@ -99,7 +104,7 @@ class SSD1306Display : public SSD1306{ #include #include -#define LCD_STD 0 // Standard LCD +#define LCD_STD 0 // Standard LCD #define LCD_I2C 1 #define BLACK 0 @@ -118,15 +123,65 @@ class SSD1306Display : public SSD1306{ #define FIRST_CHAR_POS 2 #define CHAR_NUM_POS 3 +// SSD1306 +// Codes +#define SSD1306_COMMAND_ADDRESS 0x00 +#define SSD1306_DATA_CONTINUE_ADDRESS 0x40 + +// Fundamental Commands +#define SSD1306_SET_CONTRAST_CONTROL 0x81 +#define SSD1306_DISPLAY_ALL_ON_RESUME 0xA4 +#define SSD1306_DISPLAY_ALL_ON 0xA5 +#define SSD1306_NORMAL_DISPLAY 0xA6 +#define SSD1306_INVERT_DISPLAY 0xA7 +#define SSD1306_DISPLAY_OFF 0xAE +#define SSD1306_DISPLAY_ON 0xAF +#define SSD1306_NOP 0xE3 + +// Scrolling Commands +#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 +#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 +#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 +#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A +#define SSD1306_DEACTIVATE_SCROLL 0x2E +#define SSD1306_ACTIVATE_SCROLL 0x2F +#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 + +// Addressing Setting Commands +#define SSD1306_SET_LOWER_COLUMN 0x00 +#define SSD1306_SET_HIGHER_COLUMN 0x10 +#define SSD1306_MEMORY_ADDR_MODE 0x20 +#define SSD1306_SET_COLUMN_ADDR 0x21 +#define SSD1306_SET_PAGE_ADDR 0x22 + +// Hardware Configuration Commands +#define SSD1306_SET_START_LINE 0x40 +#define SSD1306_SET_SEGMENT_REMAP 0xA0 +#define SSD1306_SET_MULTIPLEX_RATIO 0xA8 +#define SSD1306_COM_SCAN_DIR_INC 0xC0 +#define SSD1306_COM_SCAN_DIR_DEC 0xC8 +#define SSD1306_SET_DISPLAY_OFFSET 0xD3 +#define SSD1306_SET_COM_PINS 0xDA +#define SSD1306_CHARGE_PUMP 0x8D + +// Timing & Driving Scheme Setting Commands +#define SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO 0xD5 +#define SSD1306_SET_PRECHARGE_PERIOD 0xD9 +#define SSD1306_SET_VCOM_DESELECT 0xDB + class SSD1306Display { - public: +public: SSD1306Display(uint8_t addr, uint8_t _sda, uint8_t _scl) { cx = 0; cy = 0; _addr = addr; - for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) custom_chars[i] = 0; + for (uint8_t i = 0; i < NUM_CUSTOM_ICONS; i++) + custom_chars[i] = 0; - clear_buffer(); + clear_buffer(); + + height = 64; + width = 128; i2c = I2CDevice(); } @@ -136,8 +191,8 @@ class SSD1306Display { close(file); } - void init() {} // Dummy function to match ESP8266 - + void init() {} // Dummy function to match ESP8266 + int begin() { i2c.begin(_addr); @@ -145,35 +200,53 @@ class SSD1306Display { fontWidth = 8; fontHeight = 16; - i2c.begin_transaction(0x00); - ssd1306_command(0xAE); - ssd1306_command(0xD5); + i2c.begin_transaction(SSD1306_COMMAND_ADDRESS); + ssd1306_command(SSD1306_DISPLAY_OFF); + ssd1306_command(SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO); ssd1306_command(0x80); - ssd1306_command(0xA8); - unsigned char height = 64; - ssd1306_command(height-1); - ssd1306_command(0xD3); + ssd1306_command(SSD1306_SET_MULTIPLEX_RATIO); + ssd1306_command(height - 1); + ssd1306_command(SSD1306_SET_DISPLAY_OFFSET); ssd1306_command(0x00); - ssd1306_command(0x40); - ssd1306_command(0x8D); + ssd1306_command(SSD1306_SET_START_LINE); + ssd1306_command(SSD1306_CHARGE_PUMP); ssd1306_command(0x14); - ssd1306_command(0x20); + ssd1306_command(SSD1306_MEMORY_ADDR_MODE); ssd1306_command(0x00); - ssd1306_command(0xA1); - ssd1306_command(0xC8); - ssd1306_command(0xDA); - ssd1306_command(0x12); - ssd1306_command(0x81); - ssd1306_command(0xCF); - ssd1306_command(0xd9); + ssd1306_command(SSD1306_SET_SEGMENT_REMAP | 0x01); + ssd1306_command(SSD1306_COM_SCAN_DIR_DEC); + + switch (height) { + case 64: + SSD1306_command(SSD1306_SET_COM_PINS); + SSD1306_command(0x12); + SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); + SSD1306_command(0xCF); + break; + case 32: + SSD1306_command(SSD1306_SET_COM_PINS); + SSD1306_command(0x02); + SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); + SSD1306_command(0x8F); + break; + case 16: // NOTE: not tested, lacking part. + SSD1306_command(SSD1306_SET_COM_PINS); + SSD1306_command(0x2); + SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); + SSD1306_command(0xAF); + break; + } + + ssd1306_command(SSD1306_SET_PRECHARGE_PERIOD); ssd1306_command(0xF1); - ssd1306_command(0xDB); + ssd1306_command(SSD1306_SET_VCOM_DESELECT); ssd1306_command(0x40); - ssd1306_command(0xA4); - ssd1306_command(0xA6); - ssd1306_command(0x2E); - ssd1306_command(0xAF); + + ssd1306_command(SSD1306_DISPLAY_ALL_ON_RESUME); + ssd1306_command(SSD1306_NORMAL_DISPLAY); + ssd1306_command(SSD1306_DEACTIVATE_SCROLL); + ssd1306_command(SSD1306_DISPLAY_ON); i2c.end_transaction(); @@ -183,23 +256,33 @@ class SSD1306Display { void setFont(const uint8_t *f) { font = (uint8_t *)f; } void display() { - i2c.begin_transaction(0x00); - ssd1306_command(0x22); - ssd1306_command(0x00); - ssd1306_command(0xFF); - ssd1306_command(0x21); - ssd1306_command(0x00); - - int width = 128; - ssd1306_command(width - 1); // Column end address + i2c.begin_transaction(SSD1306_COMMAND_ADDRESS); + ssd1306_command(SSD1306_SET_PAGE_ADDR); + ssd1306_command(0x00); // Page start address (0 = reset) + switch (height) { + case 64: + ssd1306_command(7); + break; + case 32: + ssd1306_command(3); + break; + case 16: + ssd1306_command(1); + break; + } + + ssd1306_command(SSD1306_SET_COLUMN_ADDR); + ssd1306_command(0x00); // Column start address (0 = reset) + ssd1306_command(width - 1); // Column end address (127 = reset) + i2c.end_transaction(); - - i2c.begin_transaction(0x40); + + i2c.begin_transaction(SSD1306_DATA_CONTINUE_ADDRESS); int b; for (b = 0; b < 1024; b++) { ssd1306_data(frame[b]); - } + } i2c.end_transaction(); } @@ -210,22 +293,19 @@ class SSD1306Display { } void setBrightness(uint8_t brightness) { - ssd1306_command(0x81); + ssd1306_command(SSD1306_SET_CONTRAST_CONTROL); ssd1306_command(brightness); } - void displayOn() { - ssd1306_command(0xAF); - } + void displayOn() { ssd1306_command(SSD1306_DISPLAY_ON); } - void displayOff() { - ssd1306_command(0xAE); - } + void displayOff() { ssd1306_command(SSD1306_DISPLAY_OFF); } void setColor(uint8_t color) { this->color = color; } void drawPixel(uint8_t x, uint8_t y) { - if (x >= 128 || y >= 64) return; + if (x >= 128 || y >= 64) + return; if (color == WHITE) { frame[x + (y / 8) * 128] |= 1 << (y % 8); @@ -248,54 +328,50 @@ class SSD1306Display { setColor(WHITE); } - void print(const char *s) { - write(s); - } + void print(const char *s) { write(s); } - void print(char s) { - write(s); - } + void print(char s) { write(s); } - void print(int i) { + void print(int i) { char buf[100]; snprintf(buf, 100, "%d", i); print((const char *)buf); } - void print(unsigned int i) { + void print(unsigned int i) { char buf[100]; snprintf(buf, 100, "%u", i); print((const char *)buf); } - void print(float f) { + void print(float f) { char buf[100]; snprintf(buf, 100, "%f", f); print((const char *)buf); } - #define DEC 10 - #define HEX 16 - #define OCT 8 - #define BIN 2 +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 - void print(int i, int base) { + void print(int i, int base) { char buf[100]; switch (base) { - case DEC: - snprintf(buf, 100, "%d", i); - break; - case HEX: - snprintf(buf, 100, "%x", i); - break; - case OCT: - snprintf(buf, 100, "%o", i); - break; - case BIN: - snprintf(buf, 100, "%b", i); - break; - default: - snprintf(buf, 100, "%d", i); - break; + case DEC: + snprintf(buf, 100, "%d", i); + break; + case HEX: + snprintf(buf, 100, "%x", i); + break; + case OCT: + snprintf(buf, 100, "%o", i); + break; + case BIN: + snprintf(buf, 100, "%b", i); + break; + default: + snprintf(buf, 100, "%d", i); + break; } print((const char *)buf); } @@ -314,7 +390,7 @@ class SSD1306Display { void drawXbm(int x, int y, int w, int h, const char *xbm) { int xbmWidth = (w + 7) / 8; uint8_t data = 0; - + for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (j & 7) { @@ -322,7 +398,7 @@ class SSD1306Display { } else { data = xbm[(i * xbmWidth) + (j / 8)]; } - + if (data & 0x01) { drawPixel(x + j, y + i); } @@ -350,21 +426,21 @@ class SSD1306Display { uint8_t numChars = font[CHAR_NUM_POS]; uint16_t sizeOfJumpTable = numChars * JUMPTABLE_BYTES; - if (c < firstChar || c >= firstChar + numChars) return; + if (c < firstChar || c >= firstChar + numChars) + return; // 4 Bytes per char code uint8_t charCode = c - firstChar; uint8_t msbJumpToChar = - font[JUMPTABLE_START + - charCode * JUMPTABLE_BYTES]; // MSB \ JumpAddress + font[JUMPTABLE_START + charCode * JUMPTABLE_BYTES]; // MSB \ JumpAddress uint8_t lsbJumpToChar = font[JUMPTABLE_START + charCode * JUMPTABLE_BYTES + - JUMPTABLE_LSB]; // LSB / + JUMPTABLE_LSB]; // LSB / uint8_t charByteSize = font[JUMPTABLE_START + charCode * JUMPTABLE_BYTES + - JUMPTABLE_SIZE]; // Size + JUMPTABLE_SIZE]; // Size uint8_t currentCharWidth = font[JUMPTABLE_START + (c - firstChar) * JUMPTABLE_BYTES + - JUMPTABLE_WIDTH]; // Width + JUMPTABLE_WIDTH]; // Width // Test if the char is drawable if (!(msbJumpToChar == 255 && lsbJumpToChar == 255)) { @@ -422,7 +498,8 @@ class SSD1306Display { drawString(cx, cy, cc); } cx += fontWidth; - if (auto_display) display(); // todo: not very efficient + if (auto_display) + display(); // todo: not very efficient return 1; } @@ -436,21 +513,23 @@ class SSD1306Display { drawString(cx, cy, s); auto_display = temp_auto_display; cx += fontWidth * nc; - if (auto_display) display(); // todo: not very efficient + if (auto_display) + display(); // todo: not very efficient return nc; } void createChar(uint8_t idx, const char *ptr) { - if (idx >= 0 && idx < NUM_CUSTOM_ICONS) custom_chars[idx] = ptr; + if (idx >= 0 && idx < NUM_CUSTOM_ICONS) + custom_chars[idx] = ptr; } void createChar(unsigned char idx, const unsigned char *ptr) { - createChar(idx, (const char *)ptr); - } + createChar(idx, (const char *)ptr); + } void setAutoDisplay(bool v) { auto_display = v; } - private: +private: int file = -1; bool auto_display = false; uint8_t cx, cy = 0; @@ -464,18 +543,15 @@ class SSD1306Display { I2CDevice i2c; unsigned char _addr; - void clear_buffer() { - memset(frame, 0x00, sizeof(frame)); - } + unsigned char height; + unsigned char width; - int ssd1306_command(unsigned char command) { - return i2c.send(0x00, command); - } + void clear_buffer() { memset(frame, 0x00, sizeof(frame)); } - int ssd1306_data(unsigned char value) { - return i2c.send(0x40, value); - } + int ssd1306_command(unsigned char command) { return i2c.send(0x00, command); } + + int ssd1306_data(unsigned char value) { return i2c.send(0x40, value); } }; #endif -#endif // SSD1306_DISPLAY_H +#endif // SSD1306_DISPLAY_H From c2a4ef4ff15fea6394e21c56adbb9c94d765702c Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 11:29:26 -0400 Subject: [PATCH 69/81] fix capitalization --- SSD1306Display.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/SSD1306Display.h b/SSD1306Display.h index 2c69174a..f97683a5 100644 --- a/SSD1306Display.h +++ b/SSD1306Display.h @@ -218,22 +218,22 @@ class SSD1306Display { switch (height) { case 64: - SSD1306_command(SSD1306_SET_COM_PINS); - SSD1306_command(0x12); - SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); - SSD1306_command(0xCF); + ssd1306_command(SSD1306_SET_COM_PINS); + ssd1306_command(0x12); + ssd1306_command(SSD1306_SET_CONTRAST_CONTROL); + ssd1306_command(0xCF); break; case 32: - SSD1306_command(SSD1306_SET_COM_PINS); - SSD1306_command(0x02); - SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); - SSD1306_command(0x8F); + ssd1306_command(SSD1306_SET_COM_PINS); + ssd1306_command(0x02); + ssd1306_command(SSD1306_SET_CONTRAST_CONTROL); + ssd1306_command(0x8F); break; case 16: // NOTE: not tested, lacking part. - SSD1306_command(SSD1306_SET_COM_PINS); - SSD1306_command(0x2); - SSD1306_command(SSD1306_SET_CONTRAST_CONTROL); - SSD1306_command(0xAF); + ssd1306_command(SSD1306_SET_COM_PINS); + ssd1306_command(0x2); + ssd1306_command(SSD1306_SET_CONTRAST_CONTROL); + ssd1306_command(0xAF); break; } From 40b5a0850895b9ded9e564f2af935e4e4653e721 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Fri, 2 Aug 2024 15:10:43 -0400 Subject: [PATCH 70/81] fix build script --- build.sh | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/build.sh b/build.sh index 6a9d3a54..b9f06ff8 100755 --- a/build.sh +++ b/build.sh @@ -1,24 +1,6 @@ #!/bin/bash set -e -function download_wiringpi { - echo "Downloading WiringPi..." - if [ $(arch) == "aarch64" ]; then - wget https://github.com/WiringPi/WiringPi/releases/download/3.6/wiringpi_3.6_arm64.deb -O wiringpi.deb - else - wget https://github.com/WiringPi/WiringPi/releases/download/3.6/wiringpi_3.6_armhf.deb -O wiringpi.deb - fi - - if [ $? -ne 0 ]; then - echo "Failed to download WiringPi" - exit 1 - fi - - echo "Installing WiringPi..." - dpkg -i wiringpi.deb - rm wiringpi.deb -} - function enable_i2c { sudo raspi-config nonint do_i2c 1 sudo dtparam i2c_baudrate=400000 @@ -77,10 +59,6 @@ else exit 0 fi - if [ ! -f /usr/lib/libwiringPi.so ]; then - download_wiringpi - fi - USEGPIO="" GPIOLIB="" @@ -95,7 +73,7 @@ else ws=$(ls external/TinyWebsockets/tiny_websockets_lib/src/*.cpp) otf=$(ls external/OpenThings-Framework-Firmware-Library/*.cpp) - g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -lgpiod -li2c $GPIOLIB + g++ -o OpenSprinkler -DOSPI $USEGPIO -DSMTP_OPENSSL $DEBUG -std=c++14 -include string.h main.cpp OpenSprinkler.cpp program.cpp opensprinkler_server.cpp utils.cpp weather.cpp gpio.cpp mqtt.cpp smtp.c -Iexternal/TinyWebsockets/tiny_websockets_lib/include $ws -Iexternal/OpenThings-Framework-Firmware-Library/ $otf -lpthread -lmosquitto -lssl -lcrypto -li2c $GPIOLIB fi if [ -f /etc/init.d/OpenSprinkler.sh ]; then From 3dbd97672deec094c4043162d40a57270c674213 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 11:27:44 -0400 Subject: [PATCH 71/81] enable i2c --- build.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.sh b/build.sh index b9f06ff8..e5d6891b 100755 --- a/build.sh +++ b/build.sh @@ -54,6 +54,8 @@ else apt-get update apt-get install -y libmosquitto-dev raspi-gpio libi2c-dev libssl-dev libgpiod-dev + enable_i2c() + if ! command -v raspi-gpio &> /dev/null; then echo "Command raspi-gpio is required and is not installed" exit 0 From 3b905468dc59e91af005e9634c8e822435471f4f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 11:37:04 -0400 Subject: [PATCH 72/81] 0 enables i2c not 1 with raspi-config --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index e5d6891b..c7bea7a4 100755 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ set -e function enable_i2c { - sudo raspi-config nonint do_i2c 1 + sudo raspi-config nonint do_i2c 0 sudo dtparam i2c_baudrate=400000 sudo modprobe i2c-dev } From 8f0c187c8fa79398e42c352b39df0d9f2f1a3abe Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 11:39:00 -0400 Subject: [PATCH 73/81] make sure raspi-config is there to enable i2c --- build.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index c7bea7a4..8fbd108a 100755 --- a/build.sh +++ b/build.sh @@ -2,9 +2,14 @@ set -e function enable_i2c { - sudo raspi-config nonint do_i2c 0 - sudo dtparam i2c_baudrate=400000 - sudo modprobe i2c-dev + if command -v raspi-config &> /dev/null; then + echo "Enabling i2c" + sudo raspi-config nonint do_i2c 0 + sudo dtparam i2c_baudrate=400000 + sudo modprobe i2c-dev + else + echo "Can not automatically enable i2c you might have to do this manually" + fi } DEBUG="" From 5fa55268dd62c8ef6830b5153b7459b100061059 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:18:40 -0400 Subject: [PATCH 74/81] switch order of i2c enable --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 8fbd108a..3394194d 100755 --- a/build.sh +++ b/build.sh @@ -4,9 +4,9 @@ set -e function enable_i2c { if command -v raspi-config &> /dev/null; then echo "Enabling i2c" - sudo raspi-config nonint do_i2c 0 sudo dtparam i2c_baudrate=400000 sudo modprobe i2c-dev + sudo raspi-config nonint do_i2c 0 else echo "Can not automatically enable i2c you might have to do this manually" fi From 9ccadd7f86c394f81ec31fc63947f68a346e555f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:34:47 -0400 Subject: [PATCH 75/81] fix function call --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 3394194d..a451f895 100755 --- a/build.sh +++ b/build.sh @@ -59,7 +59,7 @@ else apt-get update apt-get install -y libmosquitto-dev raspi-gpio libi2c-dev libssl-dev libgpiod-dev - enable_i2c() + enable_i2c if ! command -v raspi-gpio &> /dev/null; then echo "Command raspi-gpio is required and is not installed" From b5a83a54a2f62baa324b25ed4129b6a76e06f7b8 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:54:46 -0400 Subject: [PATCH 76/81] update build script to set clock speed --- build.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index a451f895..9ffb2d04 100755 --- a/build.sh +++ b/build.sh @@ -4,9 +4,12 @@ set -e function enable_i2c { if command -v raspi-config &> /dev/null; then echo "Enabling i2c" - sudo dtparam i2c_baudrate=400000 sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 + if ! grep -q 'dtparam=i2c_arm=on$ /boot/config.txt'; then + echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." + sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt + fi else echo "Can not automatically enable i2c you might have to do this manually" fi From b71e76ad2ab7a08dace0684254d31285fc53a05f Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:56:02 -0400 Subject: [PATCH 77/81] fix grep --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 9ffb2d04..3faba44e 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,7 @@ function enable_i2c { echo "Enabling i2c" sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 - if ! grep -q 'dtparam=i2c_arm=on$ /boot/config.txt'; then + if ! grep -q 'dtparam=i2c_arm=on$' /boot/config.txt; then echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt fi From 1d5c046501d5dff8d5e75be7aae3976512909c32 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:57:42 -0400 Subject: [PATCH 78/81] update if --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 3faba44e..911091b6 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,7 @@ function enable_i2c { echo "Enabling i2c" sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 - if ! grep -q 'dtparam=i2c_arm=on$' /boot/config.txt; then + if ! [ grep 'dtparam=i2c_arm=on$' /boot/config.txt ]; then echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt fi From 7626e938ba86d875cef0aa979b69d682f7e8363a Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:05:54 -0400 Subject: [PATCH 79/81] fix if statement --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 911091b6..e8532ca4 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,7 @@ function enable_i2c { echo "Enabling i2c" sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 - if ! [ grep 'dtparam=i2c_arm=on$' /boot/config.txt ]; then + if [[ -z $(grep 'dtparam=i2c_arm=on$' /boot/config.txt) ]] ; then echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt fi From 0944c739f98c70a666ee338ae59a5839a4d3edeb Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:07:40 -0400 Subject: [PATCH 80/81] negate statement --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index e8532ca4..b9f01ce3 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,7 @@ function enable_i2c { echo "Enabling i2c" sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 - if [[ -z $(grep 'dtparam=i2c_arm=on$' /boot/config.txt) ]] ; then + if [[ ! -z $(grep 'dtparam=i2c_arm=on$' /boot/config.txt) ]] ; then echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt fi From 36fda4825087f02ba5ceed33b43b75526b469be8 Mon Sep 17 00:00:00 2001 From: arfrie22 <43021241+arfrie22@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:16:48 -0400 Subject: [PATCH 81/81] change build script not to override i2c --- build.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index b9f01ce3..e17439e6 100755 --- a/build.sh +++ b/build.sh @@ -3,13 +3,15 @@ set -e function enable_i2c { if command -v raspi-config &> /dev/null; then + if [[ $(sudo raspi-config nonint get_i2c) -eq 1 ]] ; then echo "Enabling i2c" sudo modprobe i2c-dev sudo raspi-config nonint do_i2c 0 - if [[ ! -z $(grep 'dtparam=i2c_arm=on$' /boot/config.txt) ]] ; then - echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." - sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt - fi + fi + if [[ $(grep -c '^dtparam=i2c_arm=on$' /boot/config.txt) -ge 1 ]] ; then + echo "Setting the i2c clock speed to 400 kHz, you will have to reboot for this to take effect." + sudo sed -i -e 's/dtparam=i2c_arm=on$/dtparam=i2c_arm=on,i2c_arm_baudrate=400000/g' /boot/config.txt + fi else echo "Can not automatically enable i2c you might have to do this manually" fi