def setup(use_spi=False, soft=True): if use_spi: # Pyb SSD # 3v3 Vin # Gnd Gnd # X1 DC # X2 CS # X3 Rst # X6 CLK # X8 DATA pdc = machine.Pin('X1', machine.Pin.OUT_PP) pcs = machine.Pin('X2', machine.Pin.OUT_PP) prst = machine.Pin('X3', machine.Pin.OUT_PP) if soft: spi = machine.SPI(sck=machine.Pin('X6'), mosi=machine.Pin('X8'), miso=machine.Pin('X7')) else: spi = machine.SPI(1) ssd = SSD1306_SPI(WIDTH, HEIGHT, spi, pdc, prst, pcs) else: # I2C # Pyb SSD # 3v3 Vin # Gnd Gnd # Y9 CLK # Y10 DATA if soft: pscl = machine.Pin('Y9', machine.Pin.OPEN_DRAIN) psda = machine.Pin('Y10', machine.Pin.OPEN_DRAIN) i2c = machine.I2C(scl=pscl, sda=psda) else: i2c = machine.I2C(2) ssd = SSD1306_I2C(WIDTH, HEIGHT, i2c) return ssd
def __init__(self, pin_dc, pin_cs, pin_rst, pin_sck, pin_mosi, pin_miso): pdc = Pin(pin_dc, Pin.OUT) #27 pcs = Pin(pin_cs, Pin.OUT) #26 prst = Pin(pin_rst, Pin.OUT) #18 #sck:22; mosi:23; miso:4 #spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(pin_sck), mosi=Pin(pin_mosi), miso=Pin(pin_miso)) spi = SPI(1, 80000000, sck=Pin(14), mosi=Pin(13)) #spi.init(baudrate=200000) # set the baudrate self.ssd = SSD1306_SPI(WIDTH, HEIGHT, spi, pdc, prst, pcs)
def __init__(self): from machine import Pin spi = machine.SPI(1) reset_pin = Pin('PA6', Pin.OUT) dc_pin = Pin('PA8', Pin.OUT) cs_pin = Pin('PA4', Pin.OUT) self.dis = SSD1306_SPI(128, 64, spi, dc_pin, reset_pin, cs_pin) self.clear() self.show()
def test(use_spi=False): if use_spi: # Pyb SSD # 3v3 Vin # Gnd Gnd # X1 DC # X2 CS # X3 Rst # X6 CLK # X8 DATA pdc = machine.Pin('X1', machine.Pin.OUT_PP) pcs = machine.Pin('X2', machine.Pin.OUT_PP) prst = machine.Pin('X3', machine.Pin.OUT_PP) # spi = machine.SPI('Y') spi = machine.SPI(sck=machine.Pin('X6'), mosi=machine.Pin('X8'), miso=machine.Pin('X7')) ssd = SSD1306_SPI(WIDTH, HEIGHT, spi, pdc, prst, pcs) else: # I2C # Pyb SSD # 3v3 Vin # Gnd Gnd # Y9 CLK # Y10 DATA pscl = machine.Pin(4, machine.Pin.OUT) psda = machine.Pin(5, machine.Pin.OUT) i2c = machine.I2C(scl=pscl, sda=psda) # i2c = machine.I2C(2) ssd = SSD1306_I2C(WIDTH, HEIGHT, i2c) rhs = WIDTH - 1 ssd.line(rhs - 20, 0, rhs, 20, 1) square_side = 10 ssd.fill_rect(rhs - square_side, 0, square_side, square_side, 1) #wri = Writer(ssd, freeserif19) wri2 = Writer(ssd, freesans20, verbose=False) Writer.set_clip(True, True) Writer.set_textpos(0, 0) wri2.printstring('Abhishek is Awesome\n') wri2.printstring('8 Nov 2016\n') wri2.printstring('10.30am') ssd.show()
import onewire, ds18x20 import network import urequests HTTP_SERVER = "http://136.167.58.213:1880" # The IP address of the raspberry pi HTTP_NODE = "/sensor" RATE = 10 # How often do you want the update to be dat = Pin(2) d = DHT11(Pin(16)) ds = ds18x20.DS18X20(onewire.OneWire(dat)) roms = ds.scan() ip = network.WLAN(network.STA_IF).ifconfig()[0] spi = SPI(1, baudrate=8000000, polarity=0, phase=0) oled = SSD1306_SPI(128,64, spi, res=Pin(4), dc=Pin(5), cs=Pin(15)) while True: oled.fill(0) oled.text(ip, 0, 0) d.measure() temp = 1.8*d.temperature()+32 hum = d.humidity() oled.text("Temp: {} F".format(temp), 0, 20) oled.text("Hum: {}%".format(hum), 0, 30) ds.convert_temp() sleep_ms(750) water_temp = "" for rom in roms: water_temp += str(ds.read_temp(rom))
# MOSI --> D6 ---> 12 # MIOS --> Null-->任意没有用的 # DC --> D8 ---> 15 # RES --> D7 ---> 13 # CS --> Null-->本次使用6针没有该引脚,指定一个空引脚即可 # 根据上面的连线,定义各个IO接口变量 SCK = Pin(14, Pin.OUT) MOSI = Pin(12, Pin.OUT) MISO = Pin(0) DC = Pin(15) RES = Pin(13) CS = Pin(16) # 初始化SPI对象,sck,mosi,miso 三个参数是必须的 spi = SPI(sck=SCK, mosi=MOSI, miso=MISO) # 初始化OLED对象,128*64 是屏幕的分辨率,所谓的大小 oled = SSD1306_SPI(128, 64, spi, dc=DC, res=RES, cs=CS) # 打开SPI通道 oled.poweron() # 初始化OLED显示 oled.init_display() def image(img_list): s = time.ticks_ms() oled.fill(0) for i in img_list: oled.hline(2 * i[0], 2 * i[1], 2 * i[2], 1) oled.show() e = time.ticks_ms() - s if e < 100: time.sleep_ms(90 - e)
from ssd1306 import SSD1306_SPI WIDTH = const(128) HEIGHT = const(64) pdc = machine.Pin(27, machine.Pin.OUT) pcs = machine.Pin(26, machine.Pin.OUT) sck_pin = machine.Pin(19, machine.Pin.OUT) mosi_pin = machine.Pin(23, machine.Pin.OUT) miso_pin = machine.Pin(25, machine.Pin.IN) prst = machine.Pin(18, machine.Pin.OUT) spi = machine.SPI(1, baudrate=1000000, sck=sck_pin, mosi=mosi_pin, miso=miso_pin) ssd = SSD1306_SPI(WIDTH, HEIGHT, spi, pdc, prst, pcs) import freesans20 from writer import Writer wri2 = Writer(ssd, freesans20, verbose=True) Writer.set_clip(True, True) Writer.set_textpos(0, 0) wri2.printstring('MicroPython\n') ssd.show()
# This program is distributed under Apache License Version 2.0 # # (ɔ) Albertas Mickėnas 2016 # [email protected] # [email protected] # import time from machine import I2C, SPI, Pin from sht25 import SHT25 from ssd1306 import SSD1306_SPI import network from umqtt.simple import MQTTClient sensor = SHT25(I2C(scl=Pin(5), sda=Pin(4))) display = SSD1306_SPI(128, 32, SPI(1), Pin(0), Pin(2), Pin(15)) def displayData(temperature, humidity): global display display.fill(0) display.text("Humidity", 0, 0) display.text("Temp", 70, 0) display.text(str(round(humidity, 2))[0:5] + "%", 8, 12) display.text(str(round(temperature, 2))[0:5] + "C", 70, 12) display.show() sta_if = network.WLAN(network.STA_IF) sta_if.active(True) display.fill(0);
from machine import SoftSPI,Pin from ssd1306 import SSD1306_SPI spi = SoftSPI(baudrate=10000000, polarity=1, phase=0, sck=Pin(17,Pin.OUT), mosi=Pin(16,Pin.OUT), miso=Pin(10))#sck=D0, mosi=D1 oled = SSD1306_SPI(128, 64, spi, dc=Pin(14),res=Pin(15), cs=Pin(13))#dc=DC, res=RES, cs=CS&GND def display(): oled.text("Hello,I am wind.", 0, 32) oled.show() if __name__=="__main__": display()
i2s_adc.init(20000) LED = Pin(18, Pin.OUT) LED.on() KEY1 = Pin(5, Pin.IN) KEY2 = Pin(17, Pin.IN) KEY3 = Pin(16, Pin.IN) KEY4 = Pin(4, Pin.IN) spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(12), mosi=Pin(14), miso=Pin(21)) oled = SSD1306_SPI(128, 64, spi, Pin(26, Pin.OUT), Pin(27, Pin.OUT), Pin(10, Pin.OUT)) oled.show() #OLED执行显示 top, downstep = [0.] * 64, 2 # 用于存放最上面的点 x1 = array.array('f', [0] * 1024) mark = 5 def run(): while True: start = time.ticks_ms() oled.fill(0) i2s_adc.read_to_array(x1) fft_real(x1) x = ezabs(x1)[0:128] # 确定缩放系数 限定在mark以内 ruler = 64 / (max(x[2:64]) + 0.001)
from ssd1306 import SSD1306_SPI from sh1106 import SH1106_SPI from machine import Pin, SPI spi = SPI(2, baudrate=8000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) oled = SSD1306_SPI(128, 64, spi, dc=Pin(16), res=Pin(17), cs=Pin(5)) def ssd1306_spi_lump_print_str(oled, text, x_size, y_size, x_axis, y_axis): max_row = y_size // 16 max_col = x_size // 8 x_axis = x_axis // 8 y_axis = y_axis // 16 row = 0 col = 0 for i in text: is_cn = i.encode('utf-8') if col == max_col: col = 0 row += 1 if row == max_row: break else: if len(is_cn) >= 2: if col == max_col - 1: row += 1
SERVER = "47.103.121.23" CLIENT_ID = "L9ZPRGlt6Bl7P8FXYRRd" TOPIC = "v1/devices/me/telemetry" username = '******' passwd = '' # 采用默认的SPI引脚 # spi = SPI(1); spi = SPI(baudrate=10000000, polarity=1, phase=0, sck=Pin(2, Pin.OUT), mosi=Pin(0, Pin.OUT), miso=Pin(12)) # D0-CLK/sck D1-MOSI 采用自定义的SPI引脚 display = SSD1306_SPI(128, 64, spi, Pin(5), Pin(4), Pin(16)) # 采用自定义的DC RES CS 引脚 def wifi_connect(essid, password): if essid == None or essid == '': raise BaseException('essid can not be null') if password == None or password == '': raise BaseException('password can not be null') sta_if = network.WLAN(network.STA_IF) if not sta_if.active(): #print("set sta active") sta_if.active(True) if not sta_if.isconnected(): #print('connecting to network...') sta_if.connect(essid, password)
# MOSI --> D6 ---> 12 # MIOS --> Null-->任意没有用的 # DC --> D8 ---> 15 # RES --> D7 ---> 13 # CS --> Null-->本次使用6针没有该引脚,指定一个空引脚即可 # 根据上面的连线,定义各个IO接口变量 SCK = Pin(14, Pin.OUT) MOSI = Pin(12, Pin.OUT) MISO = Pin(0) DC = Pin(15) RES = Pin(13) CS = Pin(16) # 初始化SPI对象,sck,mosi,miso 三个参数是必须的 spi = SPI(sck=SCK, mosi=MOSI, miso=MISO) # 初始化OLED对象,128*64 是屏幕的分辨率,所谓的大小 display = SSD1306_SPI(128, 64, spi, dc=DC, res=RES, cs=CS) # 打开SPI通道 display.poweron() # 初始化OLED显示 display.init_display() # 显示西瓜抖音头条一点,字体大小为16*16像素大小 x = 0 # 横向我们从0像素点开始显示,也就是左上角 for f in fonts: # 从列表中依次取每个字的数据 for index in range(0, 16): # 由于我们设置的字体宽度是16像素,每个字我们有16列, # 取每一列的数据,由于我们用的C51格式,每个数据,都有8个值,也就是8行,我们把二进制的数据中的0b前置项去掉,方便后续的补零操作。 a1 = bin(f[index]).replace("0b", "") while len(a1) < 8: # 如果数据不满8位,我们在数据前面加0进行补齐 a1 = "0" + a1 # 一个数据代表8行,我们设计的一个字体高度是16也就是16行,所有一列有两个数据组成,我们取第二个数据 # 我们选择的列行模式,先把第一段行数据列取完,在取下一个数据列,我们字体宽度是16像素,所有我们前16个数据是第一行的数据组
mosi = Pin.board.P23 cs = Pin.board.P4 dc = Pin.board.P28 res = Pin.board.P29 elif 'PCA10056' in mch: sck = Pin.board.P47 miso = Pin.board.P46 mosi = Pin.board.P45 cs = Pin.board.P4 dc = Pin.board.P28 res = Pin.board.P29 elif 'PCA10090' in mch: sck = Pin.board.P13 miso = Pin.board.P12 mosi = Pin.board.P11 cs = Pin.board.P15 dc = Pin.board.P16 res = Pin.board.P17 else: raise Exception('Board not supported!') spi = SPI(1, sck=sck, mosi=mosi, miso=miso) print(spi) disp_spi = SSD1306_SPI(128, 64, spi, dc, res, cs) disp_spi.fill(0) disp_spi.show() disp_spi.text("Hello", 5, 5, 1) disp_spi.text("World!", 5, 30, 1) disp_spi.show() print(len(disp_spi.buffer))