Created
January 21, 2020 15:58
-
-
Save remisarrailh/024ee5ee65d822b3c7615bd4611a8466 to your computer and use it in GitHub Desktop.
Circuit Python MakerDiary nrf52840 Bluetooth RGB test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CircuitPython RGB Color Picker Example | |
import board | |
import pulseio | |
from adafruit_ble import BLERadio | |
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement | |
from adafruit_ble.services.nordic import UARTService | |
from adafruit_bluefruit_connect.packet import Packet | |
from adafruit_bluefruit_connect.color_packet import ColorPacket | |
ble = BLERadio() | |
uart_server = UARTService() | |
advertisement = ProvideServicesAdvertisement(uart_server) | |
led_red = pulseio.PWMOut(board.LED_RED, frequency=5000, duty_cycle=0) | |
led_green = pulseio.PWMOut(board.LED_GREEN, frequency=5000, duty_cycle=0) | |
led_blue = pulseio.PWMOut(board.LED_BLUE, frequency=5000, duty_cycle=0) | |
def map(x, in_min, in_max, out_min, out_max): | |
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | |
while True: | |
# Advertise when not connected. | |
ble.start_advertising(advertisement) | |
while not ble.connected: | |
pass | |
ble.stop_advertising() | |
while ble.connected: | |
packet = Packet.from_stream(uart_server) | |
if isinstance(packet, ColorPacket): | |
print(packet.color) | |
led_red.duty_cycle = int(map(packet.color[0], 0, 255, 65535, 0)) | |
led_green.duty_cycle = int(map(packet.color[1], 0, 255, 65535, 0)) | |
led_blue.duty_cycle = int(map(packet.color[2], 0, 255, 65535, 0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment