summaryrefslogtreecommitdiff
path: root/firmware/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers')
-rw-r--r--firmware/drivers/button.c52
1 files changed, 31 insertions, 21 deletions
diff --git a/firmware/drivers/button.c b/firmware/drivers/button.c
index 33b708ea5c..b9473bec24 100644
--- a/firmware/drivers/button.c
+++ b/firmware/drivers/button.c
@@ -61,7 +61,7 @@ static bool remote_filter_first_keypress;
61#endif 61#endif
62#endif /* HAVE_BACKLIGHT */ 62#endif /* HAVE_BACKLIGHT */
63#ifdef HAVE_HEADPHONE_DETECTION 63#ifdef HAVE_HEADPHONE_DETECTION
64bool phones_present = false; 64static bool phones_present = false;
65#endif 65#endif
66 66
67/* how long until repeat kicks in, in ticks */ 67/* how long until repeat kicks in, in ticks */
@@ -79,6 +79,20 @@ static int button_read(int *data);
79static int button_read(void); 79static int button_read(void);
80#endif 80#endif
81 81
82#if defined(HAVE_HEADPHONE_DETECTION)
83static struct timeout hp_detect_timeout; /* Debouncer for headphone plug/unplug */
84/* This callback can be used for many different functions if needed -
85 just check to which object tmo points */
86static bool btn_detect_callback(struct timeout *tmo)
87{
88 /* Try to post only transistions */
89 const long id = tmo->data ? SYS_PHONE_PLUGGED : SYS_PHONE_UNPLUGGED;
90 queue_remove_from_head(&button_queue, id);
91 queue_post(&button_queue, id, 0);
92 return false;
93}
94#endif
95
82static void button_tick(void) 96static void button_tick(void)
83{ 97{
84 static int count = 0; 98 static int count = 0;
@@ -109,29 +123,22 @@ static void button_tick(void)
109 } 123 }
110#endif 124#endif
111 125
112#ifdef HAVE_HEADPHONE_DETECTION
113 if ( headphones_inserted() )
114 {
115 if (! phones_present )
116 {
117 queue_post(&button_queue, SYS_PHONE_PLUGGED, 0);
118 phones_present = true;
119 }
120 } else {
121 if ( phones_present )
122 {
123 queue_post(&button_queue, SYS_PHONE_UNPLUGGED, 0);
124 phones_present = false;
125 }
126 }
127#endif
128
129#ifdef HAVE_BUTTON_DATA 126#ifdef HAVE_BUTTON_DATA
130 btn = button_read(&data); 127 btn = button_read(&data);
131#else 128#else
132 btn = button_read(); 129 btn = button_read();
133#endif 130#endif
134 131
132#if defined(HAVE_HEADPHONE_DETECTION)
133 if (headphones_inserted() != phones_present)
134 {
135 /* Use the autoresetting oneshot to debounce the detection signal */
136 phones_present = !phones_present;
137 timeout_register(&hp_detect_timeout, btn_detect_callback,
138 HZ, phones_present);
139 }
140#endif
141
135 /* Find out if a key has been released */ 142 /* Find out if a key has been released */
136 diff = btn ^ lastbtn; 143 diff = btn ^ lastbtn;
137 if(diff && (btn & diff) == 0) 144 if(diff && (btn & diff) == 0)
@@ -369,14 +376,15 @@ intptr_t button_get_data(void)
369 376
370void button_init(void) 377void button_init(void)
371{ 378{
379 /* Init used objects first */
380 queue_init(&button_queue, true);
381
372#ifdef HAVE_BUTTON_DATA 382#ifdef HAVE_BUTTON_DATA
373 int temp; 383 int temp;
374#endif 384#endif
375 /* hardware inits */ 385 /* hardware inits */
376 button_init_device(); 386 button_init_device();
377 387
378 queue_init(&button_queue, true);
379
380#ifdef HAVE_BUTTON_DATA 388#ifdef HAVE_BUTTON_DATA
381 button_read(&temp); 389 button_read(&temp);
382 lastbtn = button_read(&temp); 390 lastbtn = button_read(&temp);
@@ -385,7 +393,6 @@ void button_init(void)
385 lastbtn = button_read(); 393 lastbtn = button_read();
386#endif 394#endif
387 395
388 tick_add_task(button_tick);
389 reset_poweroff_timer(); 396 reset_poweroff_timer();
390 397
391#ifdef HAVE_LCD_BITMAP 398#ifdef HAVE_LCD_BITMAP
@@ -397,6 +404,9 @@ void button_init(void)
397 remote_filter_first_keypress = false; 404 remote_filter_first_keypress = false;
398#endif 405#endif
399#endif 406#endif
407
408 /* Start polling last */
409 tick_add_task(button_tick);
400} 410}
401 411
402#ifdef HAVE_LCD_BITMAP /* only bitmap displays can be flipped */ 412#ifdef HAVE_LCD_BITMAP /* only bitmap displays can be flipped */