Browse Source

button tweaks

philippe44 5 years ago
parent
commit
b7f391d909
2 changed files with 23 additions and 13 deletions
  1. 20 10
      components/services/buttons.c
  2. 3 3
      components/services/monitor.c

+ 20 - 10
components/services/buttons.c

@@ -42,10 +42,11 @@ static int n_buttons = 0;
 
 static EXT_RAM_ATTR struct button_s {
 	void *id;
-	int gpio, index;
+	int gpio;
 	int debounce;
 	button_handler handler;
-	struct button_s *shifter;
+	struct button_s *self, *shifter;
+	int shifter_gpio;	// this one is just for post-creation						
 	int	long_press;
 	bool long_timer, shifted, shifting;
 	int type, level;	
@@ -126,7 +127,7 @@ static void buttons_task(void* arg) {
 					(*button.handler)(button.id, BUTTON_RELEASED, press, false);		
 				}
 				// button is a copy, so need to go to real context
-				buttons[button.index].shifting = false;
+				button.self->shifting = false;
 			} else if (!button.shifting) {
 				// normal long press and not shifting so don't discard
 				(*button.handler)(button.id, BUTTON_PRESSED, press, true);
@@ -135,7 +136,7 @@ static void buttons_task(void* arg) {
 			// normal press/release of a button or release of a long-press button
 			if (!button.shifting) (*button.handler)(button.id, event, press, button.long_press);
 			// button is a copy, so need to go to real context
-			buttons[button.index].shifting = false;
+			button.self->shifting = false;
 		}
     }
 }	
@@ -173,19 +174,24 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
 	buttons[n_buttons].handler = handler;
 	buttons[n_buttons].long_press = long_press;
 	buttons[n_buttons].level = -1;
+	buttons[n_buttons].shifter_gpio = shifter_gpio;
 	buttons[n_buttons].type = type;
 	buttons[n_buttons].timer = xTimerCreate("buttonTimer", buttons[n_buttons].debounce / portTICK_RATE_MS, pdFALSE, (void *) &buttons[n_buttons], buttons_timer);
-	// little trick to find ourselves from queued copy
-	buttons[n_buttons].index = n_buttons;
+	buttons[n_buttons].self = buttons + n_buttons;
 
 	for (int i = 0; i < n_buttons; i++) {
+		// first try to find our shifter
 		if (buttons[i].gpio == shifter_gpio) {
 			buttons[n_buttons].shifter = buttons + i;
 			// a shifter must have a long-press handler
 			if (!buttons[i].long_press) buttons[i].long_press = -1;
-			break;
 		}
-	}	
+		// then try to see if we are a non-assigned shifter
+		if (buttons[i].shifter_gpio == gpio) {
+			buttons[i].shifter = buttons + n_buttons;
+			ESP_LOGI(TAG, "post-assigned shifter gpio %u", buttons[i].gpio);			
+		}	
+	}
 
 	gpio_pad_select_gpio(gpio);
 	gpio_set_direction(gpio, GPIO_MODE_INPUT);
@@ -195,8 +201,12 @@ void button_create(void *id, int gpio, int type, bool pull, int debounce, button
 
 	// do we need pullup or pulldown
 	if (pull) {
-		if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
-		else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
+		if (GPIO_IS_VALID_OUTPUT_GPIO(gpio)) {
+			if (type == BUTTON_LOW) gpio_set_pull_mode(gpio, GPIO_PULLUP_ONLY);
+			else gpio_set_pull_mode(gpio, GPIO_PULLDOWN_ONLY);
+		} else {	
+			ESP_LOGW(TAG, "cannot set pull up/down for gpio %u", gpio);
+		}
 	}
  
 	gpio_isr_handler_add(gpio, gpio_isr_handler, (void*) &buttons[n_buttons]);

+ 3 - 3
components/services/monitor.c

@@ -21,7 +21,7 @@
 
 #ifdef CONFIG_SQUEEZEAMP
 #define JACK_GPIO		34
-#define SPKFAULT_GPIO	2
+#define SPKFAULT_GPIO	2			// this requires a pull-up, so can't be >34
 #endif
 
 #define MONITOR_TIMER	(10*1000)
@@ -104,8 +104,8 @@ void monitor_svc_init(void) {
 
 #ifdef SPKFAULT_GPIO
 	gpio_pad_select_gpio(SPKFAULT_GPIO);
-	ESP_LOGI(TAG, "DIR %d", gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT));
-	ESP_LOGI(TAG, "PULLUP %d", gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY));
+	gpio_set_direction(SPKFAULT_GPIO, GPIO_MODE_INPUT);
+	gpio_set_pull_mode(SPKFAULT_GPIO, GPIO_PULLUP_ONLY); 
 	
 	// re-use button management for speaker fault handler, it's a GPIO after all
 	button_create(NULL, SPKFAULT_GPIO, BUTTON_LOW, true, 0, spkfault_handler_default, 0, -1);