11import sys
2+
23sys .path .append ("../.." )
34
45from qiling .core import Qiling
56from qiling .const import QL_VERBOSE
7+ from qiling .hw .external_device .lcd .lcd1602 import PyGameLCD1602
68
7- import pygame
8- import threading
9-
10-
11- class LCD :
12- @classmethod
13- def get_pattern (cls , ch ):
14- lo = (ch >> 0 ) & 0xf
15- up = (ch >> 4 ) & 0xf
16- with open ('LCD1602A.txt' ) as f :
17- lines = f .read ().splitlines ()[lo * 9 + 1 : lo * 9 + 9 ]
18- pattern = [line [up * 6 + 1 : up * 6 + 6 ] for line in lines ]
19-
20- return pattern
21-
22- @classmethod
23- def make_screen (cls , infos ):
24- sc = []
25- for info in infos :
26- ps = [LCD .get_pattern (x ) for x in info ]
27- ln = ['' .join (p [r ] for p in ps ) for r in range (8 )]
28- sc += ln
29-
30- return sc
31-
32- class LCD1602 (LCD ):
33- def __init__ (self ) -> None :
34- super ().__init__ ()
35-
36- self .rows , self .cols = 2 , 16
37- self .cheight , self .cwidth = 8 , 5
38-
39- self .cur_row , self .cur_col = 0 , 0
40-
41- self .buf = []
42- self .data = [[ord (' ' ) for _ in range (self .cols )] for _ in range (self .rows )]
43- self .pixels = LCD .make_screen (self .data )
44-
45- self .address = 0x3f << 1
46-
47- def is_activate (self , i , j ):
48- return self .pixels [i ][j ] == '#'
49-
50- def render (self ):
51- size = 10
52- margin , interval = size * 4 , size // 2
53-
54- width = margin * 2 + size * (self .cols * self .cwidth ) + interval * (self .cols - 1 )
55- height = margin * 2 + size * (self .rows * self .cheight ) + interval * (self .rows - 1 )
56-
57- runable = True
58- clock = pygame .time .Clock ()
59- win = pygame .display .set_mode ((width , height ))
60-
61- pygame .display .set_caption ("LCD 1602A" )
629
63- while runable :
64- for event in pygame .event .get ():
65- if event .type == pygame .QUIT :
66- runable = False
67-
68- clock .tick (60 )
69- pygame .draw .rect (win , "#c9f6cd" , ((size , size ), (width - size * 2 , height - size * 2 )), border_radius = size )
70- for i in range (self .rows * self .cheight ):
71- for j in range (self .cols * self .cwidth ):
72- x = margin + size * j + interval * (j // self .cwidth )
73- y = margin + size * i + interval * (i // self .cheight )
10+ def create (path , lcd ):
11+ ql = Qiling ([path ], archtype = "cortex_m" , profile = "stm32f411" , verbose = QL_VERBOSE .DEBUG )
7412
75- color = "#446644" if self .is_activate (i , j ) else "#bbeebb"
76- pygame .draw .rect (win , color , ((x , y ), (size - 1 , size - 1 )))
77-
78- pygame .display .update ()
79-
80- print ('LCD quit' )
81- pygame .quit ()
82-
83- def send (self , buf ):
84- for data in buf :
85- self .buf .append (data )
86-
87- if len (self .buf ) == 4 :
88- up = self .buf [0 ] & 0xf0
89- lo = self .buf [3 ] & 0xf0
90- cmd = up | (lo >> 4 )
91-
92- if self .buf [0 ] & 0x1 :
93- if self .cur_col < 16 and self .cur_row < 2 :
94- self .data [self .cur_row ][self .cur_col ] = cmd
95- self .cur_col += 1
96- self .pixels = LCD .make_screen (self .data )
97-
98- elif cmd == 0x1 :
99- self .data = [[ord (' ' ) for _ in range (self .cols )] for _ in range (self .rows )]
100- self .pixels = LCD .make_screen (self .data )
101-
102- elif up == 0x80 :
103- self .cur_row , self .cur_col = 0 , lo >> 4
104-
105- elif up == 0xc0 :
106- self .cur_row , self .cur_col = 1 , lo >> 4
107-
108- self .buf = []
109-
110- def run (self ):
111- threading .Thread (target = self .render ).start ()
112-
113- def make (path , lcd ):
114- ql = Qiling ([path ],
115- archtype = "cortex_m" , profile = "stm32f411" , verbose = QL_VERBOSE .DEFAULT )
116-
117- ql .hw .create ('i2c1' ).connect (lcd )
13+ ql .hw .create ('i2c1' )
11814 ql .hw .create ('rcc' )
11915 ql .hw .create ('gpioa' )
120- ql .hw .create ('gpiob' )
16+ ql .hw .create ('gpiob' )
17+
18+ ql .hw .i2c1 .watch ()
19+ ql .hw .i2c1 .connect (lcd )
12120
21+ ql .hw .systick .set_ratio (100 )
22+
12223 return ql
12324
12425if __name__ == "__main__" :
125- lcd = LCD1602 ()
126- lcd .run ()
26+ lcd = PyGameLCD1602 ()
12727
128- make ("../rootfs/mcu/stm32f411/i2c-lcd.hex" , lcd ).run (count = 700000 )
129- make ("../rootfs/mcu/stm32f411/lcd-plus.hex" , lcd ).run (count = 2000000 )
28+ create ("../rootfs/mcu/stm32f411/i2c-lcd.hex" , lcd ).run (count = 50000 )
29+ create ("../rootfs/mcu/stm32f411/lcd-plus.hex" , lcd ).run (count = 100000 )
30+
31+ lcd .quit ()
0 commit comments