Skip to content

Instantly share code, notes, and snippets.

@moinologics
Created July 14, 2021 17:09
Show Gist options
  • Save moinologics/f707e8a5c561eed2abc5d6a1450ca856 to your computer and use it in GitHub Desktop.
Save moinologics/f707e8a5c561eed2abc5d6a1450ca856 to your computer and use it in GitHub Desktop.
micropython class for contolling esp8266 pins
from machine import Pin
class Pins:
__configs = {
4: {
"mode": 'out',
"value": 0
},
5: {
"mode": 'out',
"value": 0
},
14: {
"mode": 'in',
"value": None
}
}
__pins = {}
def __init__(self):
for p_no,conf in self.__configs.items():
self.__pins[p_no] = Pin(p_no, Pin.IN if conf['mode']=='in' else Pin.OUT)
if conf['mode']==Pin.OUT:
self.__pins[p_no].value(conf['value'])
def set_states(self, states):
for p_no,value in states.items():
p_no = int(p_no)
if (p_no in self.__pins) and (self.__configs[p_no]['mode'] == 'out') and (value in [0,1]):
self.__pins[p_no].value(value)
self.__configs[p_no]['value'] = value
def get_states(self):
for p_no,conf in self.__configs.items():
if conf['mode'] == 'in':
self.__configs[p_no]['value'] = self.__pins[p_no].value()
return self.__configs
def get_info(self):
return {'pins': self.__pins, 'config': self.__configs}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment