This commit is contained in:
wiki-labplus 2018-10-17 15:52:05 +08:00
parent 2e16b38db2
commit f45b5a3fcf
2 changed files with 153 additions and 66 deletions

@ -1,14 +1,23 @@
# mPython drivers for MicroPython # labplus mPython library
# MIT license; Copyright (c) 2018 ZhangKaiHua # MIT license; Copyright (c) 2018 labplus
# v1.0 # V1.0 Zhang KaiHua(apple_eat@126.com)
from machine import I2C, PWM, Pin, ADC # mpython buildin periphers drivers
from machine import I2C, PWM, Pin, ADC, TouchPad
from ssd1106 import SSD1106_I2C from ssd1106 import SSD1106_I2C
import esp import esp
import ustruct import ustruct
from neopixel import NeoPixel from neopixel import NeoPixel
from time import sleep_ms, sleep_us
pins_remap_esp32 = [33, 32, 35, 34, 39, 0, 16, 17, 26, 25,
36, 2, -1, 18, 19, 21, 5, -1, -1, 22,
23, -1, -1,
27, 14, 12, 13, 15, 4]
i2c = I2C(scl=Pin(22), sda=Pin(23), freq=400000)
i2c = I2C(scl=Pin(22,mode=Pin.OUT ,pull=None), sda=Pin(23,mode=Pin.OUT ,pull=None), freq=400000)
class Font(object): class Font(object):
def __init__(self, font_address=0x300000): def __init__(self, font_address=0x300000):
@ -16,28 +25,31 @@ class Font(object):
buffer = bytearray(18) buffer = bytearray(18)
esp.flash_read(self.font_address, buffer) esp.flash_read(self.font_address, buffer)
self.header, \ self.header, \
self.height, \ self.height, \
self.width, \ self.width, \
self.baseline, \ self.baseline, \
self.x_height, \ self.x_height, \
self.Y_height, \ self.Y_height, \
self.first_char,\ self.first_char,\
self.last_char = ustruct.unpack('4sHHHHHHH', buffer) self.last_char = ustruct.unpack('4sHHHHHHH', buffer)
self.first_char_info_address = self.font_address + 18 self.first_char_info_address = self.font_address + 18
def GetCharacterData(self, c): def GetCharacterData(self, c):
uni = ord(c) uni = ord(c)
if uni not in range(self.first_char, self.last_char): if uni not in range(self.first_char, self.last_char):
return None return None
char_info_address = self.first_char_info_address + (uni - self.first_char) * 6 char_info_address = self.first_char_info_address + \
(uni - self.first_char) * 6
buffer = bytearray(6) buffer = bytearray(6)
esp.flash_read(char_info_address, buffer) esp.flash_read(char_info_address, buffer)
ptr_char_data, len = ustruct.unpack('IH', buffer) ptr_char_data, len = ustruct.unpack('IH', buffer)
if (ptr_char_data) == 0 or (len == 0): if (ptr_char_data) == 0 or (len == 0):
return None return None
buffer = bytearray(len) buffer = bytearray(len)
esp.flash_read(ptr_char_data + self.font_address, buffer) esp.flash_read(ptr_char_data + self.font_address, buffer)
return buffer return buffer
class Accelerometer(): class Accelerometer():
""" """ """ """
def __init__(self): def __init__(self):
@ -57,6 +69,7 @@ class Accelerometer():
buf = self.i2c.readfrom(self.addr, 2) buf = self.i2c.readfrom(self.addr, 2)
y = ustruct.unpack('h', buf)[0] y = ustruct.unpack('h', buf)[0]
return y / 4 / 4096 return y / 4 / 4096
def get_z(self): def get_z(self):
self.i2c.writeto(self.addr, b'\x06', False) self.i2c.writeto(self.addr, b'\x06', False)
buf = self.i2c.readfrom(self.addr, 2) buf = self.i2c.readfrom(self.addr, 2)
@ -64,32 +77,32 @@ class Accelerometer():
return z / 4 / 4096 return z / 4 / 4096
class TextMode(): class TextMode():
normal = 1 normal = 1
rev = 2 rev = 2
trans = 3 trans = 3
xor = 4 xor = 4
class Display(SSD1106_I2C):
class OLED(SSD1106_I2C):
""" 128x64 oled display """ """ 128x64 oled display """
def __init__(self): def __init__(self):
super().__init__(128, 64, i2c) super().__init__(128, 64, i2c)
self.f = Font() self.f = Font()
if self.f == None: if self.f is None:
raise Exception('font load failed') raise Exception('font load failed')
def DispChar(self, s, x, y, mode = TextMode.normal): def DispChar(self, s, x, y, mode=TextMode.normal):
if self.f == None: if self.f is None:
return return
for c in s: for c in s:
data = self.f.GetCharacterData(c) data = self.f.GetCharacterData(c)
if data == None: if data is None:
x = x + self.width x = x + self.width
continue continue
width, bytes_per_line = ustruct.unpack('HH', data[:4]) width, bytes_per_line = ustruct.unpack('HH', data[:4])
# print('character [%d]: width = %d, bytes_per_line = %d' % (ord(c), width, bytes_per_line)) # print('character [%d]: width = %d, bytes_per_line = %d' % (ord(c)
# , width, bytes_per_line))
for h in range(0, self.f.height): for h in range(0, self.f.height):
w = 0 w = 0
i = 0 i = 0
@ -106,17 +119,18 @@ class Display(SSD1106_I2C):
px = x + w + p px = x + w + p
c = 0 c = 0
if (mask & 0x80) != 0: if (mask & 0x80) != 0:
if mode == TextMode.normal or mode == TextMode.trans: if mode == TextMode.normal or \
c = 1 mode == TextMode.trans:
if mode == TextMode.rev:
c = 0
if mode == TextMode.xor:
c = self.buffer[page * 128 + px] & bit
if c != 0:
c = 0
else:
c = 1 c = 1
print("px = %d, py = %d, c = %d" % (px, py, c)) if mode == TextMode.rev:
c = 0
if mode == TextMode.xor:
c = self.buffer[page * 128 + px] & bit
if c != 0:
c = 0
else:
c = 1
print("px = %d, py = %d, c = %d" % (px, py, c))
super().pixel(px, py, c) super().pixel(px, py, c)
else: else:
if mode == TextMode.normal: if mode == TextMode.normal:
@ -132,29 +146,103 @@ class Display(SSD1106_I2C):
class Buzz(object): class Buzz(object):
def __init__(self, pin = 16):
self.pin = pin def __init__(self, pin=6):
self.io = Pin(self.pin) self.id = pins_remap_esp32[pin]
self.io = Pin(self.id)
self.io.value(1) self.io.value(1)
self.isOn = False self.isOn = False
def on(self, freq = 500): def on(self, freq=500):
if self.isOn == False: if self.isOn is False:
self.pwm = PWM(self.io, freq, 512) self.pwm = PWM(self.io, freq, 512)
self.isOn = True self.isOn = True
def off(self): def off(self):
if self.isOn == True: if self.isOn:
self.pwm.deinit() self.pwm.deinit()
self.io.init(self.pin, Pin.OUT) self.io.init(self.id, Pin.OUT)
self.io.value(1) self.io.value(1)
self.isOn = False self.isOn = False
class PinMode(object):
IN = 1
OUT = 2
PWM = 3
ANALOG = 4
class MPythonPin(Pin):
def __init__(self, pin, mode=PinMode.IN):
if mode not in [PinMode.IN, PinMode.OUT, PinMode.PWM, PinMode.ANALOG]:
raise TypeError("mode must be 'IN, OUT, PWM, ANALOG'")
if pin == 3:
raise TypeError("pin3 is used for resistance sensor")
if pin == 4:
raise TypeError("pin4 is used for light sensor")
if pin == 10:
raise TypeError("pin10 is used for sound sensor")
self.id = pins_remap_esp32[pin]
if mode == PinMode.IN:
super().__init__(self.id, Pin.IN, Pin.PULL_UP)
if mode == PinMode.OUT:
if pin == 2:
raise TypeError('pin2 only can be set "IN, ANALOG"')
super().__init__(self.id, Pin.OUT)
if mode == PinMode.PWM:
if pin == 2:
raise TypeError('pin2 only can be set "IN, ANALOG"')
self.pwm = PWM(Pin(self.id), duty=0)
if mode == PinMode.ANALOG:
if pin not in [0, 1, 2, 3, 4, 10]:
raise TypeError('the pin can~t be set as analog')
self.adc = ADC(Pin(self.id))
self.adc.atten(ADC.ATTN_11DB)
self.mode = mode
def read_digital(self):
if not self.mode == PinMode.IN:
raise TypeError('the pin is not in IN mode')
return super().value()
def write_digital(self, value):
if not self.mode == PinMode.OUT:
raise TypeError('the pin is not in OUT mode')
super().value(value)
def read_analog(self):
if not self.mode == PinMode.ANALOG:
raise TypeError('the pin is not in ANALOG mode')
return self.adc.read()
def write_analog(self, duty, freq=1000):
if not self.mode == PinMode.PWM:
raise TypeError('the pin is not in PWM mode')
self.pwm.freq(freq)
self.pwm.duty(duty)
'''
# to be test
class LightSensor(ADC):
def __init__(self):
super().__init__(Pin(pins_remap_esp32[4]))
# super().atten(ADC.ATTN_11DB)
def value(self):
# lux * k * Rc = N * 3.9/ 4096
# k = 0.0011mA/Lux
# lux = N * 3.9/ 4096 / Rc / k
return super().read() * 1.1 / 4095 / 6.81 / 0.011
'''
# buzz # buzz
buzz = Buzz() buzz = Buzz()
# display # display
display = Display() oled = OLED()
display = oled
# 3 axis accelerometer # 3 axis accelerometer
accelerometer = Accelerometer() accelerometer = Accelerometer()
@ -173,11 +261,10 @@ sound = ADC(Pin(36))
button_a = Pin(0, Pin.IN, Pin.PULL_UP) button_a = Pin(0, Pin.IN, Pin.PULL_UP)
button_b = Pin(2, Pin.IN, Pin.PULL_UP) button_b = Pin(2, Pin.IN, Pin.PULL_UP)
# touchpad
touchPad_P = TouchPad(Pin(27))
touchPad_Y = TouchPad(Pin(14))
touchPad_T = TouchPad(Pin(12))
touchPad_H = TouchPad(Pin(13))
touchPad_O = TouchPad(Pin(15))
touchPad_N = TouchPad(Pin(4))