micropython :esp8266 OLED體驗


之前買的esp8266一直沒用,最近移植micropython試試。

按照網上教程,下載BIN固件,然后刷進去。串口使得是TQ210開發板里的一個串口軟件:SecureCRT,很好用。

然后按照網上教程連了無線網,才能上傳文件,發現很不好用,非常不好用。就在網上找了幾個別人寫的軟件,很好用,叫MicroPython File Uploader,關上串口軟件,用這個連上,就可以直接快速上傳文件了,很快,而且不需要用os.remove()先刪除一便,也不用官網提供的那個需要連接局域網的那個方法,每次進去都要 import 一大串來打開網絡再上傳文件了。

micropython 最大的困惑是模擬C語言的按位操作,比如python的 & 操作,和C語言很不一樣,由於python水平一般般,將一個16進制的數轉換為引腳的高低點平真是愁死我了。但是應該是我腦子不好使的原因,看到了別人的例子才恍然大悟,可以這么用啊。例子:

def ByteOpera(num,dat):
    byte= [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]
    if dat&byte[num]:
        return 1
    else:
        return 0

  num表示要看第幾位,dat是要傳入的十六進制數。

  進制數的按位運算很重要,比如引腳的操作,比如字模的處理等。現在按位操作沒問題了,那micropython用起來方便多了,比如操作OLED就方便了。

  手頭有個I2C的OLED,正好試試。

  說是有官方的SSD1306的支持,import 一下,真有,試了試,真能用。網上有教程,就不多說了。下面是我自己寫的一點GUI,基於官網的SSD1306的函數。

  1 from machine import I2C,Pin
  2 from ssd1306 import SSD1306_I2C
  3 import math
  4 
  5 chine=[
  6 #/*--  文字:  執  --*/
  7 #/*--  新宋體12;  此字體下對應的點陣為:寬x高=16x16   --*/
  8 0x10,0x10,0x10,0xFF,0x10,0x90,0x00,0x10,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,0x00,
  9 0x04,0x44,0x82,0x7F,0x01,0x80,0x40,0x21,0x1A,0x07,0x18,0x00,0x3F,0x40,0xF0,0x00,
 10 
 11 #/*--  文字:  念  --*/
 12 #/*--  新宋體12;  此字體下對應的點陣為:寬x高=16x16   --*/
 13 0x40,0x40,0x20,0x20,0x90,0x88,0x94,0xE3,0x84,0x88,0x90,0x20,0x20,0x40,0x40,0x00,
 14 0x40,0x30,0x00,0x00,0x38,0x40,0x40,0x44,0x5A,0x41,0x40,0x70,0x00,0x08,0x30,0x00,
 15 
 16 #/*--  文字:  執  --*/
 17 #/*--  新宋體12;  此字體下對應的點陣為:寬x高=16x16   --*/
 18 0x10,0x10,0x10,0xFF,0x10,0x90,0x00,0x10,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,0x00,
 19 0x04,0x44,0x82,0x7F,0x01,0x80,0x40,0x21,0x1A,0x07,0x18,0x00,0x3F,0x40,0xF0,0x00,
 20 
 21 #/*--  文字:  戰  --*/
 22 #/*--  新宋體12;  此字體下對應的點陣為:寬x高=16x16   --*/
 23 0x00,0x00,0x00,0xFF,0x08,0x08,0x08,0x40,0x40,0x40,0xFF,0x20,0x22,0xAC,0x20,0x00,
 24 0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x80,0x40,0x20,0x17,0x18,0x26,0x41,0xF0,0x00,
 25 
 26 
 27     ]
 28 i2c=I2C(-1, sda=Pin(4), scl=Pin(5), freq=400000)  
 29 oled = SSD1306_I2C(128, 64, i2c)
 30 def ByteOpera(num,dat):
 31     byte= [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]
 32     if dat&byte[num]:
 33         return 1
 34     else:
 35         return 0
 36 def hline(x0,y0,le,color):
 37     for i in range(le):
 38         oled.pixel(x0+i,y0,color)
 39             
 40 def shuline(x0,y0,le,color):
 41     for i in range(le):
 42         oled.pixel(x0,y0+i,color)
 43         
 44         
 45         
 46         
 47         
 48         
 49 class GUI:
 50     #畫點
 51     def DrawDot(x,y):
 52         oled.pixel(x,y)
 53     #橫線
 54     def hline(x0,y0,le,color):
 55         for i in range(le):
 56             oled.pixel(x0+i,y0,color)
 57      #豎線       
 58     def shuline(x0,y0,le,color):
 59         for i in range(le):
 60             oled.pixel(x0,y0+i,color)
 61             
 62      ##########################
 63      #函數:Line
 64      #功能:任意畫線
 65      #描述:
 66      #      x0,y0:起始位置
 67      #      x1,y1:終止位置
 68      #      color:顏色
 69      #
 70     def Line(x0,y0,x1,y1,color):
 71         dx=x1-x0
 72         if(dx>0):
 73             s1=1
 74         else:
 75             s1=-1
 76         dy=y1-y0
 77         if(dy>0):
 78             s2=1
 79         else:
 80             s2=-1
 81         dx=math.fabs(x1-x0)
 82         dy=math.fabs(y1-y0)
 83         if(dy>dx):
 84             temp=dx
 85             dx=dy
 86             dy=temp
 87             status=1
 88         else:
 89             status=0
 90             
 91         
 92         if(dx==0):
 93             hline(x0,y0,y1-y0,color)
 94         if(dy==0):
 95             shuline(x0,y0,x1-x0,color)
 96         
 97         sub=2*dy-dx
 98         for i in range(dx):
 99             oled.pixel(x0,y0,color)
