def test(log_level=logging.DEBUG): """Test the Ntc class returned by the analog_in function.""" logging.basicConfig(level=log_level) hardware_config = Config('hardware_config.json') t_kettle = analog_in.analog_in('kettle temperature', '°C', hardware_config=hardware_config) t_fridge = analog_in.analog_in('fridge temperature', '°C', hardware_config=hardware_config) t_spare = analog_in.analog_in('_spare.temperature', '°C', hardware_config=hardware_config) while True: result = list() start = time.ticks_cpu() # pylint: disable=no-member for temperature in (t_fridge, t_kettle, t_spare): try: result.append('%s: %.2f' % (temperature.device_name, temperature.get())) except ValueError: result.append('%s: #NA' % (temperature.device_name, )) duration = time.ticks_diff(time.ticks_cpu(), start) # pylint: disable=no-member print(duration, 'ticks\t', '\t'.join(result)) time.sleep(1)
def main(): '''Main Event Loop''' global interruptCounter, totalInterrupts, CUR_FORMULA, READY, GPIO_ODR, COL_INDEX, COL_TIME # PINS REGISTERS GPIO_REG = const(0x3ff44000) GPIO_EN = const(0x8) GPIO_CLR = const(0xC) BIT21 = const(1 << 21) # 2097152 BIT14 = const(1 << 14) # 16384 BIT26 = const(1 << 26) # 67108864 BIT15 = const(1 << 15) # 32768 BIT25 = const(1 << 25) # 33554432 BIT27 = const(1 << 27) # 134217728 BIT12 = const(1 << 12) # 4096 BIT13 = const(1 << 13) # 8192 LEDS = [BIT21, BIT14, BIT26, BIT15, BIT25, BIT27, BIT12, BIT13] _LED_PINS = [21, 14, 26, 15, 25, 27, 12, 13] LED_PINS = [Pin(i, Pin.OUT, value=0) for i in _LED_PINS] ALL_LEDS = const(237039616) LED_COUNT = const(8) GPIO_ODR = { "REG": GPIO_REG, "EN": GPIO_EN, "CLR": GPIO_CLR, "ALL_LEDS": ALL_LEDS, "LED_COUNT": LED_COUNT } # Last Revolution Time LAST_REV = 0 print("POVPi Ready") display_status(9) # Run Blynk in Thread thread.stack_size(5 * 1024) thread.start_new_thread(run_blynk, ()) # Startup Shadow startup_shadow = {"display": "_LINE_", "enabled": True} update_shadow(new_state=startup_shadow) gc.collect() while 1: # Handle Interrupts if interruptCounter > 0: state = machine.disable_irq() interruptCounter -= 1 machine.enable_irq(state) time_delta = time.ticks_diff(time.ticks_cpu(), LAST_REV) COL_TIME = int(time_delta / 360) COL_INDEX = 0 LAST_REV = time.ticks_cpu() totalInterrupts += 1 if READY and CUR_FORMULA: if LAST_REV == 0: LAST_REV = time.ticks_cpu() if COL_INDEX < 90: byte = CUR_FORMULA[COL_INDEX] COL_INDEX = display(byte, COL_TIME, COL_INDEX, GPIO_REG, GPIO_EN, GPIO_CLR, ALL_LEDS)
def tick(timer): global np, anim_buffer, anim_index, anim_buffer_len, last_tick # if time.ticks_cpu() < last_tick: # print( "[tick] cpu tick < last" ) # return np.buf = anim_buffer[anim_index] np.write() if anim_index < (anim_buffer_len - 1): anim_index += 1 else: anim_index = 0 print( "[t] %i" % time.ticks_diff(time.ticks_cpu(), last_tick) ) last_tick = time.ticks_cpu()
def irqCallback(pin): global state, time1, time2, time3, pwmValue if state == 0 and not(skip): state = 1 time1 = time.ticks_cpu() pin.irq(trigger = Pin.IRQ_FALLING, handler=irqCallback) elif state == 1 and not(skip): state = 2 time2 = time.ticks_cpu() pin.irq(trigger = Pin.IRQ_RISING, handler=irqCallback) elif not(skip): pin.irq(trigger = 0) state = 0 time3 = time.ticks_cpu() pwmValue = int(((time2-time1)/(time3-time1))*4095) #255 for arduino, 4096 for hat
def display(byte, col_time, COL_INDEX, GPIO_REG, GPIO_EN, GPIO_CLR, ALL_LEDS): '''Displays Text on POVPi''' if COL_INDEX < 90: cleared = ALL_LEDS - byte col_timeout = time.ticks_add(time.ticks_cpu(), col_time * 4) if byte == 0: machine.mem32[GPIO_REG + GPIO_CLR] ^= ALL_LEDS else: machine.mem32[GPIO_REG + GPIO_CLR] ^= cleared machine.mem32[GPIO_REG + GPIO_EN] ^= byte while time.ticks_diff(col_timeout, time.ticks_cpu()) >= 0: pass machine.mem32[GPIO_REG + GPIO_CLR] ^= ALL_LEDS return COL_INDEX + 1 else: machine.mem32[GPIO_REG + GPIO_CLR] ^= ALL_LEDS return COL_INDEX
def feed_touch(): point = lv.point_t() indev = lv.indev_get_act() lv.indev_get_point(indev, point) # now we can take bytes([point.x % 256, point.y % 256]) # and feed it into hash digest t = time.ticks_cpu() random_data = t.to_bytes(4,'big') + bytes([point.x % 256, point.y % 256]) rng.feed(random_data)
def trig_falling_or_rising(self, pin): if (len(self.times) > self.maxlen): self.times.popleft() self.times.append((self.pin.value(), time.ticks_cpu())) if self.pin.value() == 1: self.start_falling = time.ticks_cpu() elif self.start_falling is not 0: self.result_falling = time.ticks_diff(self.start_falling, time.ticks_cpu()) # print("falling time: " + str(self.result_falling)) self.start_falling = 0 if self.pin.value() == 0: self.start_rising = time.ticks_cpu() elif self.start_rising is not 0: self.result_rising = time.ticks_diff(self.start_rising, time.ticks_cpu()) # print("rising time: " + str(self.result_rising)) self.start_rising = 0
def graph(): random.seed(time.ticks_cpu() * time.ticks_us()) graph = Graph(display, 30, 24, x=10, y=50, bg=0, fg=colors.RGB_YELLOW) graph.point(10) for i in range(50): v = random.randint(0, 10) graph.point(v) graph.paint() # time.sleep(0.1) graph.free_fb() gc.collect()
def feed_touch(): """ Gets a point from the touchscreen and feeds it to random number pool """ point = lv.point_t() indev = lv.indev_get_act() lv.indev_get_point(indev, point) # now we can take bytes([point.x % 256, point.y % 256]) # and feed it into hash digest t = time.ticks_cpu() random_data = t.to_bytes(4, 'big') + bytes([point.x % 256, point.y % 256]) rng.feed(random_data)
SHUNT_OHMS = 0.1 def read_solar(): try: solar_voltage = str(solar_ina.voltage()) solar_current = str(solar_ina.current()) print_solar = "Solar:" + time_snapshot + ":BusVolt:" + solar_voltage + "V:Current:" + solar_current + "mA:#" except: print_solar = "Solar:" + time_snapshot + ":BusVolt:INVALID:Current:INVALID:#" print("INA219:0x45: Solar read failed...") try: print(TARGET_64BIT_ADDR, print_solar) except: print("XBee: TX Solar Failed...") solar_ina = INA219(SHUNT_OHMS, i2c, SOLAR_INA219A_ADDR) try: print("INA219:0x45: Configuring solar...") solar_ina.configure_32v_2a() except: print("INA219:0x45: Solar Missing...") while True: time_snapshot = str(time.ticks_cpu()) read_solar() time.sleep(1)
spot_test(0, (2000, 1, 1, 0, 0, 0, 5, 1)) spot_test(1, (2000, 1, 1, 0, 0, 1, 5, 1)) spot_test(59, (2000, 1, 1, 0, 0, 59, 5, 1)) spot_test(60, (2000, 1, 1, 0, 1, 0, 5, 1)) spot_test(3599, (2000, 1, 1, 0, 59, 59, 5, 1)) spot_test(3600, (2000, 1, 1, 1, 0, 0, 5, 1)) spot_test(-1, (1999, 12, 31, 23, 59, 59, 4, 365)) spot_test(447549467, (2014, 3, 7, 23, 17, 47, 4, 66)) spot_test(-940984933, (1970, 3, 7, 23, 17, 47, 5, 66)) spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1)) spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1)) spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365)) # fmt: on t1 = time.time() time.sleep(2) t2 = time.time() print(abs(time.ticks_diff(t1, t2) - 2) <= 1) t1 = time.ticks_ms() time.sleep_ms(50) t2 = time.ticks_ms() print(abs(time.ticks_diff(t1, t2) - 50) <= 1) t1 = time.ticks_us() time.sleep_us(1000) t2 = time.ticks_us() print(time.ticks_diff(t1, t2) < 1500) print(time.ticks_diff(time.ticks_cpu(), time.ticks_cpu()) < 16384)
spot_test(1, (2000, 1, 1, 0, 0, 1, 5, 1)) spot_test(59, (2000, 1, 1, 0, 0, 59, 5, 1)) spot_test(60, (2000, 1, 1, 0, 1, 0, 5, 1)) spot_test(3599, (2000, 1, 1, 0, 59, 59, 5, 1)) spot_test(3600, (2000, 1, 1, 1, 0, 0, 5, 1)) spot_test(-1, (1999, 12, 31, 23, 59, 59, 4, 365)) spot_test(447549467, (2014, 3, 7, 23, 17, 47, 4, 66)) spot_test(-940984933, (1970, 3, 7, 23, 17, 47, 5, 66)) spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1)) spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1)) spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365)) t1 = time.time() time.sleep(2) t2 = time.time() print(abs(time.ticks_diff(t1, t2) - 2) <= 1) t1 = time.ticks_ms() time.sleep_ms(50) t2 = time.ticks_ms() print(abs(time.ticks_diff(t1, t2) - 50) <= 1) t1 = time.ticks_us() time.sleep_us(1000) t2 = time.ticks_us() print(time.ticks_diff(t1, t2) < 2000) t1 = time.ticks_cpu() t2 = time.ticks_cpu() print(time.ticks_diff(t1, t2) < 16384)
width=320, height=240) # turn on the backlight bl = Pin(5, Pin.OUT) bl.value(0) # fill the screen with some colored boxes (display.fill(colors.WHITE)) # .rect(20, 20, 90, 90, RED) # .rect(200, 20, 90, 90, GREEN) # .rect(114, 130, 90, 90, BLUE) # .text("test 1 2 3", 125, 125, BLACK)) random.seed(time.ticks_cpu() * time.ticks_us()) def graph(): graph = Graph(display, 30, 8, x=10, y=150) graph.point(10) for i in range(50): v = random.randint(0, 10) graph.point(v) graph.paint() # time.sleep(0.1) graph.free_fb() gc.collect()
def fill_buffer(): global anim_buffer, anim_buffer_len for cycle in range(256): buf = bytearray(N_PIXELS * N_COLOURS) for pixel in range(0, N_PIXELS): hue = cycle + (pixel * 255 / N_PIXELS) rgb = list(map(lambda c: float_to_byte(c), hsv_to_rgb(hue / 255, 1.0, 0.3))) offset = pixel * N_COLOURS for i in range(N_COLOURS): buf[offset + P_BUF_ORDER[i]] = rgb[i] anim_buffer.append(buf) anim_buffer_len = len(anim_buffer) anim_index = 0 last_tick = time.ticks_cpu() def tick(timer): global np, anim_buffer, anim_index, anim_buffer_len, last_tick # if time.ticks_cpu() < last_tick: # print( "[tick] cpu tick < last" ) # return np.buf = anim_buffer[anim_index] np.write() if anim_index < (anim_buffer_len - 1): anim_index += 1 else: anim_index = 0 print( "[t] %i" % time.ticks_diff(time.ticks_cpu(), last_tick) )
# pulse(True, LEDTimer, machine.Timer.PERIODIC, 1000, onboardLEDPulse) pulse(True, LEDTimer, machine.Timer.PERIODIC, 200, alertLEDPulse) # pulse(True, vibrationTimer, machine.Timer.PERIODIC, 2000, vibrationPulse) # pulse(True, buzzerTimer, machine.Timer.PERIODIC, 2, buzzerPulse) # onboardLEDTimer.init(period=1000, mode=machine.Timer.PERIODIC, callback=onboardLEDPulse) # vibrationTimer.init(period=2000, mode=machine.Timer.PERIODIC, callback=vibrationPulse) # buzzerTimer.init(period=2, mode=machine.Timer.PERIODIC, callback=buzzerPulse) # try: # SEED = SEED + time.ticks_us() # except: # SEED = time.ticks_us() # Init random SEED = time.ticks_cpu() * 183 print(SEED) print("Seed: " + str(SEED)) random.seed(SEED) # Generate ID ID = random.getrandbits(30) print(ID) defaultName = "catalyst-pager" NAME = defaultName + "_" + str(ID) print("Name: " + NAME) # MQTT # b"string" and bytes("string", "utf-8") are equivalent INIT_MSG = "initialize-pager" TOPIC_BASE = "catalyst-jukebox"
from machine import Pin from neopixel import NeoPixel import time number = 150 pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels np = NeoPixel(pin, number) # create NeoPixel driver on GPIO0 for 8 pixels reset = False r, g, b = 0, 0, 0 while True: for i in range(number): n = i if reset else 149-i if reset: r, g, b = 0, 0, 0 else: r, g, b = time.ticks_cpu() % 255, time.ticks_cpu() % 255, time.ticks_cpu() % 255; np[n] = (r, g, b) np.write() print(n, np[n]) time.sleep_ms(20) reset = not reset
test() spot_test( 0, (2000, 1, 1, 0, 0, 0, 5, 1)) spot_test( 1, (2000, 1, 1, 0, 0, 1, 5, 1)) spot_test( 59, (2000, 1, 1, 0, 0, 59, 5, 1)) spot_test( 60, (2000, 1, 1, 0, 1, 0, 5, 1)) spot_test( 3599, (2000, 1, 1, 0, 59, 59, 5, 1)) spot_test( 3600, (2000, 1, 1, 1, 0, 0, 5, 1)) spot_test( -1, (1999, 12, 31, 23, 59, 59, 4, 365)) spot_test( 447549467, (2014, 3, 7, 23, 17, 47, 4, 66)) spot_test( -940984933, (1970, 3, 7, 23, 17, 47, 5, 66)) spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1)) spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1)) spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365)) t1 = time.time() time.sleep(2) t2 = time.time() print(abs(time.ticks_diff(t1, t2) -2) <= 1) t1 = time.ticks_ms() time.sleep_ms(50) t2 = time.ticks_ms() print(abs(time.ticks_diff(t1, t2)- 50) <= 1) t1 = time.ticks_us() time.sleep_us(1000) t2 = time.ticks_us() print(time.ticks_diff(t1, t2) < 1500) print(time.ticks_diff(time.ticks_cpu(), time.ticks_cpu()) < 16384)
import blinkt from time import sleep, ticks_cpu from machine import reset import random import gc print("Press Ctrl+C to stop script...") # Random numbers are the same every reset (this appears to fix it) random.seed(ticks_cpu()) for x in range(0, random.randint(25, 75)): random.random() random.seed(ticks_cpu()) def eyes_forward(seconds, r, g, b): blinkt.clear() blinkt.set_pixel(1, r, g, b) blinkt.set_pixel(6, r, g, b) sleep(seconds) blinkt.clear() sleep(0.2) def eyes_left(seconds, r, g, b): blinkt.clear() blinkt.set_pixel(0, r, g, b) blinkt.set_pixel(5, r, g, b) sleep(seconds) blinkt.clear() sleep(0.2)
spot_test( 1, (2000, 1, 1, 0, 0, 1, 5, 1)) spot_test( 59, (2000, 1, 1, 0, 0, 59, 5, 1)) spot_test( 60, (2000, 1, 1, 0, 1, 0, 5, 1)) spot_test( 3599, (2000, 1, 1, 0, 59, 59, 5, 1)) spot_test( 3600, (2000, 1, 1, 1, 0, 0, 5, 1)) spot_test( -1, (1999, 12, 31, 23, 59, 59, 4, 365)) spot_test( 447549467, (2014, 3, 7, 23, 17, 47, 4, 66)) spot_test( -940984933, (1970, 3, 7, 23, 17, 47, 5, 66)) spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1)) spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1)) spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365)) t1 = time.time() time.sleep(2) t2 = time.time() print(time.ticks_diff(t1, t2) == 2) t1 = time.ticks_ms() time.sleep_ms(50) t2 = time.ticks_ms() print(time.ticks_diff(t1, t2) == 50) t1 = time.ticks_us() time.sleep_us(1000) t2 = time.ticks_us() print(time.ticks_diff(t1, t2) < 2000) t1 = time.ticks_cpu() t2 = time.ticks_cpu() print(time.ticks_diff(t1, t2) < 16384)