Skip to content

Instantly share code, notes, and snippets.

@oschettler
Created September 12, 2020 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oschettler/4b3c4227095467a40ac0a456e9cee1c4 to your computer and use it in GitHub Desktop.
Save oschettler/4b3c4227095467a40ac0a456e9cee1c4 to your computer and use it in GitHub Desktop.
#!env python3
import time
import CHIP_IO.GPIO as GPIO
import CHIP_IO.LRADC as ADC
pin = 'XIO-P0'
#pin = 'U14_13'
#pin = 'CSID0'
#GPIO.toggle_debug()
#ADC.toggle_debug()
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Allowed:
#ADC.set_sample_rate(125)
try:
active = False
t = 0
with open("sound.log", "a+") as f:
while True:
#print("Scale", ADC.get_scale_factor())
#print("Allowed sample rates", ADC.get_allowable_sample_rates())
#print("Sample rate", ADC.get_sample_rate())
value = -1
try:
#print("ADC", ADC.get_chan0_raw(), '->', ADC.get_chan0())
value = ADC.get_chan0()
except (TimeoutError):
print("Timeout ERROR")
ts = time.strftime('%Y-%m-%d %H:%M:%S')
if GPIO.input(pin):
if not active:
f.write('[{ts}] START\n'.format(ts=ts))
active = True
t = 10
else:
if t > 0:
print("Active-"+str(t), value)
t -= 1
else:
if active:
f.write('[{ts}] END\n'.format(ts=ts))
active = False
f.flush()
time.sleep(0.1)
except (KeyboardInterrupt):
print("Cleaning up")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment