最新消息:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

Micro Bit 少儿编程 1872浏览 0评论

项目活动1:micro:bit 呼吸灯(Fade效果)

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

MakeCode程序:

方法1:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

方法2:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

方法3:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

程序说明:

方法1通过索引值,计算出每次显示的亮度,并赋值给set brightness函数积木。

方法2和方法3没有显著差别,只是使用的积木块不一样。这种方法使用brightness变量,通过有规律地改变brightness值,使之递增、递减。


写【micro:bit Micropython】系列文章,最重要的就是讲解micropython!程序思想方法和需要解决的问题跟MakeCode非常不一样。

(1)如果是micro:bit内部存储的Image图片,需要进行图片解析(解析,意为提取想要的数据),才能获得Image对应的字符串;如果是DIY的Image图片,可以省去这步。

(2)micropython没有针对Image的set brightness的函数,这就需要自己设计算法,来解决这个问题。

micropython程序说明:

(1)解析Image图片有两种方法,因此分为方法1和方法2。

方法1中,使用了micro:bit Micropython API的repr(image)函数。

# get a compact string representation of the image
repr(image)

解析出来的字符串赋值给b,但只有b[7]~b[35]是我们需要的图片数据信息,因此提取b[7:36]。

方法2中,使用了micro:bit Micropython API的display.get_pixel(x, y)函数。

# gets the brightness of the pixel (x,y). Brightness can be from 0 (the pixel # is off) to 9 (the pixel is at maximum brightness).
display.get_pixel(x, y)

使用for循环,逐行、逐列,遍历地将Image图片中的25个LED像素点的值取出,从而解析出Image图片的信息。
(2)产生呼吸灯效果的算法,方法1和方法2都相同。

产生呼吸灯(Fade效果)的算法:

Image图片中,亮的LED值都为9,不亮的都为0,因此通过Python语言字符串处理中常用的replace()函数,搜索字符串中的9,替换成8,延时100毫秒,再搜索字符串中的8,替换成7,延时100毫秒……直到替换成1,完成递减过程。然后逆向递增,搜索字符串中的1,替换成2,延时100毫秒,再搜索字符串中的2,替换成3,延时100毫秒……直到恢复成9,达成一次完整的“呼吸”效果(这样的效果称为Fade)。
micropython程序代码如下:

方法1:

from microbit import * b=repr(Image.HEART) print(b) a=b[7:36] print(a) t=100 while True:   for i in range(0,8):     a=a.replace(str(9-i),str(9-i-1))     print(a)     display.show(Image(a))     sleep(t)   for i in range(1,9):     a=a.replace(str(i),str(i+1))     print(a)     display.show(Image(a))     sleep(t) 

方法2:

from microbit import * display.show(Image.HEART) a="" for i in range(0,25):   a=a+str(display.get_pixel(i%5,i//5))   if i%5==4 and i<24:     a=a+":" print(a) t=100 while True:   for i in range(0,8):     a=a.replace(str(9-i),str(9-i-1))     print(a)     display.show(Image(a))     sleep(t)   for i in range(1,9):     a=a.replace(str(i),str(i+1))     print(a)     display.show(Image(a))     sleep(t) 

(进阶)项目活动2:自动切换下一张图片

说明:每张图片“呼吸”(Fade)两次后,自动切换下一张图片。程序中的每张图片都对应于一个数字(给图片编号,也像Python语言中常说的“键(Key)”)。因此,使图片循环播放的思想方法,跟之前我们讲过的“如何控制数字的增减、循环”相一致。目前程序中包含三张图片,只要掌握了这种编程的思想方法,图片的数量可以继续增加。

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

MakeCode程序:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

micropython程序:

from microbit import * img=0 t=100 while True: #根据变量img的值,解析图片,并把img赋值引向下一张图片   if img==0:     b=repr(Image.HEART)     img=1   elif img==1:     b=repr(Image.HAPPY)     img=2   elif img==2:     b=repr(Image.GIRAFFE)     img=0 #提取图片信息字符串   a=b[7:36] #呼吸渐变(Fade效果)   for j in range(0,2):     for i in range(0,8):       a=a.replace(str(9-i),str(9-i-1))       print(a)       display.show(Image(a))       sleep(t)     for i in range(1,9):       a=a.replace(str(i),str(i+1))       print(a)       display.show(Image(a))       sleep(t) 

(进阶)项目活动3:按A按钮,手动切换下一张图片

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

MakeCode程序:

【micro:bit Micropython】The LED Display(3)解析Image图片、调节LED亮度

micropython程序:

from microbit import * img = 0 t = 100 while True: #判断按钮A是否被按下     if button_a.get_presses():         img += 1         if img > 2:             img = 0 #根据变量img的值,选择图片进行解析、提取字符串     if img == 0:         b = repr(Image.HEART)     elif img == 1:         b = repr(Image.HAPPY)     elif img == 2:         b = repr(Image.GIRAFFE)     a = b[7:36] #呼吸渐变(Fade效果)     for i in range(0, 8):         a = a.replace(str(9 - i), str(9 - i - 1))         display.show(Image(a))         sleep(t)     for i in range(1, 9):         a = a.replace(str(i), str(i + 1))         display.show(Image(a))         sleep(t) 

您必须 登录 才能发表评论!