コード例 #1
0
class UartThreadExample(threading.Thread):
    def __init__(self):
        self.loop = None
        self.uart = None
        threading.Thread.__init__(self)

    def run(self):
        self.loop = asyncio.new_event_loop()
        self.uart = CrownstoneUart()
        # choose either sync, or async operation
        self.loop.run_until_complete(self.runIt())
        # self.runIt_sync()

    async def runIt(self):
        await self.uart.initialize_usb()
        self.switch_crownstone()

    def runIt_sync(self):
        self.uart.initialize_usb_sync()
        self.switch_crownstone()

    def switch_crownstone(self):
        turnOn = True
        for i in range(0, 10):
            if not self.uart.running:
                break

            if turnOn:
                print("Switching Crownstone on  (iteration: ", i, ")")
            else:
                print("Switching Crownstone off (iteration: ", i, ")")
            self.uart.switch_crownstone(targetCrownstoneId, on=turnOn)
            turnOn = not turnOn
            time.sleep(2)

    def stop(self):
        self.uart.stop()
コード例 #2
0
# Start up the USB bridge.
uart.initialize_usb_sync()
# you can alternatively do this async by
# await uart.initialize_usb()

# Set up event listeners
UartEventBus.subscribe(UartTopics.newDataAvailable, showNewData)

# Switch this Crownstone on and off.
turnOn = True

# The try except part is just to catch a control+c, time.sleep does not appreciate being killed.
try:
    for i in range(0, 10):
        if not uart.running:
            break

        if turnOn:
            print("Switching Crownstone on  (iteration: ", i, ")")
        else:
            print("Switching Crownstone off (iteration: ", i, ")")
        uart.switch_crownstone(targetCrownstoneId, on=turnOn)

        turnOn = not turnOn
        time.sleep(2)
except KeyboardInterrupt:
    print("\nClosing example.... Thanks for your time!")

uart.stop()