uPyLibs/uPyConfig.py

166 lines
5.0 KiB
Python

class uPyConfig:
from machine import I2C, Pin
class features:
class connectivity:
wifi = False
bluetooth = False
bluetooth_le = False
lora = False
class display:
oled = False
class sensor:
capacitive = False
temperature = False
hall_effect = False
class owc:
from onewire import OneWire
from machine import Pin
def __init__(self, pin):
if pin.__class__ != self.Pin: pin=self.Pin(pin)
self.bus = self.OneWire(pin)
class _i2c:
from machine import I2C, Pin
def load_handle(self):
self.bus = self.I2C(scl=self.scl, sda=self.sda)
self.bus.init(scl=self.scl, sda=self.sda)
def __init__(self, scl, sda):
self.scl=self.Pin(scl)
self.sda=self.Pin(sda)
self.load_handle()
class _oled:
from machine import Pin
addr = 0x0
rst = 0
rst_hold = False
height = 0
width = 0
def load_handle(self):
from time import sleep_ms
if self.rst_hold:
self.rst = self.Pin(self.rst, self.Pin.OUT)
self.rst.value(0)
sleep_ms(2)
self.rst.value(1)
from ssd1306 import SSD1306_I2C
self.handle = SSD1306_I2C(self.width, self.height, self.i2c.bus)
def __init__(self, i2c):
self.i2c = i2c
def __init__(self, board_defs):
self.i2c = self._i2c(board_defs['i2c_scl'], board_defs['i2c_sda'])
if 'has_wifi' in board_defs.keys(): self.features.connectivity.wifi = True
if 'has_bluetooth' in board_defs.keys(): self.features.connectivity.bluetooth = True
if 'has_bluetooth_le' in board_defs.keys(): self.features.connectivity.bluetooth_le = True
if 'sen_capacitive' in board_defs.keys(): self.features.sensor.capacitive = True
if 'sen_temperature' in board_defs.keys(): self.features.sensor.temperature = True
if 'sen_hall' in board_defs.keys(): self.features.sensor.hall_effect = True
if 'has_oled' in board_defs.keys():
self.features.display.oled = True
self.oled = self._oled(self.i2c)
self.oled.addr = board_defs['oled_addr']
if 'oled_hold' in board_defs.keys(): self.oled.rst_hold = board_defs['oled_hold']
if 'oled_rst' in board_defs.keys(): self.oled.rst = board_defs['oled_rst']
self.oled.width = board_defs['oled_width']
self.oled.height = board_defs['oled_height']
self.oled.load_handle()
class esp8266(uPyConfig):
family = 'esp8266'
defaults = {
'has_wifi': True,
'i2c_scl': 4,
'i2c_sda': 5,
'oled_addr': 0x3c,
}
variants = {
'generic': {},
'lolinv3': {
'i2c_scl': 4,
'i2c_sda': 5,
},
'd1-r2': {
'i2c_scl': 5,
'i2c_sda': 4,
},
'heltec': {
'i2c_scl': 5,
'i2c_sda': 4,
'oled_rst': 16,
'has_oled': True,
'oled_hold': True,
'oled_height': 32,
'oled_width': 128,
},
'ttgo': {
'i2c_scl': 14,
'i2c_sda': 2,
'oled_rst': 4,
'has_oled': True,
'oled_hold': True,
'oled_height': 32,
'oled_width': 128,
},
}
def __init__(self, variant='generic'):
if variant in self.variants.keys():
self.variant = variant
defs = self.defaults.copy()
defs.update(self.variants[variant])
super().__init__(defs)
else:
raise ValueError("Board variant '%s' is not known" % variant)
class esp32(uPyConfig):
family = 'esp32'
defaults = {
'has_wifi': True,
'has_bluetooth': True,
'has_bluetooth_le': True,
'sen_capacitive': True,
'sen_temperature': True,
'sen_hall': True,
'i2c_scl': 4,
'i2c_sda': 5,
'oled_addr': 0x3c,
}
variants = {
'generic': {},
'wemos-lolin32': {
'has_oled': True,
'oled_height': 64,
'oled_width': 128,
},
'heltec': {
'i2c_scl': 15,
'i2c_sda': 4,
'oled_rst': 16,
'has_oled': True,
'oled_hold': True,
'oled_height': 64,
'oled_width': 128,
},
}
def __init__(self, variant='generic'):
if variant in self.variants.keys():
self.variant = variant
defs = self.defaults.copy()
defs.update(self.variants[variant])
super().__init__(defs)
else:
raise ValueError("Board variant '%s' is not known" % variant)