Rewrote app (added classes)

This commit is contained in:
dbroqua 2021-01-23 22:08:40 +01:00
parent 5e05666a87
commit cdfbd29426
4 changed files with 152 additions and 116 deletions

View file

@ -5,7 +5,20 @@ MOSI = 10
CS = 8 CS = 8
[button] [button]
# GPIO Pin
BUTTON = 5 BUTTON = 5
[voltage_sensor] [voltage_sensor]
# grove or arceli
MODEL = grove MODEL = grove
[sensors]
# voltage, temperature or pressure
SENSOR_0 = voltage
SENSOR_1 = temperature
SENSOR_2 = temperature
SENSOR_3 = temperature
SENSOR_4 = temperature
SENSOR_5 = temperature
SENSOR_6 = temperature
SENSOR_7 = pressure

20
display.py Normal file
View file

@ -0,0 +1,20 @@
import board
from adafruit_ht16k33.segments import Seg7x4
class Display:
"""Class for communicate with display"""
def __init__(self):
i2c = board.I2C()
self.display = Seg7x4(i2c)
self.display.brightness = 1
self.display.blink_rate = 0
def clear(self):
self.display.fill(0)
def print(self, value):
self.clear()
self.display.print(str(value))

View file

@ -4,153 +4,58 @@ import board
import configparser import configparser
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from datetime import datetime from sensors import Sensors
from adafruit_ht16k33.segments import Seg7x4 from display import Display
config = configparser.RawConfigParser() config = configparser.RawConfigParser()
config.read(r'./config.ini') config.read(r'./config.ini')
# Voltage Sersor display = Display()
VOLTAGE_SENSOR_MODEL = config.get('voltage_sensor', 'MODEL') sensor = Sensors(config)
# ADC configuration
CLK = int(config.get('adc', 'CLK'))
MISO = int(config.get('adc', 'MISO'))
MOSI = int(config.get('adc', 'MOSI'))
CS = int(config.get('adc', 'CS'))
CHANNEL = 0 # By default read sensor 0
# Display configuration
i2c = board.I2C()
display = Seg7x4(i2c)
# Button
BUTTON = int(config.get('button', 'BUTTON'))
# Function called on push button (change sensor)
def handlePushButton(button):
global CHANNEL
CHANNEL += 1
if ( CHANNEL > 8 ):
CHANNEL = 0
display.print(" :"+str(CHANNEL)+"-")
# On exit
def handleExit():
print("GoodBye!")
display.print("--:--")
time.sleep(2)
display.fill(0)
# Init program # Init program
def init(): def init():
# Set display # Button
display.brightness = 1 BUTTON = int(config.get('button', 'BUTTON'))
display.blink_rate = 0 # Between 0 and 3
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(MOSI, GPIO.OUT)
GPIO.setup(MISO, GPIO.IN)
GPIO.setup(CLK, GPIO.OUT)
GPIO.setup(CS, GPIO.OUT)
# Add button event # Add button event
GPIO.add_event_detect(BUTTON, GPIO.FALLING, callback=handlePushButton, bouncetime=250) GPIO.add_event_detect(BUTTON, GPIO.FALLING, callback=handlePushButton, bouncetime=250)
pass pass
# Function called on push button (change sensor)
def handlePushButton(button):
channel = sensor.getChannel() + 1
if (channel > 8):
channel = 0
# Convert value to volt sensor.setChannel(channel)
def convertToVoltage(value): display.print(" :"+str(channel)+"-")
correction = 1
if ( VOLTAGE_SENSOR_MODEL == "arceli" ):
correction = 2
return value * ( 3.3 / 1024 ) * 5 / correction
# Convert volt to celcius value
def convertToCelcius(value):
return ( ( 3300.0 / 1024.0 ) - 100.0 ) / 10.0 - 40.0
#read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
#adcout >>= 1 # first bit is 'null' so drop it
return adcout
# On exit
def handleExit():
print("GoodBye!")
display.print("--:--")
time.sleep(2)
display.clear()
# Main programm # Main programm
def main(): def main():
init() init()
time.sleep(2)
print("Let's go!") print("Let's go!")
while True: while True:
if ( CHANNEL == 8 ): # Last sensor => clock mode display.print(sensor.getValue())
now = datetime.now()
printableValue = now.strftime("%H:%M")
else:
# Get current sensor value
ad_value = readadc(CHANNEL, CLK, MOSI, MISO, CS)
value = 0
# Convert value in readable Volt or Celcius # Wait before next loop
if ( CHANNEL == 0 ):
value = convertToVoltage(ad_value)
else:
value = convertToCelcius(ad_value)
# Format value with 2 decimals
printableValue = f"{value:.02f}"
# Clear display
display.fill(0)
# Print new value
display.print(str(printableValue))
# Wait between next loop
time.sleep(10) time.sleep(10)
atexit.register(handleExit) atexit.register(handleExit)
if __name__ =='__main__': if __name__ =='__main__':

98
sensors.py Normal file
View file

@ -0,0 +1,98 @@
import RPi.GPIO as GPIO
from datetime import datetime
class Sensors:
"""Class for communicate with sensors through MCP3008"""
def __init__(self, config):
self.mosi = int(config.get('adc', 'MOSI'))
self.miso = int(config.get('adc', 'MISO'))
self.clk = int(config.get('adc', 'CLK'))
self.cs = int(config.get('adc', 'CS'))
self.voltage_sensor_model = config.get('voltage_sensor', 'MODEL')
self.channel = 0
self.config = config
GPIO.setup(self.mosi, GPIO.OUT)
GPIO.setup(self.miso, GPIO.IN)
GPIO.setup(self.clk, GPIO.OUT)
GPIO.setup(self.cs, GPIO.OUT)
# read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
def readadc(self):
adcnum = self.getChannel()
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(self.cs, True)
GPIO.output(self.clk, False) # start clock low
GPIO.output(self.cs, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(self.mosi, True)
else:
GPIO.output(self.mosi, False)
commandout <<= 1
GPIO.output(self.clk, True)
GPIO.output(self.clk, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(self.clk, True)
GPIO.output(self.clk, False)
adcout <<= 1
if (GPIO.input(self.miso)):
adcout |= 0x1
GPIO.output(self.cs, True)
return adcout
# Convert value to volt
def convertToVoltage(self, value):
correction = 1
if (self.voltage_sensor_model == "arceli"):
correction = 2
return value * (3.3 / 1024) * 5 / correction
# Convert volt to celcius value
def convertToCelcius(self, value):
return ((3300.0/1024.0) - 100.0) / 10.0 - 40.0
def convertToPrintableValue(self, value):
if (self.getChannelType() == 'voltage'):
return self.convertToVoltage(value)
else:
return self.convertToCelcius(value)
def getChannelType(self):
return self.config.get('sensors', 'SENSOR_' + str(self.channel))
def getChannel(self):
return self.channel
def setChannel(self, channel):
self.channel = channel
def getValue(self):
if (self.channel == 8):
now = datetime.now()
return now.strftime("%H:%M")
else:
raw_value = self.readadc()
value = self.convertToPrintableValue(raw_value)
return f"{value:.02f}"
def main(self):
print('MAIN')