-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftbuttons.py
725 lines (681 loc) · 38.6 KB
/
tftbuttons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#!/usr/bin/python
##################################################################################
# IMPORTS
##################################################################################
import signal
import time
from functools import partial
import RPi.GPIO as GPIO
import tftmenu
from tftutility import *
logger.debug("Loading Buttons Module")
##################################################################################
# GPIO ECHO PORT CONSTANTS
##################################################################################
GPIO_ECHO_BACKLIGHT = 508
GPIO_ECHO_BACKLIGHT_ALT = 252
##################################################################################
# ADAFRUIT CONFIGURATION CONSTANTS
##################################################################################
ADAFRUIT_CONF_FILE = "/etc/modprobe.d/adafruit.conf"
ADAFRUIT_CONF_GPIO = "options rpi_power_switch gpio_pin="
##################################################################################
# DEFAULT CONSTANTS
##################################################################################
DEFAULT_BACKLIGHT_STEPS = 4
DEFAULT_POWER_GPIO = 0
DEFAULT_PI_BATTERY_GPIO = 0
##################################################################################
# GPIO PORT CONSTANTS
##################################################################################
GPIO_PWM_BACKLIGHT = 18
##################################################################################
# BACKLIGHT STATE CONSTANTS
##################################################################################
GPIO_BACKLIGHT_OFF = 0
GPIO_BACKLIGHT_ON = 1
GPIO_BACKLIGHT_PWM_ON = GPIO_BACKLIGHT_ON
GPIO_BACKLIGHT_PWM_ON_OLD = GPIO_BACKLIGHT_OFF
GPIO_BACKLIGHT_HIGH = 1023
##################################################################################
# GPIO COMMAND CONSTANTS
##################################################################################
GPIO_SUDO_SHELL = ['sudo', 'sh', '-c']
##################################################################################
# GPIO BACKLIGHT COMMAND CONSTANTS
##################################################################################
GPIO_BACKLIGHT_ECHO_PATH = "/sys/class/gpio/"
GPIO_BACKLIGHT_ECHO_OUTPUT = "echo 'out' > {0}gpio$pin/direction".format(GPIO_BACKLIGHT_ECHO_PATH)
GPIO_BACKLIGHT_ECHO_LINK = "echo {{0}} > {0}export".format(GPIO_BACKLIGHT_ECHO_PATH)
GPIO_BACKLIGHT_ECHO_SET = "echo '{{0}}' > {0}gpio{{1}}/value".format(GPIO_BACKLIGHT_ECHO_PATH)
GPIO_BACKLIGHT_STMPE_PATH = "/sys/class/backlight/soc:backlight"
GPIO_BACKLIGHT_STMPE_COMMAND = "echo '{0}' > /sys/class/backlight/soc\:backlight/brightness"
GPIO_BACKLIGHT_PWM_MODE = "pwm"
GPIO_BACKLIGHT_PWM_BINARY_MODE = "out"
GPIO_BACKLIGHT_PWM_FREQUENCY = "1000"
GPIO_BACKLIGHT_PWM_MODE_SET = "gpio -g mode {0} {1}"
GPIO_BACKLIGHT_PWM_SET = "gpio -g pwm {0} {1}"
GPIO_BACKLIGHT_PWM_BINARY_SET = "gpio -g write {0} {1}"
GPIO_BACKLIGHT_PWMC_SET = "gpio pwmc {0}"
GPIO_BACKLIGHT_CHANGE_STEPS_MIN = 2
GPIO_BACKLIGHT_CHANGE_STEPS_MAX = GPIO_BACKLIGHT_HIGH
##################################################################################
# TIMER CLASS
##################################################################################
class Timer:
__timeout = 0
__alarmed = False
__triggered = False
__abort = False
__ignore_reset = False
##################################################################################
# TIMER ALARM METHOD
##################################################################################
# Method called from a system Alarm signal. When an alarm occurs, the __triggered
# variable is set to True and can be detected in the is_expired method.
##################################################################################
@classmethod
def alarm(cls, signum, frame):
logger.debug("Alarm Signal Received. Signum: {0}, Frame: {1}".format(signum, frame))
cls.__triggered = True
cls.__ignore_reset = False
signal.alarm(0)
##################################################################################
# TIMER RESET METHOD
##################################################################################
# Method to reset the Alarm signal timer
##################################################################################
@classmethod
def reset(cls):
if not cls.__ignore_reset:
logger.debug("Timer reset")
cls.__triggered = False
signal.alarm(0)
signal.alarm(cls.__timeout)
else:
logger.debug("Timer reset ignored.")
##################################################################################
# TIMER TIMEOUT METHOD
##################################################################################
# Method that initially sets the Alarm signal callback. The ignore_reset
# parameter can be set to True to ignore reset signals.
##################################################################################
@classmethod
def timeout(cls, timeout, ignore_reset=False):
logger.debug("Timeout set. Timeout: {0}, Ignore Reset: {1}".format(timeout, ignore_reset))
cls.__timeout = timeout
if not cls.__alarmed:
signal.signal(signal.SIGALRM, Timer.alarm)
cls.__alarmed = True
logger.debug("Alarm Signal Initialized")
cls.reset()
cls.__ignore_reset = ignore_reset
##################################################################################
# TIMER IS_EXPIRED METHOD
##################################################################################
# Method to detect if a timer alarm has occurred.
##################################################################################
@classmethod
def is_expired(cls):
if cls.__timeout == 0 or not cls.__triggered:
return False
else:
cls.reset()
return True
##################################################################################
# BACKLIGHT CLASS
##################################################################################
# Class to manage the backlight of the PiTft. Handles dimming, brightening,
# sleeping and waking of the device.
##################################################################################
class Backlight:
method = 0
steps = None
restore_last = False
default = None
callback = None
state = 0
last_state = 0
change = 0.0
initialized = False
state_sleep = False
pwm_on = GPIO_BACKLIGHT_PWM_ON
##################################################################################
# BACKLIGHT INITIALIZE METHOD
##################################################################################
# Method for initializing the Backlight class. Checks if already initialized and
# set to initialized if not. Set the backlight options that are passed in then
# set the backlight method and tweak options based on the backlight method.
##################################################################################
@classmethod
def initialize(cls, method=None, steps=None, restore_last=False, state_sleep=False, default=None, use_old_pwm=False, callback=None):
if cls.initialized:
logger.error("Backlight Class has already been initialized.")
raise AlreadyInitializedException("Backlight Class has already been initialized.")
cls.initialized = True
# Set Class Variables
cls.method = method
cls.restore_last = restore_last
cls.steps = steps
cls.state_sleep = state_sleep
cls.default = default
cls.callback = merge(Timer.reset, callback)
if use_old_pwm is True:
cls.pwm_on = GPIO_BACKLIGHT_PWM_ON_OLD
if cls.method is None:
cls.method = BacklightMethod.NoBacklight
if cls.steps is None:
cls.steps = DEFAULT_BACKLIGHT_STEPS
if cls.default is None:
cls.default = DEFAULT_BACKLIGHT_STEPS
if cls.default > cls.steps:
cls.default = cls.steps
if cls.method is not BacklightMethod.NoBacklight and not is_package_installed(Package.PkgWiringPi):
logger.warning("WiringPi package is not installed. Backlight functionality will be disabled.")
cls.method = BacklightMethod.NoBacklight
if cls.method != BacklightMethod.NoBacklight:
subprocess.call(GPIO_BACKLIGHT_PWMC_SET.format(GPIO_BACKLIGHT_PWM_FREQUENCY).split())
# If using PWM non-binary method, we need to get exact backlight values
if cls.method == BacklightMethod.Pwm:
# Determine the number of steps for the backlight_up/BacklightDown buttons
if cls.steps < GPIO_BACKLIGHT_CHANGE_STEPS_MIN:
# Always need at least two steps (on and off)
cls.steps = GPIO_BACKLIGHT_CHANGE_STEPS_MIN
if cls.steps > GPIO_BACKLIGHT_CHANGE_STEPS_MAX:
# Maximum amount of steps is the maximum number of backlight settings
cls.steps = GPIO_BACKLIGHT_CHANGE_STEPS_MAX
# Determine the amount to change the backlight at each step
# noinspection PyTypeChecker
cls.change = GPIO_BACKLIGHT_CHANGE_STEPS_MAX / ((float(cls.steps)) - 1.0)
# noinspection PyTypeChecker
cls.default = int((float(cls.default) - 1) * cls.change)
# Make sure default is in the proper range, if not set to off (too low) or (too) high
if cls.default < GPIO_BACKLIGHT_OFF:
cls.default = GPIO_BACKLIGHT_OFF
if cls.default > GPIO_BACKLIGHT_HIGH:
cls.default = GPIO_BACKLIGHT_HIGH
initial_value = cls.default
# All other methods are binary and are either on or off
else:
cls.steps = GPIO_BACKLIGHT_CHANGE_STEPS_MIN
cls.change = GPIO_BACKLIGHT_HIGH
# The default backlight can be set to either off or high when binary.
if cls.default == GPIO_BACKLIGHT_OFF:
initial_value = GPIO_BACKLIGHT_OFF
else:
cls.default = GPIO_BACKLIGHT_HIGH
initial_value = GPIO_BACKLIGHT_ON
# Set backlight current and last states
cls.state = cls.default
if cls.default == GPIO_BACKLIGHT_OFF:
cls.last_state = GPIO_BACKLIGHT_HIGH
else:
cls.last_state = cls.default
# Depending on whether the screen uses echo to set backlight. Note on the GPIO
# screens, the jumper enabling GPIO 18 to control the backlight must be soldered to
# control the backlight.
if cls.method == BacklightMethod.Pwm:
# Set PWM Mode on GPIO 18, which controls the backlight and set the brightness
# to default if present, or maximum brightness if no default
subprocess.call(GPIO_BACKLIGHT_PWM_MODE_SET.format(GPIO_PWM_BACKLIGHT, GPIO_BACKLIGHT_PWM_MODE).split())
subprocess.call(GPIO_BACKLIGHT_PWMC_SET.format(GPIO_BACKLIGHT_PWM_FREQUENCY).split())
subprocess.call(GPIO_BACKLIGHT_PWM_SET.format(GPIO_PWM_BACKLIGHT, initial_value).split())
# If STMPE path exists, set it to 0 which seems to relinquish control of the
# backlight to PWM
if os.path.isdir(GPIO_BACKLIGHT_STMPE_PATH):
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_STMPE_COMMAND.format(cls.pwm_on)])
elif cls.method == BacklightMethod.PwmBinary:
subprocess.call(GPIO_BACKLIGHT_PWM_MODE_SET.format(GPIO_PWM_BACKLIGHT,
GPIO_BACKLIGHT_PWM_BINARY_MODE).split())
# Set PWM Mode on GPIO 18, which controls the backlight and set the brightness
# to default if present, or maximum brightness if no default
subprocess.call(GPIO_BACKLIGHT_PWM_BINARY_SET.format(GPIO_PWM_BACKLIGHT, initial_value).split())
# If STMPE path exists, set it to 0 which seems to relinquish control of the
# backlight to PWM
if os.path.isdir(GPIO_BACKLIGHT_STMPE_PATH):
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_STMPE_COMMAND.format(cls.pwm_on)])
elif cls.method == BacklightMethod.Stmpe:
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_STMPE_COMMAND.format(initial_value)])
elif (cls.method == BacklightMethod.Echo) or (cls.method == BacklightMethod.Echo252):
# Create the device link to the GPIO 508 pin. On real old kernels, this is 252 (in case
# someone is still running a pre-2014 kernel) and can be changed by setting the
# method to BacklightMethod.Echo252
backlight_pin = GPIO_ECHO_BACKLIGHT
if cls.method == BacklightMethod.Echo252:
backlight_pin = GPIO_ECHO_BACKLIGHT_ALT
# ECHO GPIO is binary (ON/OFF) so change is always the maximum brightness
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_ECHO_LINK.format(backlight_pin)])
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_ECHO_OUTPUT.format(backlight_pin)])
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_ECHO_SET.format(initial_value, backlight_pin)])
else:
logger.warning("Unknown Backlight Method. Setting backlight method to No Backlight. Method: {0}"
.format(cls.method))
cls.method = BacklightMethod.NoBacklight
send_wake_command()
return True
##################################################################################
# BACKLIGHT IS_SCREEN_SLEEPING METHOD
##################################################################################
# Determines if the backlight is in a sleeping state that is maintained by the
# class.
##################################################################################
@classmethod
def is_screen_sleeping(cls):
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to detect sleep with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. "
"Unable to determine sleep state.")
return cls.state == GPIO_BACKLIGHT_OFF
##################################################################################
# BACKLIGHT SET_BACKLIGHT METHOD
##################################################################################
# Method that sets the backlight using the code required by the backlight method.
##################################################################################
@classmethod
def set_backlight(cls, state):
if not cls.initialized:
return
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to set backlight with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. Unable to set backlight.")
if state == cls.state:
return
time.sleep(Times.SleepShort)
if cls.method == BacklightMethod.Pwm:
logger.debug("Setting backlight to state {0} from {1} using PWM".format(state, cls.state))
if state == GPIO_BACKLIGHT_OFF:
subprocess.call(GPIO_BACKLIGHT_PWM_SET.format(GPIO_PWM_BACKLIGHT, GPIO_BACKLIGHT_OFF).split())
cls.state = GPIO_BACKLIGHT_OFF
else:
subprocess.call(GPIO_BACKLIGHT_PWM_SET.format(GPIO_PWM_BACKLIGHT, state).split())
cls.state = state
cls.last_state = state
elif cls.method == BacklightMethod.PwmBinary:
logger.debug("Setting backlight to state {0} using PWM Binary Mode".format(state, cls.state))
if state == GPIO_BACKLIGHT_OFF:
subprocess.call(GPIO_BACKLIGHT_PWM_BINARY_SET.format(GPIO_PWM_BACKLIGHT, GPIO_BACKLIGHT_OFF).split())
cls.state = GPIO_BACKLIGHT_OFF
else:
subprocess.call(GPIO_BACKLIGHT_PWM_BINARY_SET.format(GPIO_PWM_BACKLIGHT, GPIO_BACKLIGHT_ON).split())
cls.state = GPIO_BACKLIGHT_HIGH
cls.last_state = GPIO_BACKLIGHT_HIGH
elif cls.method == BacklightMethod.Stmpe:
logger.debug("Setting backlight to state {0} using STMPE".format(state, cls.state))
if state == GPIO_BACKLIGHT_OFF:
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_STMPE_COMMAND.format(GPIO_BACKLIGHT_OFF)])
cls.state = GPIO_BACKLIGHT_OFF
else:
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_STMPE_COMMAND.format(GPIO_BACKLIGHT_ON)])
cls.state = GPIO_BACKLIGHT_HIGH
cls.last_state = GPIO_BACKLIGHT_HIGH
elif (cls.method == BacklightMethod.Echo) or (cls.method == BacklightMethod.Echo252):
logger.debug("Setting backlight to state {0} using Echo".format(state, cls.state))
backlight_pin = GPIO_ECHO_BACKLIGHT
if cls.method == BacklightMethod.Echo252:
backlight_pin = GPIO_ECHO_BACKLIGHT_ALT
if state == GPIO_BACKLIGHT_OFF:
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_ECHO_SET.format(backlight_pin, GPIO_BACKLIGHT_OFF)])
cls.state = GPIO_BACKLIGHT_OFF
else:
subprocess.call(GPIO_SUDO_SHELL + [GPIO_BACKLIGHT_ECHO_SET.format(backlight_pin, GPIO_BACKLIGHT_ON)])
cls.state = GPIO_BACKLIGHT_HIGH
cls.last_state = GPIO_BACKLIGHT_HIGH
else:
logger.warning("Unable to set backlight. Unknown Backlight Method: {0}".format(cls.method))
##################################################################################
# BACKLIGHT BACKLIGHT_UP METHOD
##################################################################################
# Method to increase the brightness of the backlight by the amount in the
# backlight steps
##################################################################################
@classmethod
def backlight_up(cls, button=None):
logger.debug("Method backlight_up executed with button: {0} ".format(button if button is not None else "0"))
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to set backlight up with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. "
"Unable to adjust backlight up.")
if Backlight.is_screen_sleeping():
send_wake_command()
if cls.state == GPIO_BACKLIGHT_HIGH:
new_state = GPIO_BACKLIGHT_OFF
else:
new_state = int(cls.state + cls.change)
if new_state > GPIO_BACKLIGHT_HIGH:
new_state = GPIO_BACKLIGHT_HIGH
# If using soft buttons, we don't want to sleep screen so ignore anything that
# would turn off backlight is ignored, even if state_sleep is True.
if (cls.state_sleep is False or (button is None or button == 0)) and new_state == GPIO_BACKLIGHT_OFF:
return
Backlight.set_backlight(new_state)
cls.call_button_press()
##################################################################################
# BACKLIGHT BACKLIGHT_DOWN METHOD
##################################################################################
# Method to decrease the brightness of the backlight by the amount in the
# backlight steps
##################################################################################
@classmethod
def backlight_down(cls, button=None):
logger.debug("Method backlight_down executed with button: {0} ".format(button if button is not None else "0"))
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to set backlight down with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. "
"Unable to adjust backlight down.")
if Backlight.is_screen_sleeping():
send_wake_command()
if cls.state == GPIO_BACKLIGHT_OFF:
new_state = GPIO_BACKLIGHT_HIGH
else:
new_state = int(cls.state - cls.change)
if new_state < GPIO_BACKLIGHT_OFF:
new_state = GPIO_BACKLIGHT_OFF
# If using soft buttons, we don't want to sleep screen so ignore anything that
# would turn off backlight is ignored, even if state_sleep is True.
if (cls.state_sleep is False or (button is None or button == 0)) and new_state == GPIO_BACKLIGHT_OFF:
return
Backlight.set_backlight(new_state)
cls.call_button_press()
##################################################################################
# BACKLIGHT SCREEN_WAKE METHOD
##################################################################################
# Send the screen wake command as it has nothing to do with the backlight but
# allows the menu to wake up from the system screen sleep if enabled. This is
# done regardless of any backlight manipulation.
##################################################################################
@classmethod
def screen_wake(cls, button=None):
logger.debug("Method screen_wake executed with button: {0} ".format(button if button is not None else "0"))
send_wake_command()
if cls.method != BacklightMethod.NoBacklight:
# Check if we should restore the backlight to the setting it was set to
# before it sleeping. If that setting was Off, then go back to full
# brightness.
if cls.restore_last:
if cls.last_state == GPIO_BACKLIGHT_OFF:
cls.set_backlight(GPIO_BACKLIGHT_HIGH)
else:
cls.set_backlight(cls.last_state)
else:
if cls.state != cls.default:
# Set backlight to high/on if it is not already
cls.set_backlight(cls.default)
cls.call_button_press()
##################################################################################
# BACKLIGHT SCREEN_SLEEP METHOD
##################################################################################
# Sends the command to the screen to sleep the screen
##################################################################################
@classmethod
def screen_sleep(cls, button=None):
logger.debug("Method screen_sleep executed with button: {0} ".format(button if button is not None else "0"))
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to sleep screen up with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. Unable to sleep screen.")
# Set backlight to off if it is not already
if not cls.is_screen_sleeping():
cls.set_backlight(GPIO_BACKLIGHT_OFF)
cls.call_button_press()
##################################################################################
# BACKLIGHT SCREEN_TOGGLE METHOD
##################################################################################
# Sends the command to the screen to sleep the screen if it awake or wake the
# screen if it is sleeping.
##################################################################################
@classmethod
def screen_toggle(cls, button=None):
logger.debug("Method screen_toggle executed with button: {0} ".format(button if button is not None else "0"))
if cls.method == BacklightMethod.NoBacklight:
logger.error("Attempt to toggle screen up with a Backlight method of BacklightMethod.NoBacklight.")
raise BacklightNotEnabled("Backlight method set to BacklightMethod.NoBacklight. Unable to toggle screen.")
# Set backlight to off if it is not already
if cls.is_screen_sleeping():
cls.screen_wake(None)
else:
cls.screen_sleep(None)
cls.call_button_press()
##################################################################################
# BACKLIGHT CALL_BUTTON_PRESS METHOD
##################################################################################
# Classmethod that calls any callbacks in the list of callbacks when a button is
# pressed. Timer.reset is added by default when creating the Button class and is
# used internally. Additional handlers can be implemented by adding custom
# callback functions.
##################################################################################
@classmethod
def call_button_press(cls):
if cls.callback is not None:
for callback_function in cls.callback:
logger.debug("Calling button press function {0}".format(callback_function))
callback_function()
##################################################################################
# GPIO ACTION CLASS
##################################################################################
# Class that holds the actions for a GPIO button press, including and additional
# data or data for drawing a new display (render_data).
##################################################################################
class GpioAction(object):
action = None
data = None
render_data = None
##################################################################################
# GPIO ACTION INIT METHOD
##################################################################################
# Sets the GPIO Action properties to defaults.
##################################################################################
def __init__(self, action=GpioButtonAction.NoAction, data=None, render_data=None):
self.action = action
self.data = data
self.render_data = render_data
##################################################################################
# GPIO BUTTONS CLASS
##################################################################################
# Class to hold the state, methods and creation of the gpio buttons. These
# buttons can be virtual, physical (on the tft) or external (such as on a bread-
# board.
##################################################################################
class GpioButtons(object):
tft_type = None
buttons = []
actions = []
power_gpio = 0
battery_gpio = 0
gpio_initialized = False
initialized = False
##################################################################################
# BUTTONS GPIO_BUTTON METHOD
##################################################################################
# Method called by the GPIO.add_event_detect. The button contains the sequential
# id of the button, starting at 0, the actions parameter contains the list of
# actions to be executed by the button press and channel contains the GPIO id of
# the pressed button.
##################################################################################
@classmethod
def gpio_button(cls, button, actions, channel):
if actions is not None:
logger.debug(actions)
if not isinstance(actions, list):
actions = [actions]
for action_function in actions:
if action_function is not None and isinstance(action_function, GpioAction):
if action_function.action == GpioButtonAction.NoAction:
continue
elif action_function.action == GpioButtonAction.Display:
tftmenu.Displays.show(action_function.data, action_function.render_data)
elif action_function.action == GpioButtonAction.Exit:
tftmenu.Displays.shutdown(Shutdown.Normal)
elif action_function.action == GpioButtonAction.Reboot:
tftmenu.Displays.shutdown(Shutdown.Reboot)
elif action_function.action == GpioButtonAction.Shutdown:
tftmenu.Displays.shutdown(Shutdown.Shutdown)
elif action_function.action == GpioButtonAction.Function:
if action_function.data is not None and action_function.data:
return_val = action_function.data(tftmenu.Displays.current, channel)
if return_val is not None:
if isinstance(return_val, tftmenu.Display):
tftmenu.Displays.show(return_val, action_function.render_data)
action_function.data(button, channel)
elif action_function.action == GpioButtonAction.ScreenSleep:
Backlight.screen_sleep(channel)
elif action_function.action == GpioButtonAction.ScreenWake:
Backlight.screen_wake(channel)
elif action_function.action == GpioButtonAction.ScreenToggle:
Backlight.screen_toggle(channel)
elif action_function.action == GpioButtonAction.BacklightDown:
Backlight.backlight_down(channel)
elif action_function.action == GpioButtonAction.BacklightUp:
Backlight.backlight_up(channel)
elif action_function.action == GpioButtonAction.Shell:
tftmenu.Displays.shell()
elif action_function.action == GpioButtonAction.StartX:
tftmenu.Displays.start_x(action_function.data)
elif action_function.action == GpioButtonAction.Execute:
run_cmd(action_function.data)
##################################################################################
# BUTTONS INIT METHOD
##################################################################################
# Method that sets the initial properties of the GpioButtons class. These
# properties include the GPIOs for power on/of, battery monitoring and buttons as
# well as the associated actions.
##################################################################################
def __init__(self, tft_type, buttons=None, actions=None, backlight_method=BacklightMethod.NoBacklight,
backlight_steps=None, backlight_default=None, backlight_restore_last=False,
backlight_state_sleep=False, backlight_auto=False, use_old_pwm=False,
button_callback=None, power_gpio=None, battery_gpio=None):
if GpioButtons.gpio_initialized:
logger.error("TftButtons Class has already been initialized.")
raise AlreadyInitializedException("TftButtons Class has already been initialized.")
GpioButtons.initialized = True
self.tft_type = tft_type
self.buttons = array_single_none(buttons, no_coerce_none=True)
self.actions = array_single_none(actions, no_coerce_none=True)
if power_gpio is None:
self.power_gpio = DEFAULT_POWER_GPIO
else:
self.power_gpio = power_gpio
if battery_gpio is None:
self.battery_gpio = DEFAULT_PI_BATTERY_GPIO
else:
self.battery_gpio = battery_gpio
# if using a capacitive TFT, only PWM is supported for backlight control. If another method is
# set, default to BacklightMethod.PwmBinary which is similar to the other ones.
if (tft_type == DISP28C or tft_type == DISP28CP) and \
(backlight_method != BacklightMethod.Pwm and backlight_method != BacklightMethod.PwmBinary and
backlight_method != BacklightMethod.NoBacklight and backlight_method is not None):
backlight_method = BacklightMethod.PwmBinary
Backlight.initialize(backlight_method, backlight_steps, backlight_restore_last, backlight_state_sleep,
backlight_default, use_old_pwm, button_callback)
# Set GPIO if not passed in based on device
if self.buttons is None and backlight_auto:
if tft_type == DISP22NT:
self.buttons = [17, 22, 23, 27]
elif tft_type == DISP24R:
self.buttons = [16, 13, 12, 6, 5]
elif tft_type == DISP28R:
self.buttons = [23, 22, 21, 18]
elif tft_type == DISP28C:
self.buttons = [23, 22, 21, 17]
elif tft_type == DISP28RP:
self.buttons = [17, 22, 23, 27]
elif tft_type == DISP28CP:
self.buttons = [17, 22, 23, 27]
elif tft_type == DISP32RP:
self.buttons = [22, 23, 17, 27]
else: # No buttons PiTft buttons for 3.5" Displays (yet)
self.buttons = []
else:
self.buttons = []
# Get GPIO used as shutdown button in Adafruit kernels, if set in the config, we can
# get it and set it automatically. This only works for one or two digit GPIOs and
# setting the GPIO manually will negate any checking.
if (self.power_gpio == DEFAULT_PI_BATTERY_GPIO) and (os.path.isfile(ADAFRUIT_CONF_FILE)):
file_config = open(ADAFRUIT_CONF_FILE, "r").read()
start_pos = file_config.find(ADAFRUIT_CONF_GPIO)
if start_pos != -1:
value_pos = start_pos + len(ADAFRUIT_CONF_GPIO)
try:
value = int(file_config[value_pos:value_pos + 2].strip())
except ValueError:
value = 0
if value > 0:
self.power_gpio = value
# Remove any duplicates in the GPIO list
if self.buttons:
self.buttons = remove_duplicates(self.buttons)
# Remove the power_gpio button
if self.power_gpio in self.buttons:
self.buttons.remove(self.power_gpio)
# Remove any bad GPIOs (those 0 or below).
for button in self.buttons:
if button <= 0:
self.buttons.remove(button)
if backlight_auto and Backlight.method != BacklightMethod.NoBacklight and not self.actions:
# Set default actions based on the number of buttons if we are using a backlight mode and actions are not
# passed in specifically.
if len(self.buttons) >= 5:
self.actions = [GpioAction(GpioButtonAction.BacklightDown),
GpioAction(GpioButtonAction.BacklightUp),
GpioAction(GpioButtonAction.ScreenWake),
GpioAction(GpioButtonAction.ScreenSleep),
GpioAction(GpioButtonAction.ScreenToggle)]
elif len(self.buttons) == 4:
self.actions = [GpioAction(GpioButtonAction.BacklightDown),
GpioAction(GpioButtonAction.BacklightUp),
GpioAction(GpioButtonAction.ScreenWake),
GpioAction(GpioButtonAction.ScreenSleep)]
elif len(self.buttons) == 3:
self.actions = [GpioAction(GpioButtonAction.BacklightDown),
GpioAction(GpioButtonAction.ScreenWake),
GpioAction(GpioButtonAction.ScreenSleep)]
elif len(self.buttons) == 2:
self.actions = [GpioAction(GpioButtonAction.BacklightDown),
GpioAction(GpioButtonAction.ScreenToggle)]
elif len(self.buttons) == 1:
self.actions = [GpioAction(GpioButtonAction.ScreenToggle)]
else:
self.actions = []
# Setup GPIO for low battery indication which is useful if using a LiPo battery with a board that can
# control a GPIO pin.
GPIO.setmode(GPIO.BCM)
if self.battery_gpio is not DEFAULT_PI_BATTERY_GPIO:
GPIO.setup(self.battery_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if self.actions:
for button in self.buttons:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Bind actions to GPIO Buttons if they exist
action_count = 0
for button in self.buttons:
if action_count < len(self.actions):
button_actions = self.actions[action_count]
GPIO.add_event_detect(button, GPIO.RISING,
callback=partial(GpioButtons.gpio_button, action_count, button_actions),
bouncetime=Times.SwitchBounce)
action_count += 1
GpioButtons.gpio_initialized = True
##################################################################################
# BUTTONS DEL METHOD
##################################################################################
# Method to clean up upon deletion of the class.
##################################################################################
def __del__(self):
# Set the backlight back to its highest setting if any backlight manipulation
# could have been done
if Backlight.method != BacklightMethod.NoBacklight:
Backlight.set_backlight(GPIO_BACKLIGHT_HIGH)
# If any GPIO event detection was done, clean up.
if GpioButtons.gpio_initialized:
GPIO.cleanup()
GpioButtons.gpio_initialized = False
GpioButtons.initialized = False
##################################################################################
# BUTTONS IS_LOW_BATTERY METHOD
##################################################################################
# Method to check to see if the GPIO input used to monitor the battery is
# indicating a low battery.
##################################################################################
def is_low_battery(self):
# No need to check if we are set to default (no battery GPIO).
if self.battery_gpio is DEFAULT_PI_BATTERY_GPIO:
return False
else:
return GPIO.input(self.battery_gpio) == GPIO.LOW