python is driving me bonkers seems like the list is mucking me about

This commit is contained in:
Robin Clark 2022-07-16 21:55:29 +01:00
parent 12846d5a8a
commit 567ece4ccc

146
main.py
View File

@ -4,7 +4,7 @@ from PicoAirQuality import KitronikBME688, KitronikButton, KitronikDataLogger,
KitronikOLED, KitronikRTC, KitronikZIPLEDs,\ KitronikOLED, KitronikRTC, KitronikZIPLEDs,\
KitronikBuzzer KitronikBuzzer
import time import time
from array import array
#from datetime import datetime #from datetime import datetime
logFileName = "atmosphere_log.txt" logFileName = "atmosphere_log.txt"
@ -27,20 +27,56 @@ sec = 0
beeper = KitronikBuzzer() beeper = KitronikBuzzer()
buttons = KitronikButton() buttons = KitronikButton()
def ButtonB_r_IRQHandler(pin): # make this array size 20. A morse char of 5 dots|dashes
global buttonB # would take up 10 slots max.
buttonB = 2*60 #
print("button B pressed", buttonB)
class buttonEvent:
mstimestamp = 0
pinstate = 0
def __init__(self, mststamp, pinst):
buttonEvent.mstimestamp = mststamp
buttonEvent.pinstate = pinst
#def str(self):
#return "mstimestamp:",str(mstimestamp), "pinst ",str(pinstate)
mmaxindex = 20
mbuf = [] # array(buttonEvent)
mindex = 0 # head new button events
mindex_t = 0 # tail unprocessed button events
mcount = 0
old_m_count = 0
last_pv = 0
#def ButtonB_r_IRQHandler(pin):
# global buttonB
# buttonB = 2*60
# print("button B pressed", buttonB)
def ButtonB_f_IRQHandler(pin): def ButtonB_f_IRQHandler(pin):
#now = time.now() #now = time.now()
global buttonB global buttonB, last_pv
buttonB = 2*60 global mbuff, mindex, mcount, mmaxindex
print("button B released", buttonB, pin.value(),time.ticks_ms()) pv = pin.value()
if pin.value(): buttonB = 2*60 # maKES leds LIGHT IN AIRq
print("button B IRQ", time.ticks_ms(), pv)
if last_pv == 0 and pin.value() == 0: # get get spurious 0 level ints
print("two zero ints low level in a row") # ignore
else:
bollocks=buttonEvent(time.ticks_ms(),pv)
mbuf.insert(mindex,bollocks)
print ("bollocks", str(bollocks.mstimestamp), str(bollocks.pinstate)) # arghhh why is it not storing thsi
print ("mbuf inserting ",str( time.ticks_ms()), str(pv), "@", str(mindex))
b3 = mbuf[mindex]
print ("did shit bag python actually put this in the list??", b3.pinstate, b3)
# it would appear the shitbag python did store this
mcount += 1
mindex += 1
mindex = mindex % mmaxindex
if pv:
beeper.playTone(600) beeper.playTone(600)
else: else:
beeper.stopTone() beeper.stopTone()
last_pv = pv
# FALLING DOES NOT WORK AND IRQ_LOW_LEVEL not recognised # FALLING DOES NOT WORK AND IRQ_LOW_LEVEL not recognised
@ -48,68 +84,38 @@ buttons.buttonB.irq(trigger=machine.Pin.IRQ_FALLING|machine.Pin.IRQ_RISING, hand
#buttons.buttonB.irq(trigger=machine.Pin.IRQ_RISING, handler=ButtonB_r_IRQHandler) #buttons.buttonB.irq(trigger=machine.Pin.IRQ_RISING, handler=ButtonB_r_IRQHandler)
while True: while True:
#global old_m_count
if sec == 0:
f = open(logFileName, 'a+')
f.write("# Data Logging: Date, Time, Temperature, Pressure, Humidity, AirQuality, CO2\n")
f.write('# Restarted ' + str(rtc.readDateString()) + ' ' + str(rtc.readTimeString()) + '\n' )
time.sleep_ms(1000)
bme688.measureData()
oled.clear() oled.clear()
#oled.drawRect(4, 5, 120, 35)
oled.displayText(rtc.readDateString(), 1, 25) oled.displayText(rtc.readDateString(), 1, 25)
oled.displayText(rtc.readTimeString(), 2, 33) oled.displayText(rtc.readTimeString(), 2, 33)
#oled.drawLine(0, 48, 127, 48) oled.displayText(str(mcount), 4, 33)
#oled.drawLine(0, 49, 127, 49) # process the list
oled.displayText(str(bme688.readTemperature()) + " oC", 3, 0) if (mcount - old_m_count) >= 2:
oled.displayText(str(bme688.readHumidity()) + " %H", 3, 80) print(" old_m_count != mcount >= 2")
eCO2 = bme688.readeCO2() b1 = mbuf[(mindex_t+mmaxindex)%mmaxindex]
oled.displayText(str(eCO2) + " CO2 ppm", 4, 0) b2 = mbuf[(mindex_t+1)%mmaxindex]
IAQ = bme688.getAirQualityScore() # ok shitbag python why is the pinstate in that object now zero you turd
oled.displayText(str(IAQ) + " AQ ", 5, 0) #print("b1 index", str((mindex_t+mmaxindex)%mmaxindex), "b1 ps", str(b1.pinstate))
mbar = bme688.readPressure()/100.0 #print("b2 index", str((mindex_t+mmaxindex+1)%mmaxindex), "b2 ps ", str(b2.pinstate))
oled.displayText(str(mbar) + " mB ", 5, 50) print("b1", b1, b1.pinstate)
print("b2", b2, b2.pinstate)
print(" b1 ps, b2 ps", b1.pinstate, b2.pinstate)
if b1.pinstate == 1 and b2.pinstate == 0:
print(" high to low pinstate valid press", b1.pinstate, b2.pinstate)
# press and release found
duration = b2.mstimestamp - b1.mstimestamp
if duration < 300:
oled.displayText('dit '+str(duration), 5, 33)
else:
oled.displayText('dah '+str(duration), 5, 33)
mindex_t += 2;
mindex_t = mindex_t % mmaxindex
mcount -= 2
else:
print(" undefined pinstate order", b1.pinstate, b2.pinstate)
mindex_t += 1 # that one made no sense so move up
mindex_t = mindex_t % mmaxindex
mcount -= 1
oled.displayText('BAH ', 5, 33)
old_m_count = mcount
oled.show() oled.show()
if (IAQ < 100):
zipleds.setLED(0, zipleds.GREEN)
else:
zipleds.setLED(0, zipleds.RED)
if (eCO2 < 800):
zipleds.setLED(2, zipleds.GREEN)
else:
zipleds.setLED(2, zipleds.RED)
zipleds.setBrightness(10)
# ffs all I want is an action every minute I hate python scope shit
#now = rtc.readTimeString();
# log every minute, thats 1440 records a day
sec = sec + 1
secmod = sec % 29
if (secmod) == 0:
#log.setupDataFields("Date", "Time", "Temperature", "Pressure", "Humidity", "IAQ", "eCO2")
log_string = (rtc.readDateString() + ',' + \
rtc.readTimeString() + ',' + \
str(bme688.readTemperature()) + ',' + \
str(bme688.readPressure()) + ',' + \
str(bme688.readHumidity())+ ',' + \
str(bme688.getAirQualityScore()) + ',' + \
str(bme688.readeCO2())) + '\n'
#print (log_string)
#print ("logging data\n")
f.write(log_string)
f.flush()
zipleds.setLED(1, zipleds.RED)
elif (secmod < 10):
zipleds.setLED(1, zipleds.BLUE)
elif (secmod > 10):
s2 = secmod * 2
zip_col = (secmod, s2, s2)
zipleds.setLED(1, zip_col)
if ( (sec+1) % 2 ) == 0:
zipleds.setLED(1, zipleds.BLACK)
zipleds.show()