From e96ba5fd5b11a6e4ec9c51ec3c2cdbd77a018311 Mon Sep 17 00:00:00 2001 From: philippe44 Date: Tue, 14 Jan 2020 16:11:15 -0800 Subject: [PATCH] globalize a few things --- components/display/driver_SSD1306.c | 19 ++++---- .../tarablessd1306/ifaces/default_if_i2c.c | 26 +++++----- components/services/globdefs.h | 34 ++++++++++++++ components/services/monitor.c | 6 +-- components/services/services.c | 47 +++++++++++++++++-- main/esp_app_main.c | 3 ++ 6 files changed, 102 insertions(+), 33 deletions(-) create mode 100644 components/services/globdefs.h diff --git a/components/display/driver_SSD1306.c b/components/display/driver_SSD1306.c index 1abda98c..68209e70 100644 --- a/components/display/driver_SSD1306.c +++ b/components/display/driver_SSD1306.c @@ -23,13 +23,13 @@ #include #include "esp_log.h" #include "display.h" +#include "globdefs.h" #include "ssd1306.h" #include "ssd1306_draw.h" #include "ssd1306_font.h" #include "ssd1306_default_if.h" -#define I2C_PORT 1 #define I2C_ADDRESS 0x3C #define LINELEN 40 static const char *TAG = "display"; @@ -77,25 +77,24 @@ static bool display_init(char *config, char *welcome) { bool res = false; if (strstr(config, "I2C")) { - int scl = -1, sda = -1; int width = -1, height = -1; char *p; // no time for smart parsing - this is for tinkerers - if ((p = strcasestr(config, "scl")) != NULL) scl = atoi(strchr(p, '=') + 1); - if ((p = strcasestr(config, "sda")) != NULL) sda = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "width")) != NULL) width = atoi(strchr(p, '=') + 1); if ((p = strcasestr(config, "height")) != NULL) height = atoi(strchr(p, '=') + 1); - - if (sda != -1 && scl != -1 && width != -1 && height != -1) { - SSD1306_I2CMasterInitDefault( I2C_PORT, sda, scl ); - SSD1306_I2CMasterAttachDisplayDefault( &I2CDisplay, width, height, I2C_ADDRESS, -1); + + if (width != -1 && height != -1) { + SSD1306_I2CMasterInitDefault( i2c_system_port, -1, -1 ) ; + SSD1306_I2CMasterAttachDisplayDefault( &I2CDisplay, width, height, I2C_ADDRESS, -1 ); + SSD1306_SetHFlip( &I2CDisplay, strcasestr(config, "HFlip") ? true : false); + SSD1306_SetVFlip( &I2CDisplay, strcasestr(config, "VFlip") ? true : false); SSD1306_SetFont( &I2CDisplay, &Font_droid_sans_fallback_15x17 ); print_message(welcome); - ESP_LOGI(TAG, "Initialized I2C display %dx%d (sda:%d, scl:%d)", width, height, sda, scl); + ESP_LOGI(TAG, "Initialized I2C display %dx%d", width, height); res = true; } else { - ESP_LOGI(TAG, "Cannot initialized I2C display %s [%dx%d sda:%d, scl:%d]", config, width, height, sda, scl); + ESP_LOGI(TAG, "Cannot initialized I2C display %s [%dx%d]", config, width, height); } } else { // other types diff --git a/components/display/tarablessd1306/ifaces/default_if_i2c.c b/components/display/tarablessd1306/ifaces/default_if_i2c.c index 878d3135..190c479c 100644 --- a/components/display/tarablessd1306/ifaces/default_if_i2c.c +++ b/components/display/tarablessd1306/ifaces/default_if_i2c.c @@ -32,21 +32,21 @@ static bool I2CDefaultReset( struct SSD1306_Device* Display ); * Returns true on successful init of the i2c bus. */ bool SSD1306_I2CMasterInitDefault( int PortNumber, int SDA, int SCL ) { - i2c_config_t Config; - - memset( &Config, 0, sizeof( i2c_config_t ) ); - - Config.mode = I2C_MODE_MASTER; - Config.sda_io_num = SDA; - Config.sda_pullup_en = GPIO_PULLUP_ENABLE; - Config.scl_io_num = SCL; - Config.scl_pullup_en = GPIO_PULLUP_ENABLE; - Config.master.clk_speed = I2CDisplaySpeed; - I2CPortNumber = PortNumber; + + if (SDA != -1 && SCL != -1) { + i2c_config_t Config = { 0 }; - ESP_ERROR_CHECK_NONFATAL( i2c_param_config( I2CPortNumber, &Config ), return false ); - ESP_ERROR_CHECK_NONFATAL( i2c_driver_install( I2CPortNumber, Config.mode, 0, 0, 0 ), return false ); + Config.mode = I2C_MODE_MASTER; + Config.sda_io_num = SDA; + Config.sda_pullup_en = GPIO_PULLUP_ENABLE; + Config.scl_io_num = SCL; + Config.scl_pullup_en = GPIO_PULLUP_ENABLE; + Config.master.clk_speed = I2CDisplaySpeed; + + ESP_ERROR_CHECK_NONFATAL( i2c_param_config( I2CPortNumber, &Config ), return false ); + ESP_ERROR_CHECK_NONFATAL( i2c_driver_install( I2CPortNumber, Config.mode, 0, 0, 0 ), return false ); + } return true; } diff --git a/components/services/globdefs.h b/components/services/globdefs.h new file mode 100644 index 00000000..33470606 --- /dev/null +++ b/components/services/globdefs.h @@ -0,0 +1,34 @@ +/* + * Squeezelite for esp32 + * + * (c) Philippe G. 2019, philippe_44@outlook.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#pragma once + +#define I2C_SYSTEM_PORT 1 +extern int i2c_system_port; + +#ifdef CONFIG_SQUEEZEAMP +#define JACK_GPIO 34 +#define SPKFAULT_GPIO 2 // this requires a pull-up, so can't be >34 +#define LED_GREEN_GPIO 12 +#define LED_RED_GPIO 13 +#endif + + + diff --git a/components/services/monitor.c b/components/services/monitor.c index 341714d1..763d9cd8 100644 --- a/components/services/monitor.c +++ b/components/services/monitor.c @@ -18,11 +18,7 @@ #include "driver/gpio.h" #include "buttons.h" #include "led.h" - -#ifdef CONFIG_SQUEEZEAMP -#define JACK_GPIO 34 -#define SPKFAULT_GPIO 2 // this requires a pull-up, so can't be >34 -#endif +#include "globdefs.h" #define MONITOR_TIMER (10*1000) diff --git a/components/services/services.c b/components/services/services.c index 6257bb8b..5e908aea 100644 --- a/components/services/services.c +++ b/components/services/services.c @@ -9,26 +9,63 @@ #include #include "esp_log.h" #include "driver/gpio.h" +#include +#include "config.h" #include "battery.h" #include "led.h" #include "monitor.h" +#include "globdefs.h" extern void battery_svc_init(void); extern void monitor_svc_init(void); extern void led_svc_init(void); -static const char TAG[] = "services"; +int i2c_system_port = I2C_SYSTEM_PORT; -#ifdef CONFIG_SQUEEZEAMP -#define LED_GREEN_GPIO 12 -#define LED_RED_GPIO 13 -#endif +static const char *TAG = "services"; /**************************************************************************************** * */ void services_init(void) { + int scl = -1, sda = -1, i2c_speed = 250000; + char *nvs_item, *p; + gpio_install_isr_service(0); + + nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config"); + + if (nvs_item) { + if ((p = strcasestr(nvs_item, "scl")) != NULL) scl = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "sda")) != NULL) sda = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c_speed = atoi(strchr(p, '=') + 1); + if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1); + } + +#ifdef CONFIG_SQUEEZEAMP + if (i2c_system_port == 0) { + i2c_system_port = 1; + ESP_LOGE(TAG, "can't use i2c port 0 on SqueezeAMP"); + } +#endif + + ESP_LOGI(TAG,"Configuring I2C sda:%d scl:%d port:%u speed:%u", sda, scl, i2c_system_port, i2c_speed); + + if (sda != -1 && scl != -1) { + i2c_config_t i2c = { 0 }; + + i2c.mode = I2C_MODE_MASTER; + i2c.sda_io_num = sda; + i2c.sda_pullup_en = GPIO_PULLUP_ENABLE; + i2c.scl_io_num = scl; + i2c.scl_pullup_en = GPIO_PULLUP_ENABLE; + i2c.master.clk_speed = i2c_speed; + + i2c_param_config(i2c_system_port, &i2c); + i2c_driver_install(i2c_system_port, i2c.mode, 0, 0, 0 ); + } else { + ESP_LOGE(TAG, "can't initialize I2C"); + } ESP_LOGD(TAG,"Configuring LEDs"); led_svc_init(); diff --git a/main/esp_app_main.c b/main/esp_app_main.c index dc775c44..c1609185 100644 --- a/main/esp_app_main.c +++ b/main/esp_app_main.c @@ -312,6 +312,9 @@ void register_default_nvs(){ ESP_LOGD(TAG,"Registering default value for key %s, value %s", "display_config", STR(CONFIG_DISPLAY_CONFIG)); config_set_default(NVS_TYPE_STR, "display_config", STR(CONFIG_DISPLAY_CONFIG), 0); + ESP_LOGD(TAG,"Registering default value for key %s", "i2c_config"); + config_set_default(NVS_TYPE_STR, "i2c_config", "", 0); + ESP_LOGD(TAG,"Done setting default values in nvs."); }