-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.py
More file actions
141 lines (87 loc) · 3.5 KB
/
control.py
File metadata and controls
141 lines (87 loc) · 3.5 KB
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
from ili9341 import Display, color565
from xpt2046 import Touch
from machine import idle, Pin, SPI # type: ignore
from micropython import const
from nrf24l01 import NRF24L01
AQUA = const(0X07FF) # (0, 255, 255)
CYAN = color565(0, 255, 255)
PURPLE = color565(255, 0, 255)
WHITE = color565(255, 255, 255)
GREEN = color565(127, 255, 0)
RED = color565(204, 0, 0)
BLACK = color565(0, 0, 0)
class Control(object):
"""Touchscreen simple demo."""
y = 0
state = ""
CYAN = color565(0, 255, 255)
PURPLE = color565(255, 0, 255)
WHITE = color565(255, 255, 255)
GREEN = color565(127, 255, 0)
RED = color565(204, 0, 0)
BLACK = color565(0, 0, 0)
def __init__(self, display, spi2):
"""Initialize box.
Args:
display (ILI9341): display object
spi2 (SPI): SPI bus
"""
self.display = display
self.touch = Touch(spi2, cs=Pin(1), int_pin=Pin(5),
int_handler=self.touchscreen_press)
# Display initial message
self.display.draw_text8x8(self.display.width // 2 - 32,
self.display.height - 9,
"Lets Ride!",
self.BLACK,
background=self.WHITE)
# A small 5x5 sprite for the dot
self.dot = bytearray(b'\x00\x00\x07\xE0\xF8\x00\x07\xE0\x00\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\xF8\x00\xF8\x00\x07\xE0\xF8\x00\xF8\x00\xF8\x00\x07\xE0\x00\x00\x07\xE0\xF8\x00\x07\xE0\x00\x00')
def stop_button_press(self, x, y):
# Draw dot
if (x >= 55 and x <= 185) and (y >= 180 and y <= 260):
self.display.draw_circle(x-2 , y-10 , 5, WHITE)
return "Stop_Button"
def go_button_press(self, x, y):
# Draw dot
if (x >= 55 and x <= 185) and (y >= 60 and y <= 140):
self.display.draw_circle(x-2 , y-10 , 5, WHITE)
return "Go_Button"
def touchscreen_press(self, x, y):
"""Process touchscreen press events."""
x = (self.display.width - 1) - x
self.y = y
# Display coordinates
# self.display.draw_text8x8(self.display.width // 2 - 32,
# self.display.height - 9,
# "{0:03d}, {1:03d}".format(x, y),
# self.CYAN)
print(x, y)
# #commenting these out for now
# if self.stop_button_press(x, y) == "Stop_Button":
# self.state = "Stop_Button"
# if self.go_button_press(x, y) == "Go_Button":
# self.state = "Go_Button"
# print(self.state)
def return_state(self):
return self.state
def clear_state(self):
self.state = ""
def return_y(self):
return self.y
# def test():
# """Test code."""
# spi = SPI(0, baudrate=40000000)
# display = Display(spi, dc=Pin(20), cs=Pin(17), rst=Pin(21))
# spi2 = SPI(0, baudrate=1000000, sck=Pin(2), mosi=Pin(3), miso=Pin(0))
# display.fill_rectangle(55, 60, 130, 80, GREEN) #Go button
# display.fill_rectangle(55, 180, 130, 80, RED) #Stop button
# StopNGo(display, spi2)
# try:
# while True:
# idle()
# except KeyboardInterrupt:
# print("\nCtrl-C pressed. Cleaning up and exiting...")
# finally:
# display.cleanup()
# test()