100             if(sub>0):
101                 if(status==1):
102                     x0+=s1
103                 else:
104                     y0+=s2
105                 sub-=2*dx
106             if(status==1):
107                 y0+=s2
108             else:
109                 x0+=s1
110             sub+=2*dy
111           #oled.show()  
112           
113           #######################
114           #畫矩形函數
115           #fill為是否填充,默認不填充
116     def DrawBox(x0,y0,x1,y1,color=1,fill=0):
117         if (fill==1):
118             for i in range(y1-y0):
119                 hline(x0,y0+i,x1-x0,color)
120         else:
121             hline(x0,y0,x1-x0,color)
122             hline(x0,y1,x1-x0,color)
123             shuline(x0,y0,y1-y0,color)
124             shuline(x1,y0,y1-y0,color)
125             
126         ##############################
127         #畫圓函數,很占CPU
128         #fill 為是否填充
129         #畫兩遍,是因為只畫一遍的話中間有點畫不上
130     def DrawCircle_math(x,y,r,color,fill=0):
131         if(fill==0):
132             for i in range(x-r,x+r+1):
133                 oled.pixel(i,int(y-math.sqrt(r*r-(x-i)*(x-i))),color)
134                 oled.pixel(i,int(y+math.sqrt(r*r-(x-i)*(x-i))),color)
135             for i in range(y-r,y+r+1):
136                 oled.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color)
137                 oled.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color)
138         else:
139             for i in range(x-r,x+r+1):
140                 a=int(math.sqrt(r*r-(x-i)*(x-i)))
141                 shuline(i,y-a,a*2,color)
142 
143             for i in range(y-r,y+r+1):
144                 a=int(math.sqrt(r*r-(y-i)*(y-i)))
145                 hline(x-a,i,a*2,color)             
146                 
147                 
148     #########################
149     #畫圓函數
150     #網上的算法,不怎么圓
151     def DrawCircle(x,y,r,color):
152         a=0
153         b=r
154         di=3-2*r
155         while a<b:
156             oled.pixel(x-b,y-a,color)
157             oled.pixel(x+b,y-a,color)
158             oled.pixel(x-a,y+b,color)
159             oled.pixel(x-b,y-a,color)
160             oled.pixel(x-a,y-b,color)
161             oled.pixel(x+b,y+a,color)
162             oled.pixel(x+a,y-b,color)
163             oled.pixel(x+a,y+b,color)
164             oled.pixel(x-b,y+a,color)
165             a=a+1
166             if(di<0):
167                 di+=4*a+6
168             else:
169                 di+=10+4*(a-b)
170                 b=b-1
171             oled.pixel(x+a,y+b,color)  
172         #oled.show()
173     ############################
174     #16x16中文字符函數
175     def ShowChar16x16(x,y,n):
176         for i in range(2):
177             for a in range(16):
178                 for b in range(8):
179                     if(ByteOpera(b,chine[n*32+i*16+a])):
180                         oled.pixel(x+a,y+i*8+b,1)
181                     else:
182                         oled.pixel(x+a,y+i*8+b,0)
183         #oled.show()
184     ######################
185     #任意大小圖片顯示
186     def ShowPic(x,y,w,h,color,pic):
187         a=h/8
188         if(h%8>0):
189             a+=1
190         for i in range(a):
191             for n in range(w):
192                 for z in range(8):
193                     if(ByteOpera(z,pic[a*w+n])):
194                         oled.pixel(w,y+a*8+z,1)
195                     else:
196                         oled.pixel(w,y+a*8+z,0)
197                     
198     
199         
200         
201         
202     #奇數時反相顯示,偶數時正常顯示
203     def invert(n):
204         oled.invert(n)
205     #調整亮度。0最暗,255最亮
206     def contrast(n):
207         oled.contrast(n)
208     #在(x, y)處顯示字符串,注意text()函數內置的字體是8x8的,暫時不能替換
209     def text(char,x,y):
210         oled.text(char,x,y)
211      #n=0,清空屏幕,n大於0,填充屏幕   
212     def fill(n):
213         oled.fill(n)
214           #######################
215       #顯示函數
216     def show():
217         oled.show()
218         #關屏函數
219     def poweroff():
220         oled.poweroff()
221     def poweron():
222         oled.poweron()

使用如下:

import micropythonGUI

from micropythonGUI import GUI

GUI.DrawBox(0,0,20,20,1,0)

GUI.text('Draw A Rectangle'0,25)

GUI.show()

 

一直在想,為什么沒有人做一個帶小屏的運行micropython的esp8266或STM32F4的板子,能直接在小屏上顯示命令行,支持普通鍵盤直接輸入的小板子,能直接插上電就運行micropython,多好。我得理解理解micropython的運行機制,自己看看能不能改改底層驅動,在屏幕上顯示需要電腦才能顯示的命令行和鍵盤輸入,甚至能用命令行實現屏幕疊加操控和直接文件編寫等功能,直接在小板子上寫代碼,多好玩!!!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM