浏览代码

globalize a few things

philippe44 5 年之前
父节点
当前提交
e96ba5fd5b

+ 9 - 10
components/display/driver_SSD1306.c

@@ -23,13 +23,13 @@
 #include <arpa/inet.h>
 #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

+ 14 - 14
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;
-
-    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 );
+	
+	if (SDA != -1 && SCL != -1) {
+		i2c_config_t Config = { 0 };
+
+        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;
 }

+ 34 - 0
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 <http://www.gnu.org/licenses/>.
+ *
+ */
+ 
+#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
+
+
+

+ 1 - 5
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)
 

+ 42 - 5
components/services/services.c

@@ -9,26 +9,63 @@
 #include <stdio.h>
 #include "esp_log.h"
 #include "driver/gpio.h"
+#include <driver/i2c.h>
+#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();

+ 3 - 0
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.");
 }