|
楼主 |
发表于 2020-4-19 10:52
|
显示全部楼层
10、动态正弦与圆的关系
通过掌控板载的oled,显示正弦与圆的关系。
[mw_shl_code=arduino,true]#MicroPython动手做(14)——掌控板之OLED屏幕
#动态正弦与圆的关系
from mpython import *
from machine import Timer
import math
def upRange(start, stop, step):
while start <= stop:
yield start
start += abs(step)
def downRange(start, stop, step):
while start >= stop:
yield start
start -= abs(step)
def timer1_tick(_):
global Dy, Dx, i, j, my_list, _item
oled.fill_rect(48, 0, 80, 64, 0)
oled.hline(48, 32, 80, 1)
oled.vline(48, 8, 50, 1)
j_end = int(len(my_list) - 1)
for j in (0 <= j_end) and upRange(0, j_end, 1) or downRange(0, j_end, 1):
oled.pixel(((len(my_list) + 49) - j), my_list[j], 1)
tim1 = Timer(1)
def add_to_list(_item):
global Dy, Dx, i, j, my_list
my_list.append(_item)
if len(my_list) > 72:
my_list.pop(0)
my_list = []
oled.fill(0)
tim1.init(period=100, mode=Timer.PERIODIC, callback=timer1_tick)
while True:
for i in range(0, 361, 5):
oled.fill_rect(0, 0, 47, 64, 0)
oled.circle(20, 32, 20, 1)
Dx = int((20 + math.cos(math.radians(i)) * 20))
Dy = int((32 - math.sin(math.radians(i)) * 20))
oled.line(20, 32, Dx, Dy, 1)
oled.line(Dx, Dy, 46, Dy, 1)
oled.show()
add_to_list(Dy)
[/mw_shl_code]
|
|