Rewrote app (added classes)
This commit is contained in:
parent
5e05666a87
commit
cdfbd29426
4 changed files with 152 additions and 116 deletions
13
config.ini
13
config.ini
|
@ -5,7 +5,20 @@ MOSI = 10
|
|||
CS = 8
|
||||
|
||||
[button]
|
||||
# GPIO Pin
|
||||
BUTTON = 5
|
||||
|
||||
[voltage_sensor]
|
||||
# grove or arceli
|
||||
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
20
display.py
Normal 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))
|
137
raspicar.py
137
raspicar.py
|
@ -4,153 +4,58 @@ import board
|
|||
import configparser
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
from datetime import datetime
|
||||
from adafruit_ht16k33.segments import Seg7x4
|
||||
from sensors import Sensors
|
||||
from display import Display
|
||||
|
||||
config = configparser.RawConfigParser()
|
||||
config.read(r'./config.ini')
|
||||
|
||||
# Voltage Sersor
|
||||
VOLTAGE_SENSOR_MODEL = config.get('voltage_sensor', 'MODEL')
|
||||
|
||||
# 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)
|
||||
display = Display()
|
||||
sensor = Sensors(config)
|
||||
|
||||
|
||||
# Init program
|
||||
def init():
|
||||
# Set display
|
||||
display.brightness = 1
|
||||
display.blink_rate = 0 # Between 0 and 3
|
||||
# Button
|
||||
BUTTON = int(config.get('button', 'BUTTON'))
|
||||
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
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
|
||||
GPIO.add_event_detect(BUTTON, GPIO.FALLING, callback=handlePushButton, bouncetime=250)
|
||||
|
||||
pass
|
||||
|
||||
# Function called on push button (change sensor)
|
||||
def handlePushButton(button):
|
||||
channel = sensor.getChannel() + 1
|
||||
if (channel > 8):
|
||||
channel = 0
|
||||
|
||||
# Convert value to volt
|
||||
def convertToVoltage(value):
|
||||
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
|
||||
sensor.setChannel(channel)
|
||||
display.print(" :"+str(channel)+"-")
|
||||
|
||||
# On exit
|
||||
def handleExit():
|
||||
print("GoodBye!")
|
||||
display.print("--:--")
|
||||
time.sleep(2)
|
||||
display.clear()
|
||||
|
||||
# Main programm
|
||||
def main():
|
||||
init()
|
||||
time.sleep(2)
|
||||
print("Let's go!")
|
||||
|
||||
while True:
|
||||
if ( CHANNEL == 8 ): # Last sensor => clock mode
|
||||
now = datetime.now()
|
||||
printableValue = now.strftime("%H:%M")
|
||||
else:
|
||||
# Get current sensor value
|
||||
ad_value = readadc(CHANNEL, CLK, MOSI, MISO, CS)
|
||||
value = 0
|
||||
display.print(sensor.getValue())
|
||||
|
||||
# Convert value in readable Volt or Celcius
|
||||
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
|
||||
# Wait before next loop
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
atexit.register(handleExit)
|
||||
|
||||
if __name__ =='__main__':
|
||||
|
|
98
sensors.py
Normal file
98
sensors.py
Normal 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')
|
Loading…
Reference in a new issue