diff --git a/gex/units/DIn.py b/gex/units/DIn.py index d8c09d3..accbbee 100644 --- a/gex/units/DIn.py +++ b/gex/units/DIn.py @@ -46,11 +46,19 @@ class DIn(gex.Unit): def on_trigger(self, sensitive_pins, callback): """ Assign a trigger callback. + Pins are passed as a list of indices (packed), or a bitmap Arguments are: pins snapshot, timestamp """ - for i in range(0, 16): - if sensitive_pins & (1 << i): - self.handlers[i] = callback + + 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 def _on_event(self, evt:EventReport): if evt.code == 0x00: diff --git a/gex/units/Neopixel.py b/gex/units/Neopixel.py index f49918b..6264a80 100644 --- a/gex/units/Neopixel.py +++ b/gex/units/Neopixel.py @@ -6,7 +6,7 @@ class Neopixel(gex.Unit): """ def _type(self): - return 'NEOPIXEL' + return 'NPX' def get_len(self): """ Get the neopixel strip length """ diff --git a/main.py b/main.py index 58bd8f8..73c3f72 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,11 @@ transport = gex.TrxRawUSB(sn='0029002F-42365711-32353530') #transport = gex.TrxSerialSync(port='/dev/ttyACM0') with gex.Client(transport) as client: + # + # if True: + # s = client.ini_read() + # print(s) + # client.ini_write(s) if False: adc = gex.ADC(client, 'adc') @@ -28,15 +33,24 @@ with gex.Client(transport) as client: if True: adc = gex.ADC(client, 'adc') - adc.set_active_channels([16]) - fs = adc.set_sample_rate(30000) + adc.set_active_channels([1]) + fs = adc.set_sample_rate(1000) + + data = adc.capture(1000) + + if data is not None: + 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(1000) - print("sr = %d" % r) - std = np.std(data) - print(std) + # 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) # diff --git a/pymodoro.py b/pymodoro.py new file mode 100644 index 0000000..3e259b4 --- /dev/null +++ b/pymodoro.py @@ -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()