class BLEHandler(LoggingHandler): """Send logging output to the BLE uart port.""" def __init__(self): """Create an instance. :param uart: the busio.UART instance to which to write messages """ self._advertising_now = False self._uart = UARTServer() self._uart.start_advertising() def format(self, level, msg): """Generate a string to log. :param level: The level at which to log :param msg: The core message """ return super().format(level, msg) + '\r\n' def emit(self, level, msg): """Generate the message and write it to the UART. :param level: The level at which to log :param msg: The core message """ while not self._uart.connected: pass data = bytes(self.format(level, msg), 'utf-8') self._uart.write(data)
RSTEP_PIN.direction = Direction.OUTPUT Rled.direction = Direction.OUTPUT Gled.direction = Direction.OUTPUT fwd_cmd = str.encode("f") rev_cmd = str.encode("r") fast_cmd = str.encode("h") slow_cmd = str.encode("l") fstep_cmd = str.encode("u") rstep_cmd = str.encode("d") x_cmd = str.encode("x") flag = True while True: uart.start_advertising() # Wait for a connection while not uart.connected: Gled.value = True Rled.value = False FWD_PIN.value = False REV_PIN.value = False SLOW_PIN.value = False FAST_PIN.value = False FSTEP_PIN.value = False RSTEP_PIN.value = False pass while uart.connected:
# Load each pixel's color from the palette using an offset, run it # through the gamma function, pack RGB value and assign to pixel. color = fancy.palette_lookup(palette, (offset + i) / NUM_LEDS) color = fancy.gamma_adjust(color, brightness=0.25) pixels[i] = color.pack() pixels.show() # set initial palette to run on startup palette_choice = PALETTE_RAINBOW # True if cycling a palette cycling = True while True: uart_server.start_advertising() while not uart_server.connected: if cycling: set_palette(palette_choice) offset = (offset + offset_increment) % OFFSET_MAX # Now we're connected while uart_server.connected: if uart_server.in_waiting: packet = Packet.from_stream(uart_server) if isinstance(packet, ColorPacket): cycling = False # Set all the pixels to one color and stay there. pixels.fill(packet.color) pixels.show()