forked from pikasTech/PikaPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
259 lines (231 loc) · 6.13 KB
/
example.py
File metadata and controls
259 lines (231 loc) · 6.13 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
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
import machine
import time
import PikaStdDevice as std
import bflb
import lvgl as lv
import network
import requests
def led():
"""
led 例程
led0、led1、led2 依次闪烁 10 次
led0 sipeed m0p 的板载 led
led1 是 verimake bl618 竞赛板的板载 led
"""
led0 = machine.Led(0)
led1 = machine.Led(1)
for i in range(10):
print('Blinking (%d/10)' % (i + 1))
led0.on()
led1.off()
time.sleep(0.1)
led0.off()
led1.on()
time.sleep(0.1)
led0.off()
led1.off()
led0.off()
led1.off()
led0.close()
led1.close()
def key():
"""
key 例程
等待 key0 被按下 3 次, 使用阻塞方式
key0 是 verimake bl618 竞赛板的板载按键
如果不是竞赛板,可以自行连接 P33 引脚
"""
key0 = machine.Key(0)
print('Waiting for key0 to be pressed')
count_expect = 3
count = 0
while True:
if key0.isPressed():
print('Key0 is pressed (%d/%d)' % (count + 1, count_expect))
count += 1
time.sleep(0.3)
if count == count_expect:
break
time.sleep(0.1)
key0.close()
count = 0
count_expect = 3
def _handle_key0(siganl):
"""
key 回调例程的回调函数
"""
global count
print('Key0 is pressed (%d/%d)' % (count + 1, count_expect))
count += 1
key_cb = None
def key_callback():
"""
key 回调例程
等待 key0 被按下 3 次, 使用回调方式
key0 是 verimake bl618 竞赛板的板载按键
如果不是竞赛板,可以自行连接 P33 引脚
"""
global count, key_cb
count = 0
key_cb = machine.Key(0)
print('Waiting for key0 to be pressed')
key_cb.setCallBack(_handle_key0, std.GPIO.SIGNAL_FALLING)
while True:
if count > count_expect:
break
time.sleep(0.1)
def adc():
"""
adc 例程
读取 adc0 的值 10 次
adc 接在 P0 引脚
"""
adc0 = machine.ADC()
adc0.setPin('P0')
adc0.enable()
for i in range(10):
print('ADC0: %f (%d/%d)' % (adc0.read(), i, 10))
time.sleep(0.5)
def dac():
"""
dac 例程
输出 0.5V 的方波 10 次
dac 接在 P3 引脚
adc 接在 P0 引脚, 实验时可以用杜邦线将 P3 和 P0 连接起来
"""
dac0 = machine.DAC()
dac0.setPin('P3')
dac0.enable()
adc0 = machine.ADC()
adc0.setPin('P0')
adc0.enable()
val_max = 1.5
val_min = 1
for i in range(10):
dac0.write(val_max)
print('DAC0: %r, ADC0: %r (%d/%d)' % (val_max, adc0.read(), i+1, 10))
time.sleep(0.5)
dac0.write(val_min)
print('DAC0: %r, ADC0: %r (%d/%d)' % (val_min, adc0.read(), i+1, 10))
time.sleep(0.5)
dac0.disable()
adc0.disable()
def uart():
"""
uart 例程
通过 uart1 进行自发自收实验,你可以将 P23 和 P24 用杜邦线连接起来
"""
uart1 = machine.UART()
uart1.setId(1)
uart1.setPinTX('P23')
uart1.setPinRX('P24')
uart1.setBaudRate(115200)
uart1.enable()
print('Waiting for data from uart1')
for i in range(10):
uart1.write('Hello!')
data = uart1.read(256)
time.sleep(0.1)
if data:
print('Received: %r' % data)
time.sleep(0.1)
def _uart_receive_callback(signal):
"""
UART 回调例程的回调函数
"""
global uart_rx_cnt
data = uart1.read(256)
print('Received:', data, '(%d/%d)' % (uart_rx_cnt + 1, 3))
uart_rx_cnt += 1
def uart_callback():
"""
uart 回调例程
通过 uart1 与电脑通信, 使用回调方式接收数据
电脑需要连接到 uart1 的 TX 和 RX 引脚,波特率为 115200
例程开始后,你需要在电脑上输出三行数据
TX引脚是 P23
RX引脚是 P24
"""
global uart1
global uart_rx_cnt
uart_rx_cnt = 0
uart1 = machine.UART()
uart1.setId(1)
uart1.setPinTX('P23')
uart1.setPinRX('P24')
uart1.setBaudRate(115200)
uart1.enable()
uart1.setCallBack(_uart_receive_callback, uart1.SIGNAL_RX)
print('Waiting for data from uart1 (0/3)')
while True:
if uart_rx_cnt == 3:
break
time.sleep(0.1)
uart1.disable()
cam0:bflb.Camera = None
canvas:lv.canvas = None
def _cam_callback(signal):
"""
Camera 回调例程的回调函数
"""
global cam0, canvas
pic_addr, pic_size = cam0.get_frame_info()
# print('Picture address: 0x%08x' % pic_addr)
# print('Picture size: %d' % pic_size)
cam0.pop_one_frame()
LV_IMG_CF_TRUE_COLOR = 4
canvas.set_buffer(pic_addr, 320, 240, LV_IMG_CF_TRUE_COLOR)
def cam():
global cam0, canvas
# lvgl
canvas = lv.canvas(lv.scr_act())
canvas.set_size(320, 240)
canvas.align(lv.ALIGN.TOP_MID, 0, 0)
# camera
cam0 = bflb.Camera()
cam0.set_callback(_cam_callback)
cam0.start()
return cam0
mic0:bflb.Microphone = None
chart:lv.chart = None
def _mic_callback(signal):
# print('Mic data received')
chart.refresh()
def mic():
global mic0, chart
# init mic
mic0 = bflb.Microphone()
mic0.set_callback(_mic_callback)
# init chart
chart = lv.chart(lv.scr_act())
chart.set_size(320, 240)
chart.set_range(0, 0, 2000)
LV_PART_INDICATOR = 0x020000
chart.set_style_size(0, LV_PART_INDICATOR)
ser = chart.add_series(lv.palette_main(lv.PALETTE.RED), 0)
addr, size = mic0.get_frame_info()
chart.set_point_count(size)
chart.set_ext_y_array(ser, addr)
print('Mic frame address: 0x%08x' % addr)
print('Mic frame size: %d' % size)
# start mic
mic0.start()
return mic0
def wifi_sta():
"""
wifi sta 例程
"""
wlan = network.WLAN(network.STA_IF)
wlan.active(1)
ssid = input('SSID: ')
password = input('Password: ')
wlan.connect(ssid, password)
for i in range(10):
print('Waiting for wifi connection...')
if wlan.isconnected():
break
time.sleep(1)
print('Wifi connected:', bool(wlan.isconnected()))
print('GET pikapython.com header:')
res = requests.request('GET', 'http://www.pikapython.com')
print(res.headers)