1
1
import struct
2
2
3
- import serial
4
3
from serial .tools .list_ports import comports
5
4
6
5
from library .lcd_comm import *
@@ -28,15 +27,13 @@ def get_rev_b_orientation(orientation: Orientation) -> OrientationValueRevB:
28
27
29
28
class LcdCommRevB (LcdComm ):
30
29
def __init__ (self ):
31
- self .lcd_serial = None
32
- if CONFIG_DATA ['config' ]['COM_PORT' ] == 'AUTO' :
33
- lcd_com_port = self .auto_detect_com_port ()
34
- self .lcd_serial = serial .Serial (lcd_com_port , 115200 , timeout = 1 , rtscts = 1 )
35
- print (f"Auto detected comm port: { lcd_com_port } " )
36
- else :
37
- lcd_com_port = CONFIG_DATA ["config" ]["COM_PORT" ]
38
- print (f"Static comm port: { lcd_com_port } " )
39
- self .lcd_serial = serial .Serial (lcd_com_port , 115200 , timeout = 1 , rtscts = 1 )
30
+ self .openSerial ()
31
+
32
+ def __del__ (self ):
33
+ try :
34
+ self .lcd_serial .close ()
35
+ except :
36
+ pass
40
37
41
38
@staticmethod
42
39
def auto_detect_com_port ():
@@ -49,7 +46,7 @@ def auto_detect_com_port():
49
46
50
47
return auto_com_port
51
48
52
- def SendCommand (self , cmd : Command , payload = None ):
49
+ def SendCommand (self , cmd : Command , payload = None , bypass_queue : bool = False ):
53
50
# New protocol (10 byte packets, framed with the command, 8 data bytes inside)
54
51
if payload is None :
55
52
payload = [0 ] * 8
@@ -68,9 +65,12 @@ def SendCommand(self, cmd: Command, payload=None):
68
65
byteBuffer [8 ] = payload [7 ]
69
66
byteBuffer [9 ] = cmd
70
67
71
- # Lock queue mutex then queue the request
72
- with config .update_queue_mutex :
73
- config .update_queue .put ((self .WriteData , [byteBuffer ]))
68
+ if bypass_queue :
69
+ self .WriteData (byteBuffer )
70
+ else :
71
+ # Lock queue mutex then queue the request
72
+ with config .update_queue_mutex :
73
+ config .update_queue .put ((self .WriteData , [byteBuffer ]))
74
74
75
75
def WriteData (self , byteBuffer : bytearray ):
76
76
try :
@@ -90,28 +90,17 @@ def WriteLine(self, line: bytes):
90
90
print ("(Write line) Too fast! Slow down!" )
91
91
92
92
def Hello (self ):
93
- # This command reads LCD answer on serial link, so it bypasses the queue
94
- byteBuffer = bytearray (10 )
95
- byteBuffer [0 ] = Command .HELLO
96
- byteBuffer [1 ] = ord ('H' )
97
- byteBuffer [2 ] = ord ('E' )
98
- byteBuffer [3 ] = ord ('L' )
99
- byteBuffer [4 ] = ord ('L' )
100
- byteBuffer [5 ] = ord ('O' )
101
- byteBuffer [6 ] = 0
102
- byteBuffer [7 ] = 0
103
- byteBuffer [8 ] = 0
104
- byteBuffer [9 ] = Command .HELLO
93
+ hello = [ord ('H' ), ord ('E' ), ord ('L' ), ord ('L' ), ord ('O' )]
105
94
106
- with config . update_queue_mutex :
107
- self .WriteData ( byteBuffer )
108
- response = self .lcd_serial .read (10 )
95
+ # This command reads LCD answer on serial link, so it bypasses the queue
96
+ self .SendCommand ( Command . HELLO , payload = hello , bypass_queue = True )
97
+ response = self .lcd_serial .read (10 )
109
98
110
99
if len (response ) != 10 :
111
100
print ("Device not recognised (short response to HELLO)" )
112
101
if response [0 ] != Command .HELLO or response [- 1 ] != Command .HELLO :
113
102
print ("Device not recognised (bad framing)" )
114
- if [x for x in response [1 :6 ]] != byteBuffer [ 1 : 6 ] :
103
+ if [x for x in response [1 :6 ]] != hello :
115
104
print ("Device not recognised (No HELLO; got %r)" % (response [1 :6 ],))
116
105
# The HELLO response here is followed by:
117
106
# 0x0A, 0x12, 0x00
@@ -125,11 +114,13 @@ def InitializeComm(self):
125
114
self .Hello ()
126
115
127
116
def Reset (self ):
128
- # HW revision B does not implement a command to reset it
129
- pass
117
+ # HW revision B does not implement a command to reset it: clear the screen instead
118
+ self . Clear ()
130
119
131
120
def Clear (self ):
132
- pass
121
+ # HW revision B does not implement a Clear command: display a blank image on the whole screen
122
+ blank = Image .new ("RGB" , (get_width (), get_height ()), (255 , 255 , 255 ))
123
+ self .DisplayPILImage (blank )
133
124
134
125
def ScreenOff (self ):
135
126
# HW revision B does not implement a "ScreenOff" native command: using SetBrightness(0) instead
0 commit comments