1. 程式人生 > >micropython驅動sh1106點亮oled

micropython驅動sh1106點亮oled

研究 ifconfig nat 模塊 通過 路由器 code iso 就是

繼上一帖給esp32刷入micropython之後,忍不住給以esp12e也刷了micropython

這裏先說一下webrepl:

通過wifi可以和esp8266交互,以及便攜的傳輸文件

首次使用:

1 import webrepl
2 webrepl.start()

中間可能讓你import 一個配置模塊來設置密碼具體即執行上兩句就知道了.

WebREPL - a prompt over WiFi
WebREPL allows you to use the Python prompt over WiFi, connecting through a browser. The latest versions of Firefox and Chrome are supported.

For your convenience, WebREPL client is hosted at http://micropython.org/webrepl . Alternatively, you can install it locally from the the GitHub repository https://github.com/micropython/webrepl .

Before connecting to WebREPL, you should set a password and enable it via a normal serial connection. Initial versions of MicroPython for ESP8266 came with WebREPL automatically enabled on the boot and with the ability to set a password via WiFi on the first connection, but as WebREPL was becoming more widely known and popular, the initial setup has switched to a wired connection for improved security:

import webrepl_setup
Follow the on-screen instructions and prompts. To make any changes active, you will need to reboot your device.

To use WebREPL connect your computer to the ESP8266’s access point (MicroPython-xxxxxx, see the previous section about this). If you have already reconfigured your ESP8266 to connect to a router then you can skip this part.

Once you are on the same network as the ESP8266 you click the “Connect” button (if you are connecting via a router then you may need to change the IP address, by default the IP address is correct when connected to the ESP8266’s access point). If the connection succeeds then you should see a password prompt.

Once you type the password configured at the setup step above, press Enter once more and you should get a prompt looking like >>>. You can now start typing Python commands!

以上參見micropython docs

初次設置好會提示重啟,然後電腦就可以通過serial或者瀏覽器http://micropython.org/webrepl/和micropython交互了,貼張圖:

技術分享圖片

先通過repl交互,如果之前設置好網絡的話,esp8266開機之後會自動連接,這一點很方便,

這裏通過可以通過.ifconfig()查看IP或者進入路由器後臺查看也行,然後把地址填入webrepl點擊連接,輸入密碼即可.

可以看到兩側的信息交互是同步的,在一端輸入另一端自動跟隨.右側可以上傳或者下載文件.


現在進入正題,micropython實現了spi iic等接口,還寫了ssd1306,但是看過我之前的博文可以知道,我自己做過1.3寸oled的轉接板,這用的是sh1106.

然後查了查好像micropython還沒有實現,可能沒工夫管這麽小的地方吧,畢竟.96的ssd1306廣為人知.

然後之前自己也改過ssh1306的程序,就是有偏移而已,正尋思著要不要自己改改用?百度了一下,國內好像沒有,打梯子google一下,發現頁首兩個好像靠譜:

貼上地址吧:

https://github.com/robert-hh/SH1106 這個項目裏面好像說是基於別人的,然後由於看到另一個更為直觀

https://blog.boochow.com/article/453949310.html這個是個日本佬的博客,直接放出代碼,我采用了這個,效果不錯!

當然啦,它用的SPI那會的和現在的有點區別可能,根據traceback刪掉第一個參數(你進這個地址去看,和我下面貼的程序就知道哪個參數了,就是哪個1),之後ctrl e進去 ctrl d屏幕竟然就直接點亮了,(當然這裏還稍微改了改pin)

貼上我的代碼:

from micropython import const
from ssd1306 import SSD1306_SPI

SET_LOW_COLUMN      = const(0x00)
SET_HIGH_COLUMN     = const(0x10)
SET_PAGE_ADDR       = const(0xb0)
SET_DISP_START_LINE = const(0x40)

class SH1106_SPI(SSD1306_SPI):
    def show(self):
        for pg in range(0, self.pages):
            for cmd in (
                SET_PAGE_ADDR | pg,
                SET_LOW_COLUMN | 2,
                SET_HIGH_COLUMN | 0,
                ):
                self.write_cmd(cmd)
            self.write_data(self.buffer[pg * 0x80:(pg + 1) * 0x80])

#sh1106_spi.py

from sh1106 import SH1106_SPI
from machine import Pin, SPI
spi = SPI(baudrate=8000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
oled = SH1106_SPI(128, 64, spi, dc=Pin(16), res=Pin(15), cs=Pin(5))
oled.line(0,0,128,64,1);oled.show()
oled.line(128,0,0,64,1);oled.show()
oled.text(SH1106,36,0);oled.show()
oled.text(Micropython,24,56);oled.show()

註釋之前的保存為sh1106.py 然後上傳到esp,然後下面的直接ctrl e,效果很好!

技術分享圖片

這裏可以體現出前人栽樹後人乘涼的好處了,也意識到梯子很有用.

這裏這塊板子直接用esp12e,左側兩個按鍵,一個復位一個io0,背面飛線接spi,就不貼圖了,

我給出引腳定義:

GND VCC D0 D1 RES DC CS(我做的板子cs直接接地了)

D0接sck D1接mosi res接的io15 dc接的io16 至於程序裏面dc我分配了引腳,是因為現在還沒搞清楚怎樣丟給它一個None,我直接賦值None不行,直接丟個空的io給它吧,之後再研究...

本來是想做個時鐘,數碼管io占用太多,然後想到了oled,我有兩種.66和1.3,.66的太小了,也想過兩塊拼一起,emmm太麻煩了.

先進行到這一步吧!

micropython驅動sh1106點亮oled