Skip to content

Instantly share code, notes, and snippets.

View boochow's full-sized avatar
🏠
Working from home

boochow

🏠
Working from home
View GitHub Profile
from machine import Pin, SPI
spi = SPI(0)
dc = Pin(27,Pin.OUT)
res = Pin(22,Pin.OUT)
cs = Pin(17,Pin.OUT)
d = SSD1306_SPI(128,64,spi,dc,res,cs)
d.text("SPI OK", 0, 0)
d.show()
class MCP3204:
def __init__(self, spi, cs):
self.spi = spi
self.cs = cs
def read(self, ch):
cmd = 6 | (ch >> 2)
par = (ch << 6) & 0xff
send = bytearray([cmd, par, 0])
recv = bytearray(3)
# stm32 version of wiznet_connect.py
import network
nic = network.Ethernet()
while not nic.isconnected():
pass
nic.active(True)
nic.ifconfig('dhcp')
print(nic.ifconfig())
import socket
print(socket.getaddrinfo('micropython.org', 80))
import uio
class FBConsole(uio.IOBase):
def __init__(self, framebuf, bgcolor=0, fgcolor=-1, width=-1, height=-1):
self.fb = framebuf
if width > 0:
self.width=width
else:
try:
self.width=framebuf.width
from machine import I2C
i2c=I2C(1)
i2c.init(freq=1000000)
oled = SSD1306_I2C(128, 64, i2c)
console = FBConsole(oled)
os.dupterm(console)
import gpu
import framebuf
import FBConsole
class RPiScreen(framebuf.FrameBuffer):
def __init__(self, width, height):
self.width = width
self.height = height
gpu.fb_init(width,height,screen_w=1920,screen_h=1080)
super().__init__(gpu.fb_data(),width,height,framebuf.RGB565)
from mcu import mem32
# PWM clock manager
CLK_GND = const(0)
CLK_OSC = const(1)
CLK_PLLA = const(4)
CLK_PLLC = const(5)
CLK_PLLD = const(6)
CLK_HDMI = const(7)
# GPIO alternate functions
ALT0 = const(4)
ALT1 = const(5)
ALT5 = const(2)
Pin_AF_PWM = {12:(ALT0,0), 13:(ALT0,1), 18:(ALT5,0), 19:(ALT5,1), \
40:(ALT0,0), 41:(ALT0,1), 45:(ALT0,1), 52:(ALT1,0), 53:(ALT1,1)}
def select_PWM_pin(pin):
if pin in Pin_AF_PWM.keys():
@boochow
boochow / pwm.py
Last active October 8, 2018 10:33
# Hardware PWM
MS_DISABLE = const(0)
MS_ENABLE = const(1)
MODE_PWM = const(0)
MODE_SERIALIZER = const(1)
DEFAULT_RANGE = const(960)
class PWM:
from time import sleep_ms
def test():
p=PWM(0, pin=18, ms=MS_ENABLE)
p.freq(1000)
p.range(DEFAULT_RANGE)
while True:
for i in range(DEFAULT_RANGE // 2):
p.data(i)
sleep_ms(1)
for i in range(DEFAULT_RANGE // 2 - 1, 0, -1):