12
Return list

3386

Posts

0

Resources
21
 

10. AB key and rose curve
Rose curve, press A plus 1, press B to start

The rose curve basically means: a curve with an integer number of sinusoidal petals evenly distributed around a central point in a plane. The mathematical formula is: ρ=a*sin(nθ), where a is a fixed length and n is an integer (not necessarily the number of petals).

Since this graph is not composed of straight lines, all drawing functions on the control panel cannot be used. Only the pixel display function can be used. The command is as follows:
display.pixel(50,0,1)
In this command: display.pixel(x, y [,c ]) x, y are the point coordinates (x,y). If c is not given, the color value of the specified pixel is obtained. If c is given, the specified pixel is set to the given color. Since the control panel uses a black and white screen, 1 can be used to represent lighting.

#MicroPython动手做(15)——掌控板之AB按键
#玫瑰曲线,按A加1,按B开始,


from mpython import *
import math,time#引入库文件[/align][align=left]
def DrawRoseCurve(a,n):#定义函数,函数名称可以自己命名
    for t in range(0,360):#循环次数,由于是画一圈,所以是360;可以自行设定
        x = math.floor(math.cos(t)*a*math.sin(n*t))#计算x坐标的值,注意:这里需要取整
        y = math.floor(math.sin(t)*a*math.sin(n*t))#计算y坐标的值,并且取整
        display.pixel(x+64,y+32,1) #显示坐标像素点,为什么要+64、+32,哪是因为要把中心坐标(64,32)作为起点
        display.show() #执行[/align][align=left]
# 按键引脚初始化
  
display.fill(0)#清屏
display.show()#清屏执行
  
