-
Notifications
You must be signed in to change notification settings - Fork 1
/
laser_sensor_10929.py
83 lines (66 loc) · 2.45 KB
/
laser_sensor_10929.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
from micropython import const
from machine import Pin, SoftI2C
from lib.ssd1306 import SSD1306_I2C
from framebuf import FrameBuffer, MONO_HLSB
LASER_GPIO_PIN = const(23)
SDA_GPIO_PIN = const(21)
SCL_GPIO_PIN = const(22)
DISPLAY_WIDTH = const(128)
DISPLAY_HEIGHT = const(64)
IMAGE_STOP = 'stop.pbm'
IMAGE_GO = 'go.pbm'
class Display:
def __init__(self, sda_pin: int, scl_pin: int, width: int, height: int):
"""
display constructor
:param sda_pin: SDA GPIO Pin for display as integer
:param scl_pin: SCL GPIO Pin for display as integer
:param width: display width as integer
:param height: display height as integer
"""
i2c = SoftI2C(sda=Pin(int(sda_pin)), scl=Pin(int(scl_pin)))
self.__width = int(width)
self.__height = int(height)
self.display = SSD1306_I2C(self.__width, self.__height, i2c)
class Laser:
def __init__(self, display, pin: int):
"""
laser constructor
:param display: display object
:param pin: DOUT GPIO pin as integer
"""
self.__display = display
self.__img_go = Laser.load_image(IMAGE_GO, 50, 50)
self.__img_stop = Laser.load_image(IMAGE_STOP, 50, 50)
self._detection = Pin(int(pin), Pin.IN)
self._detection.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=self._handler)
def _handler(self, pin) -> None:
"""
irg multiplexer handler for display status
:param pin: pin object
:return: None
"""
self.__display.fill(0)
if pin.value() == 1:
self.__display.blit(self.__img_go, 39, 5)
else:
self.__display.blit(self.__img_stop, 39, 5)
self.__display.show()
@staticmethod
def load_image(filename: str, w: int, h: int):
"""
load pbm image into FrameBuffer object
:param filename: image path/filename as string
:param w: width as integer of image to load
:param h: height as integer of image to load
:return: image as FrameBuffer object
"""
with open(filename, 'rb') as f:
f.readline()
f.readline()
f.readline()
data = bytearray(f.read())
return FrameBuffer(data, w, h, MONO_HLSB)
if __name__ == '__main__':
oled = Display(sda_pin=SDA_GPIO_PIN, scl_pin=SCL_GPIO_PIN, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT)
laser = Laser(display=oled.display, pin=LASER_GPIO_PIN)