some improvements in DIn

doublebuf
Ondřej Hruška 7 years ago
parent 018f993154
commit adee8b7b0f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 12
      gex/units/DIn.py
  2. 2
      gex/units/Neopixel.py
  3. 28
      main.py
  4. 92
      pymodoro.py

@ -46,10 +46,18 @@ class DIn(gex.Unit):
def on_trigger(self, sensitive_pins, callback): def on_trigger(self, sensitive_pins, callback):
""" """
Assign a trigger callback. Assign a trigger callback.
Pins are passed as a list of indices (packed), or a bitmap
Arguments are: pins snapshot, timestamp Arguments are: pins snapshot, timestamp
""" """
for i in range(0, 16):
if sensitive_pins & (1 << i): if type(sensitive_pins) == int:
L = []
for i in range(0,16):
if sensitive_pins & (1 << i) != 0:
L.append(i)
sensitive_pins = L
for i in sensitive_pins:
self.handlers[i] = callback self.handlers[i] = callback
def _on_event(self, evt:EventReport): def _on_event(self, evt:EventReport):

@ -6,7 +6,7 @@ class Neopixel(gex.Unit):
""" """
def _type(self): def _type(self):
return 'NEOPIXEL' return 'NPX'
def get_len(self): def get_len(self):
""" Get the neopixel strip length """ """ Get the neopixel strip length """

@ -10,6 +10,11 @@ transport = gex.TrxRawUSB(sn='0029002F-42365711-32353530')
#transport = gex.TrxSerialSync(port='/dev/ttyACM0') #transport = gex.TrxSerialSync(port='/dev/ttyACM0')
with gex.Client(transport) as client: with gex.Client(transport) as client:
#
# if True:
# s = client.ini_read()
# print(s)
# client.ini_write(s)
if False: if False:
adc = gex.ADC(client, 'adc') adc = gex.ADC(client, 'adc')
@ -28,15 +33,24 @@ with gex.Client(transport) as client:
if True: if True:
adc = gex.ADC(client, 'adc') adc = gex.ADC(client, 'adc')
adc.set_active_channels([16]) adc.set_active_channels([1])
fs = adc.set_sample_rate(30000) fs = adc.set_sample_rate(1000)
for r in range(0,8):
adc.set_sample_time(r)
data = adc.capture(1000) data = adc.capture(1000)
print("sr = %d" % r)
std = np.std(data) if data is not None:
print(std) plt.plot(data, 'r-', lw=1)
plt.show()
else:
print("Nothing rx")
# for r in range(0,8):
# adc.set_sample_time(r)
# data = adc.capture(10000)
# print("sr = %d" % r)
# std = np.std(data)
# print(std)
# #

@ -0,0 +1,92 @@
import time
import gex
WK_TIME = 25
BK_TIME = 5
LIGHT_CNT = 30
PH_BREAK = 'Break'
PH_BREAK_OVER = 'BreakOver'
PH_WORK = 'Work'
PH_WORK_OVER = 'WorkOver'
class Pymodoro:
def __init__(self):
self.phase = PH_BREAK_OVER
self.work_s = 0
self.break_s = 0
self.colors = [0x000000 for _ in range(0, LIGHT_CNT)]
client = gex.Client(gex.TrxRawUSB())
self.btn = gex.DIn(client, 'btn')
self.neo = gex.Neopixel(client, 'neo')
self.btn.on_trigger([0], self.on_btn)
self.switch(PH_BREAK_OVER)
self.display()
def display(self):
self.neo.load(self.colors)
def on_btn(self, snapshot, timestamp):
if self.phase == PH_BREAK_OVER:
self.switch(PH_WORK)
elif self.phase == PH_WORK:
self.switch(PH_WORK) # restart
elif self.phase == PH_WORK_OVER:
self.switch(PH_BREAK)
def switch(self, phase):
print("Switch to %s" % phase)
if phase == PH_BREAK:
self.colors = [0x009900 for _ in range(0, LIGHT_CNT)]
self.break_s = BK_TIME * 60
elif phase == PH_BREAK_OVER:
self.colors = [0x662200 for _ in range(0, LIGHT_CNT)]
elif phase == PH_WORK:
self.colors = [0x990000 for _ in range(0, LIGHT_CNT)]
self.work_s = WK_TIME * 60
elif phase == PH_WORK_OVER:
self.colors = [0x113300 for _ in range(0, LIGHT_CNT)]
self.phase = phase
def extinguish(self, dark, total):
per_light = total / LIGHT_CNT
lights = int((dark + per_light / 2) / per_light)
for n in range(0, LIGHT_CNT - lights):
self.colors[n] = 0x000000
def tick(self):
if self.phase == PH_BREAK:
self.break_s -= 1
print("Break remain: %d s" % self.break_s)
self.extinguish(self.break_s, BK_TIME * 60)
if self.break_s == 0:
self.switch(PH_BREAK_OVER)
elif self.phase == PH_WORK:
self.work_s -= 1
print("Work remain: %d s" % self.work_s)
self.extinguish(self.work_s, WK_TIME * 60)
if self.work_s == 0:
self.switch(PH_WORK_OVER)
self.display()
def run(self):
while True:
time.sleep(1)
self.tick()
a = Pymodoro()
a.run()
Loading…
Cancel
Save