BTNA = Pin(0, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
BTNB = Pin(2, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
  
display.DispChar('玫瑰曲线', 38, 0)
display.DispChar('输入花瓣数量,按A加', 0, 17)
display.DispChar('按B开始', 0, 34)
display.show() #执行
  
n1 = 0
jishu = True
Start = False
while True:
    if jishu:
        if BTNA.value() == 0 and  BTNB.value() == 1 :
            display.fill(0)#清屏
            display.show()#清屏执行
            n1 = n1+1
            display.DispChar('数量:%d' % n1, 0, 0)
            display.show()#执行
            time.sleep_ms(400)
        if BTNA.value() == 1 and  BTNB.value() == 0 :
            jishu = False
            Start = True
        if Start:
            if n1%2 == 0:
                n1=n1/2
                display.fill(0)#清屏
                display.show()#清屏执行
                DrawRoseCurve(30,n1)#调用函数
Break

 
 

3386

Posts

0

Resources
22
 

 
 
 

3386

Posts

0

Resources
23
 

 
 
 

3386

Posts

0

Resources
24
 
11. A and B keys control the image conversion display

#MicroPython动手做(15)——掌控板之AB按键
#A、B键控制图片转换显示
 
from mpython import *
import music
 
def callback_button_a_pressed():
  oled.fill(0)
  bmp_wZZv = bytearray([0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x89,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x19,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x39,0xff,0xff,0xff,0xff,0xff,0xff,0xf8,0x0,0x3f,0xff,0xff,0xff,0xff,0xff,0xfe,0x79,0xff,0xff,0xff,0xff,0xff,0xff,0xe0,0x1a,0xf,0xff,0xff,0xff,0xff,0xff,0xfe,0xf9,0xff,0xff,0xff,0x3f,0xff,0xff,0x83,0xff,0xe0,0x0,0x0,0x0,0x7f,0xff,0xfc,0xf9,0xff,0xff,0xfc,0xf,0xff,0xfe,0x0,0x7f,0xf0,0x0,0x0,0x0,0x3f,0xff,0xfc,0xf9,0xff,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f,0x38,0xcf,0x9f,0xff,0xff,0x80,0x0,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0x7f,0x3,0x80,0x4f,0x9f,0xff,0xff,0x81,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0x3f,0x21,0x9f,0xf,0x9f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0x3f,0x0,0x7f,0x9f,0x9f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0xf,0xff,0xff,0x3f,0x10,0x3f,0xdf,0x9f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0xbf,0x39,0x0,0x1f,0x80,0x0,0x0,0x3f,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0xbf,0x3b,0x80,0x1f,0x80,0x0,0x0,0x3f,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0x3f,0x30,0x3f,0x9f,0x1f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0x1f,0xff,0xff,0x3f,0x0,0x7f,0x9f,0x1f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0x3f,0xff,0xff,0x3f,0x1,0x9f,0x1c,0x1f,0xff,0xff,0xbf,0xff,0xff,0xff,0xe0,0x0,0x3f,0xff,0xfe,0x7f,0x3,0x80,0x38,0x9f,0xff,0xff,0x9f,0xff,0xff,0xff,0xe0,0x0,0x7f,0xe3,0xfe,0x7f,0x1f,0x30,0xb1,0xdf,0xff,0xff,0x80,0x1f,0xff,0xff,0xf0,0x0,0x3c,0xc1,0xfc,0xff,0x3f,0x3f,0xb3,0xdf,0xff,0xff,0x80,0x0,0x1f,0xff,0xf0,0x0,0x3c,0xb0,0xfd,0xfe,0x6,0x7f,0x87,0xdf,0xff,0xff,0x80,0x0,0x0,0x1,0xf0,0x0,0x1c,0xb0,0xff,0xfe,0x40,0x73,0x87,0xdf,0xff,0xff,0x80,0x0,0x0,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xff,0xff,0xff,0xff,0xff,0xfe,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x79,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x39,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x99,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff])
  oled.Bitmap(0, 0, bmp_wZZv, 128, 64, 1)
  oled.show()
 
 
def callback_button_b_pressed():
  oled.fill(0)
  bmp_h61P = bytearray([0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x83,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x99,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9c,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9e,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xf8,0x0,0xf,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xe1,0xff,0x83,0xff,0xff,0xfd,0xff,0xff,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0x7,0xff,0xc0,0xff,0xff,0xf0,0x7f,0xff,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0x1f,0xfc,0x0,0x7f,0xff,0xe6,0x0,0x1f,0xff,0x9f,0x3f,0xff,0xf9,0xff,0xcf,0xfe,0x3f,0xf0,0xff,0x1f,0xff,0xc0,0x0,0x7,0xff,0x9f,0x7f,0xf0,0x1,0xff,0xcf,0xfc,0x7f,0xe7,0xff,0x9f,0xc0,0x0,0x0,0x3,0xff,0x9e,0x60,0x0,0x1,0xff,0xcf,0xfc,0xff,0x8f,0xff,0xce,0x3,0xff,0xf0,0x1,0xff,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf9,0xff,0x9f,0xff,0xe0,0x7f,0xff,0xf8,0x0,0xff,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf1,0xff,0x3f,0xff,0xe3,0xff,0xff,0xf8,0x0,0x7f,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf0,0x1e,0xe,0xf,0xcf,0xff,0xff,0xf8,0x0,0x3f,0x9f,0x0,0x0,0x1,0xff,0xff,0xf9,0x6,0x0,0x0,0x9f,0xff,0xfc,0xf8,0x0,0x3f,0x9f,0x0,0x0,0x1,0xff,0xff,0xf9,0xf0,0x33,0xf8,0x1f,0xff,0x86,0x78,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xf9,0xff,0xcf,0xfe,0x7f,0xe3,0xff,0x9f,0xff,0x80,0x0,0x7,0xff,0x9f,0x3f,0xff,0xfd,0xff,0xcf,0xff,0x3f,0xf0,0x7e,0x3f,0xff,0xe7,0x0,0x1f,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0xf,0xfe,0x0,0x7f,0xff,0xf0,0x3f,0xff,0xff,0x9f,0x3f,0xff,0xfe,0x0,0x0,0x0,0x7,0xff,0xc1,0xff,0xff,0xfc,0xff,0xff,0xff,0x9f,0x7f,0xff,0xff,0xff,0xff,0xff,0xf0,0x58,0x7,0xff,0xff,0xff,0xff,0xff,0xff,0x9e,0x7f,0xff,0xff,0xff,0xff,0xff,0xfc,0x0,0x1f,0xff,0xff,0xff,0xff,0xff,0xff,0x9c,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x98,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x91,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff])
  oled.Bitmap(0, 0, bmp_h61P, 128, 64, 1)
  oled.show()
 
 
def on_button_a_pressed():
  while True:
    if button_a.value() == 0:
      yield callback_button_a_pressed()
    else:
      yield
 
def on_button_b_pressed():
  while True:
    if button_b.value() == 0:
      yield callback_button_b_pressed()
    else:
      yield
 
func_button_a_pressed = on_button_a_pressed()
func_button_b_pressed = on_button_b_pressed()
while True:
  next(func_button_a_pressed)
  next(func_button_b_pressed)

 
 
 

3386

Posts

0

Resources
25
 

Mind+ Graphics Programming

 
 
 

3386

Posts

0

Resources
26
 

 
 
 

3386

Posts

0

Resources
27
 

11. AB button switches the sound and light value and the three-axis value

#MicroPython Hands-on (15) - AB button of the control panel
#AB button switches the sound and light value and the three-axis value

#MicroPython动手做(15)——掌控板之AB按键
#AB按键切换显示声光值与三轴数值

from mpython import *
import time

def on_button_a_down(_):
    global sensor, switch
    time.sleep_ms(10)
    if button_a.value() == 1: return
    switch = switch + -1
    if switch < 0:
        switch = 2

def on_button_b_down(_):
    global sensor, switch
    time.sleep_ms(10)
    if button_b.value() == 1: return
    switch = switch + 1
    if switch > 2:
        switch = 0

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)


switch = 0
oled.fill(0)
oled.DispChar("上一个", 0, 0, 1)
oled.DispChar("下一个", 91, 0, 1)
while True:
    oled.fill_rect(0, 16, 128, 48, 0)
    if switch == 1:
        oled.DispChar((str("声音值:") + str(sound.read())), 30, 28, 1)
    elif switch == 2:
        oled.DispChar((str(" 光线值:") + str(light.read())), 25, 28, 1)
    else:
        oled.DispChar((str("X 轴:") + str(accelerometer.get_x())), 15, 15, 1)
        oled.DispChar((str("Y 轴:") + str(accelerometer.get_y())), 15, 30, 1)
        oled.DispChar((str("Z 轴:") + str(accelerometer.get_z())), 15, 45, 1)
    oled.show()

 
 
 

3386

Posts

0

Resources
28
 

mPython X Graphical Programming

 
 
 

3386

Posts

0

Resources
29
 

 
 
 

3386

Posts

0

Resources
30
 

12. AB button stopwatch timer

The stopwatch is mainly used to count seconds. It usually has two buttons: A and B.

When you press the A button for the first time, the timer starts.

When you press the A button a second time, the timer is paused.

When you press the A key again, the timing that was paused in the previous step will continue.

Press the B button to return to zero.

#MicroPython Hands-on (15) - AB button of the control panel
#AB button stopwatch timer

#MicroPython动手做(15)——掌控板之AB按键
#AB按键秒表计时器from mpython import *

import framebuf

import font.digiface_44

import time

from machine import Timer

def timer1_tick(_):
    global aaa, ttt
    ttt = ttt + 1
    oled.fill(0)
    display_font(font.digiface_44, (''.join([str(x) for x in [ttt // 60, ':', ttt % 60]])), 2, 8, False, 2)
    oled.show()

tim1 = Timer(1)

def on_button_a_down(_):
    global aaa, ttt
    time.sleep_ms(10)
    if button_a.value() == 1: return
    if aaa == 1:
        aaa = 0
        tim1.init(period=1000, mode=Timer.PERIODIC, callback=timer1_tick)
    else:
        aaa = 1
        tim1.deinit()

def on_button_b_down(_):
    global aaa, ttt
    time.sleep_ms(10)
    if button_b.value() == 1: return
    ttt = 0
    oled.fill(0)
    display_font(font.digiface_44, '00:00', 2, 8, False, 2)
    oled.show()
    aaa = 1
    tim1.deinit()

def display_font(_font, _str, _x, _y, _wrap, _z=0):
    _start = _x
    for _c in _str:
        _d = _font.get_ch(_c)
        if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
        if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
        oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
        framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
        _x += _d[2]

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
oled.fill(0)
display_font(font.digiface_44, '00:00', 2, 8, False, 2)
oled.show()
ttt = 0
aaa = 1


 
 
 

3386

Posts

0

Resources
31
 

mPython Graphics Programming

 
 
 

3386

Posts

0

Resources
32
 

 
 
 

3386

Posts

0

Resources
33
 
This post was last edited by eagle8 on 2020-6-5 12:48

Table of Contents
MicroPython Hands-on (01) - I bought a K210 chip AI development board after the Spring Festival
https://en.eeworld.com/bbs/thread-1115786-1-1.html

MicroPython Hands-on (02) - Try to build the IDE environment of K210 development board
https://en.eeworld.com/bbs/thread-1115831-1-1.html

MicroPython Hands-on (03) - Learn to start and run MicroPython from scratch
https://en.eeworld.com/bbs/thread-1116184-1-1.html

MicroPython Hands-on (04) - Basic examples of learning MicroPython from scratch
https://en.eeworld.com/bbs/thread-1116438-1-1.html

MicroPython Hands-on (05) - Learn MicroPython from scratch - LCD screen
https://en.eeworld.com/bbs/thread-1116577-1-1.html

MicroPython Hands-on (06) - Learn MaixPy from scratch for monocular camera
https://en.eeworld.com/bbs/thread-1116591-1-1.html

MicroPython Hands-on (07) - Learn Machine Vision with MaixPy from Scratch
https://en.eeworld.com/bbs/thread-1116617-1-1.html

MicroPython Hands-on (08) - Learn MaixPy from scratch to recognize colors
https://en.eeworld.com/bbs/thread-1116662-1-1.html

MicroPython Hands-on (09) - Learn MaixPy face recognition from scratch
https://en.eeworld.com/bbs/thread-1116720-1-1.html

MicroPython Hands-on (10) - Learn MaixPy Neural Network KPU from scratch
https://en.eeworld.com/bbs/thread-1116925-1-1.html

MicroPython Hands-on (11) - Building the control board IDE environment
https://en.eeworld.com/bbs/thread-1117964-1-1.html

MicroPython Hands-on (12) - Hello World on the Control Board
https://en.eeworld.com/bbs/thread-1118180-1-1.html

MicroPython Hands-on (13) - RGB tricolor light of the control board
https://en.eeworld.com/bbs/thread-1118275-1-1.html

MicroPython Hands-on (14) - OLED screen of the control board
https://en.eeworld.com/bbs/thread-1118389-1-1.html

MicroPython Hands-on (15) - AB buttons on the control panel
https://en.eeworld.com/bbs/thread-1118496-1-1.html

MicroPython Hands-on (16) - Image display of the control board
https://en.eeworld.com/bbs/thread-1118945-1-1.html

MicroPython Hands-on (17) - Touch pin of control board
https://en.eeworld.com/bbs/thread-1119462-1-1.html

MicroPython Hands-on (18) - Sound and light sensor of the control board
https://en.eeworld.com/bbs/thread-1119583-1-1.html

MicroPython Hands-on (19) - Buzzer and music on the control board
https://en.eeworld.com/bbs/thread-1119668-1-1.html

MicroPython Hands-on (20) - Three-axis acceleration of the control board
https://en.eeworld.com/bbs/thread-1119998-1-1.html

MicroPython Hands-on (21) - Magnetic Field Sensor of Control Board
https://en.eeworld.com/bbs/thread-1120188-1-1.html

MicroPython Hands-on (22) - Wireless Broadcasting of Control Board
https://en.eeworld.com/bbs/thread-1120835-1-1.html

MicroPython Hands-on (23) - WiFi and Bluetooth of the control board
https://en.eeworld.com/bbs/thread-1120934-1-1.html

MicroPython Hands-on (24) - Extension of the control board and control treasure
https://en.eeworld.com/bbs/thread-1121208-1-1.html

MicroPython Hands-on (25) - Speech Synthesis and Speech Recognition
https://en.eeworld.com/bbs/thread-1123752-1-1.html

MicroPython Hands-on (26) - OneNET of the Internet of Things
https://en.eeworld.com/bbs/thread-1125172-1-1.html

MicroPython Hands-on (27) - WeChat applet for the Internet of Things
https://en.eeworld.com/bbs/thread-1125520-1-1.html

MicroPython Hands-on (28) - Yeelight of the Internet of Things
https://en.eeworld.com/bbs/thread-1125939-1-1.html

MicroPython Hands-on (29) - Internet of Things SIoT
https://en.eeworld.com/bbs/thread-1126090-1-1.html

 
 
 

12
Return list
Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list