本帖最后由 gada888 于 2019-4-30 14:31 编辑

尽管python的编程软件很多,anaconda,pycharm or python ide.个人认为最好用的是Thonny。虽然做AI项目时候可能需要用pyCharm这类的去编程。但低级别单片机是上的py编程一定选Thonny了。这次就是用Thonny环境来给ESP32单片机和RGB灯带硬件编程。

main.jpg (95.29 KB, 下载次数: 3)

2019-4-30 13:40 上传

这是Thonny主界面了。

org.jpg (11.46 KB, 下载次数: 5)

2019-4-30 13:40 上传

Thonny下载地址

COM.jpg (53.33 KB, 下载次数: 4)

2019-4-30 13:40 上传

选定端口

mp device.jpg (50.74 KB, 下载次数: 4)

2019-4-30 13:40 上传

选MicroPython主控

LED code.jpg (12.56 KB, 下载次数: 4)

2019-4-30 13:40 上传

先输入一段程序,测试下ESP32上的LED的显示

import machine, neopixel

import time

n = 14

# strip control gpio

p = 14

np = neopixel.NeoPixel(machine.Pin(p), n)

# set single pixel (1st pixel = index [0]) to red color

np[0] = (255, 0, 0)

np.write()

time.sleep(1)

# set strip color

def set_color(r, g, b):

for i in range(n):

np= (r, g, b)

np.write()

set_color(0, 0, 255)

time.sleep(1)

# fade in/out

def fade_in_out(color, wait):

for i in range(0, 4 * 256, 8):

for j in range(n):

if (i // 256) % 2 == 0:

val = i & 0xff

else:

val = 255 - (i & 0xff)

if color == 'red':

np[j] = (val, 0, 0)

elif color == 'green':

np[j] = (0, val, 0)

elif color == 'blue':

np[j] = (0, 0, val)

elif color == 'purple':

np[j] = (val, 0, val)

elif color == 'yellow':

np[j] = (val, val, 0)

elif color == 'teal':

np[j] = (0, val, val)

elif color == 'white':

np[j] = (val, val, val)

np.write()

time.sleep_ms(wait)

#fade_in_out('red', 0)

fade_in_out('green', 10)

#fade_in_out('blue', 25)

#fade_in_out('purple', 10)

fade_in_out('yellow', 10)

fade_in_out('teal', 10)

#fade_in_out('white', 10)

time.sleep(1)

# bounce

def bounce(r, g, b, wait):

for i in range(4 * n):

for j in range(n):

np[j] = (r, g, b)

if (i // n) % 2 == 0:

np[i % n] = (0, 0, 0)

else:

np[n - 1 - (i % n)] = (0, 0, 0)

np.write()

time.sleep_ms(wait)

bounce(255, 0, 125, 50)

time.sleep(1)

# cycle

def cycle(r, g, b, wait):

for i in range(4 * n):

for j in range(n):

np[j] = (0, 0, 0)

np[i % n] = (r, g, b)

np.write()

time.sleep_ms(wait)

cycle(0, 255, 0, 50)

time.sleep(1)

# function to go through all colors

def wheel(pos):

# Input a value 0 to 255 to get a color value.

# The colours are a transition r - g - b - back to r.

if pos < 0 or pos > 255:

return (0, 0, 0)

if pos < 85:

return (255 - pos * 3, pos * 3, 0)

if pos < 170:

pos -= 85

return (0, 255 - pos * 3, pos * 3)

pos -= 170

return (pos * 3, 0, 255 - pos * 3)

# rainbow

def rainbow_cycle(wait):

for j in range(255):

for i in range(n):

rc_index = (i * 256 // n) + j

np= wheel(rc_index & 255)

np.write()

time.sleep_ms(wait)

rainbow_cycle(10)

rainbow_cycle(5)

time.sleep(1)

# turn off all pixels

def clear():

for i in range(n):

np= (0, 0, 0)

np.write()

clear()

测试ws2812b彩灯带。

cat.jpg (77.07 KB, 下载次数: 4)

2019-4-30 13:40 上传

用cat /main.py命令测试主程序

lsdevice.jpg (76.49 KB, 下载次数: 5)

2019-4-30 13:40 上传

用%lsdevice命令测试ESP32里有什么程序,

2019-04-30_135513.jpg (18.04 KB, 下载次数: 3)

2019-4-30 13:56 上传

点击三角运行按钮,可以运行程序,但并没有烧录到ESP32里。

upload.jpg (49.22 KB, 下载次数: 4)

2019-4-30 13:40 上传

要在device里运行点击激活如上功能。上传程序。

最后因为Thonny并没有能从ESP32里删除文件的功能。很多人用上传空文件的方法达到删除的目的。

2019-04-30_142902.jpg (85.06 KB, 下载次数: 6)

2019-4-30 14:30 上传

2019-04-30_143015.jpg (48.45 KB, 下载次数: 3)

2019-4-30 14:31 上传

Logo

一站式 AI 云服务平台

更多推荐