def test(duration=0): # responds to switches if duration: print("Tests pushbuttons for {:5d} seconds".format(duration)) else: print("Tests pushbuttons") objSched = Sched() btn1 = Pin(14, Pin.IN, Pin.PULL_UP) btn2 = Pin(12, Pin.IN, Pin.PULL_UP) Pushbutton(objSched, btn1, descriptor, false_func=x5print, false_func_args=("Red", )) # X5 triggers on open Pushbutton(objSched, btn2, descriptor, true_func=x6print, true_func_args=("Yellow", ), long_func=yellowlong, long_func_args=("Long press", ), double_func=yellowdbl, double_func_args=("Double click", )) # X6 triggers on close if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(duration = 0): if duration: print("Test LCD display for {:3d} seconds".format(duration)) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols = 16) objSched.add_thread(lcd_thread(lcd0)) if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(duration=0): if duration: print("Test LCD display for {:3d} seconds".format(duration)) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols=16) objSched.add_thread(lcd_thread(lcd0)) if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(duration = 0): if duration: print("Flash LED's for {:3d} seconds".format(duration)) leds = [pyb.LED(x) for x in range(1,5)] # Initialise all four on board LED's objSched = Sched() # Instantiate the scheduler for x in range(4): # Create a thread instance for each LED objSched.add_thread(toggle(leds[x], 0.2 + x/2)) if duration: objSched.add_thread(stop(duration, objSched)) # Commit suicide after specified no. of seconds objSched.run() # Run it!
def test(duration = 0): if duration: print("Output accelerometer values for {:3d} seconds".format(duration)) else: print("Output accelerometer values") objSched = Sched() objSched.add_thread(accelthread()) if duration: objSched.add_thread(stop(duration, objSched)) # Run for a period then stop objSched.run()
def test(): mtx = Pin(14, Pin.OUT) # Define passive pins mckout = Pin(15, Pin.OUT, value = 0) # clocks must be initialised to zero. Pin 15 has pulldown mrx = Pin(13, Pin.IN) mckin = Pin(12, Pin.IN) # add 10K pulldown objsched = Sched() with SynCom(objsched, True, mckin, mckout, mrx, mtx) as channel: objsched.add_thread(passive_thread(channel)) objsched.run()
def test(): stx = Pin(Pin.board.Y5, Pin.OUT_PP) # Define pins sckout = Pin(Pin.board.Y6, Pin.OUT_PP) sckout.value(0) # Don't assert clock until data is set srx = Pin(Pin.board.Y7, Pin.IN) sckin = Pin(Pin.board.Y8, Pin.IN) objsched = Sched(heartbeat = 1) with SynCom(objsched, False, sckin, sckout, srx, stx) as channel: objsched.add_thread(initiator_thread(channel)) objsched.run()
def test(duration = 0): objSched = Sched() objSched.add_thread(robin("Thread 1")) th2 = objSched.add_thread(robin("Thread 2")) objSched.add_thread(robin("Thread 3")) objSched.add_thread(pauser(objSched, th2)) if duration: objSched.add_thread(stop(duration, objSched)) # Kill after a period objSched.run()
def test(duration=0): if duration: print("Flash LED's for {:3d} seconds".format(duration)) leds = [pyb.LED(x) for x in range(1, 5)] # Initialise all four on board LED's objSched = Sched() # Instantiate the scheduler for x in range(4): # Create a thread instance for each LED objSched.add_thread(toggle(leds[x], 0.2 + x / 2)) if duration: objSched.add_thread( stop(duration, objSched)) # Commit suicide after specified no. of seconds objSched.run() # Run it!
def test(duration=0): if duration: print("Flash LED's for {:3d} seconds".format(duration)) print('Interrupt with ctrl-C or wait for deliberate error') print('Expect the usual error dump. But should see four exit lines first.') leds = [pyb.LED(x) for x in range(1, 5)] # Initialise all four on board LED's objSched = Sched() # Instantiate the scheduler for x in range(4): # Create a thread instance for each LED objSched.add_thread(toggle(leds[x], 0.2 + x / 2, x)) if duration: objSched.add_thread( stop(duration, objSched)) # Commit suicide after specified no. of seconds objSched.run() # Run it!
def test(duration = 0): # responds to switches if duration: print("Tests pushbuttons for {:5d} seconds".format(duration)) else: print("Tests pushbuttons") objSched = Sched() Pushbutton(objSched, 'X5', descriptor, false_func = x5print, false_func_args = ("Red",)) # X5 triggers on open Pushbutton(objSched, 'X6', descriptor, true_func = x6print, true_func_args = ("Yellow",), long_func = yellowlong, long_func_args = ("Long press",), double_func = yellowdbl, double_func_args = ("Double click",)) # X6 triggers on close if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(): freq(160000000) mtx = Pin(14, Pin.OUT) # Define pins mckout = Pin(15, Pin.OUT, value=0) # clocks must be initialised to zero. mrx = Pin(13, Pin.IN) mckin = Pin(12, Pin.IN) objsched = Sched(heartbeat=1) channel = SynCom(objsched, True, mckin, mckout, mrx, mtx) channel.start() objsched.add_thread(passive_thread(channel)) try: objsched.run() finally: mckout(0)
def test(duration): assert duration >= 1, 'Duration must be at least one second' print('Running test for {} seconds'.format(duration)) objSched = Sched() leds = [pyb.LED(x) for x in range(1,5)] # Initialise all four on board LED's for x in range(4): # Create a thread instance for each LED objSched.add_thread(toggle(leds[x], 0.2 + x/2)) # objSched.add_thread(robin("Thread 1")) # Instantiate a few threads # objSched.add_thread(robin("Thread 2")) # objSched.add_thread(robin("Thread 3")) lstResult = [0, 0] objSched.add_thread(thr_instrument(objSched, lstResult)) objSched.add_thread(instrument(objSched, 0.1, duration)) objSched.run() print("Maximum roundrobin yield delay was {:6.1f}mS".format(lstResult[0]/1000.0)) print("Thread thr_instrument was executed {:3d} times in {:3d} seconds {:5d}Hz".format(lstResult[1], duration, lstResult[1]//duration))
def test(): stx = Pin(Pin.board.Y5, Pin.OUT_PP) # Define pins sckout = Pin(Pin.board.Y6, Pin.OUT_PP) sckout.value(0) # Don't assert clock until data is set srx = Pin(Pin.board.Y7, Pin.IN) sckin = Pin(Pin.board.Y8, Pin.IN) reset = Pin(Pin.board.Y4, Pin.OPEN_DRAIN) objsched = Sched(heartbeat=1) channel = SynCom(objsched, False, sckin, sckout, srx, stx) channel.start(reset, 0) objsched.add_thread(initiator_thread(channel)) try: objsched.run() finally: sckout.value(0)
def test(): # setup uart object to pass # output = pyb.UART(1, 4800) # generate objects OB = output_buffer() compassthread = compass.cthread(OB) # mpu_i2c = compass.i2c_object pyb.delay(100) othread = outputthread(OB)#, test=True) objSched=Sched() # objSched.add_thread(seatalkthread(OB)) objSched.add_thread(compassthread) # objSched.add_thread(NASAWindThread(OB)) # objSched.add_thread(NASADepthThread(OB)) # pyb.delay(100) # objSched.add_thread(barometerthread(OB))#i2c_object=mpu_i2c)) objSched.add_thread(othread) objSched.run()
def test(duration = 0): objSched = Sched() objSched.add_thread(robin("Thread 1")) # Instantiate a few threads objSched.add_thread(robin("Thread 2")) objSched.add_thread(robin("Thread 3")) lstResult = [0, 0] objSched.add_thread(thr_instrument(objSched, lstResult)) if duration: objSched.add_thread(stop(duration, objSched)) # Run for a period then stop objSched.run() print("Maximum delay was {:6.1f}mS".format(lstResult[0]/1000.0)) print("Thread was executed {:3d} times in {:3d} seconds".format(lstResult[1], duration))
def run(): OB = output_buffer() compassthread = compass.cthread(OB) pyb.delay(100) othread = outputthread(OB) objSched = Sched() objSched.add_thread(seatalkthread(OB)) objSched.add_thread(compassthread) pyb.delay(100) objSched.add_thread(barometerthread(OB)) objSched.add_thread(othread) objSched.run()
def test(duration): assert duration >= 1, 'Duration must be at least one second' print('Running test for {} seconds'.format(duration)) objSched = Sched() leds = [pyb.LED(x) for x in range(1, 5)] # Initialise all four on board LED's for x in range(4): # Create a thread instance for each LED objSched.add_thread(toggle(leds[x], 0.2 + x / 2)) # objSched.add_thread(robin("Thread 1")) # Instantiate a few threads # objSched.add_thread(robin("Thread 2")) # objSched.add_thread(robin("Thread 3")) lstResult = [0, 0] objSched.add_thread(thr_instrument(objSched, lstResult)) objSched.add_thread(instrument(objSched, 0.1, duration)) objSched.run() print("Maximum roundrobin yield delay was {:6.1f}mS".format(lstResult[0] / 1000.0)) print( "Thread thr_instrument was executed {:3d} times in {:3d} seconds {:5d}Hz" .format(lstResult[1], duration, lstResult[1] // duration))
def test(duration=0): objSched = Sched(True, 1) # heartbeat on LED 1 objSched.add_thread(robin("Thread 1")) objSched.add_thread(robin("Thread 2")) objSched.add_thread(robin("Thread 3")) if duration: objSched.add_thread(stop(duration, objSched)) # Kill after a period objSched.run()
def test(duration = 0): # Runs oscillator, counts interrupts, responds to switches if duration: print("Test interrupt on pin X8 for {:3d} seconds".format(duration)) objSched = Sched() # Requires jumper between pins X7 and X8 objSched.add_thread(oscillator(1)) # 1Hz objSched.add_thread(irqtest_thread()) Switch(objSched, 'X5', open_func = x5print, open_func_args = ("Red",)) # X5 triggers on open Switch(objSched, 'X6', x6print, ("Yellow",)) # X6 triggers on close if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(duration=0 ): # Runs oscillator, counts interrupts, responds to switches if duration: print("Test interrupt on pin X8 for {:3d} seconds".format(duration)) objSched = Sched() # Requires jumper between pins X7 and X8 objSched.add_thread(oscillator(1)) # 1Hz objSched.add_thread(irqtest_thread()) Switch(objSched, 'X5', open_func=x5print, open_func_args=("Red", )) # X5 triggers on open Switch(objSched, 'X6', x6print, ("Yellow", )) # X6 triggers on close if duration: objSched.add_thread(stop(duration, objSched)) objSched.run()
def test(): print("Demonstration of subthreads") objSched = Sched() objSched.add_thread( waitforit(objSched)) # Test of one thread waiting on another objSched.run()
from usched import Sched, wait, Poller from lcdthread import LCD, PINLIST # Library supporting Hitachi LCD module from mpu9150 import MPU9150 def pollfunc(mpu): return 1 if mpu.mag_ready else None def lcd_thread(mylcd, mpu9150): k = mpu9150.mag_correction mylcd[1] = "x {:5.3f} y {:5.3f} z {:5.3f}".format(k[0], k[1], k[2]) while True: reason = (yield Poller(pollfunc, (mpu9150, ))) # Scheduler returns when pollfunc if reason[ 1] == 1: # returns something other than None. 1 indicates ready. xyz = mpu9150.mag.xyz mylcd[0] = "x {:5.1f} y {:5.1f} z {:5.1f}".format( xyz[0], xyz[1], xyz[2]) elif reason[1] == 2: mylcd[0] = "Mag read failure" yield from wait(0.5) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols=24) mpu9150 = MPU9150('X') objSched.add_thread(lcd_thread(lcd0, mpu9150)) objSched.run()
imu = MPU9150('X') # Attached to 'X' bus, 1 device, disable interruots fuse = Fusion() def waitfunc(): yield from wait(0.1) def lcd_thread(mylcd, imu, sw): if sw.value() == 1: mylcd[0] = "Calibrate. Push switch" mylcd[1] = "when done" yield from wait(0.1) fuse.calibrate(imu.mag.xyz, lambda : not sw.value(), waitfunc) print(fuse.magbias) mylcd[0] = "{:5s}{:5s} {:5s}".format("Yaw","Pitch","Roll") count = 0 while True: yield from wait(0.02) # imu updates at 50Hz count += 1 if count % 25 == 0: mylcd[1] = "{:4.0f} {:4.0f} {:4.0f}".format(fuse.heading, fuse.pitch, fuse.roll) fuse.update(imu.accel.xyz, imu.gyro.xyz, imu.mag.xyz) # For 6DOF sensors # fuse.update_nomag(imu.get_accel(), imu.get_gyro()) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols = 24) # Should work with 16 column LCD objSched.add_thread(lcd_thread(lcd0, imu, switch)) objSched.run()
def test(): print("Demonstration of subthreads") objSched = Sched() objSched.add_thread(waitforit(objSched)) # Test of one thread waiting on another objSched.run()
def test(): objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols=24) objSched.add_thread(slave(lcd=lcd0)) objSched.run()
# Example 2 of nonblocking magnetometer reads, demonstrating delegation of polling to a scheduler # Expects an MPU9150 on X side and a 24*2 LCD with Hitachi controller wired as per PINLIST import pyb from usched import Sched, wait, Poller from lcdthread import LCD, PINLIST # Library supporting Hitachi LCD module from mpu9150 import MPU9150 def pollfunc(mpu): return 1 if mpu.mag_ready else None def lcd_thread(mylcd,mpu9150): k = mpu9150.mag_correction mylcd[1] = "x {:5.3f} y {:5.3f} z {:5.3f}".format(k[0],k[1],k[2]) while True: reason = (yield Poller(pollfunc, (mpu9150,))) # Scheduler returns when pollfunc if reason[1] == 1: # returns something other than None. 1 indicates ready. xyz = mpu9150.mag.xyz mylcd[0] = "x {:5.1f} y {:5.1f} z {:5.1f}".format(xyz[0], xyz[1], xyz[2]) elif reason[1] == 2: mylcd[0] = "Mag read failure" yield from wait(0.5) objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols = 24) mpu9150 = MPU9150('X') objSched.add_thread(lcd_thread(lcd0, mpu9150)) objSched.run()
from usched import Sched from machine import Pin led1 = Pin(4, Pin.OUT) led2 = Pin(13, Pin.OUT) def toggle1(objLED, period): while True: yield period objLED.value(not objLED.value()) def toggle2(objLED, period): while True: yield period objLED.value(not objLED.value()) objSched = Sched() objSched.add_thread(toggle1(led1, .2)) objSched.add_thread(toggle1(led2, 1)) objSched.run()
def test(): sched = Sched(True) sched.add_thread(flash(0, 0.5)) sched.add_thread(flash(2, 0.25)) sched.add_thread(stop(5.6, sched)) sched.run()
from usched import Sched from machine import Pin from time import sleep led1 = Pin(4, Pin.OUT) led2 = Pin(13, Pin.OUT) def toggle1(objLED): while True: sleep(1) yield objLED.value(not objLED.value()) def toggle2(objLED): while True: sleep(.1) yield objLED.value(not objLED.value()) objSched = Sched() objSched.add_thread(toggle1(led1)) objSched.add_thread(toggle2(led2)) objSched.run()
def test(): objSched = Sched() lcd0 = LCD(PINLIST, objSched, cols=24) objSched.add_thread(master(lcd=lcd0)) objSched.run()
gpio23.value(not gpio23.value()) yield 1 def connected(): global client client = MQTTClient(CLIENT_ID, 'q.emqtt.com', port=1883, keepalive=20) will_msg = {'id': CLIENT_ID, 'status': False, 'msg': 'The connection from this device is lost:('} client.set_last_will('/device/will/status', json.dumps(will_msg)) client.set_callback(on_message) client.connect() client.subscribe('/device/12345/switch', 0) client.subscribe('/device/4567/switch', 0) def run(): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect('HUAWEI', '0000000000') while not wlan.isconnected(): print('.') time.sleep(.5) print(wlan.ifconfig()) connected() print('WIFI config...') run() objSched = Sched() objSched.add_thread(mqtt_loop()) objSched.add_thread(display()) objSched.add_thread(blink()) objSched.run()