Logic Flow
Example of Code
Import Module
from nanoleafapi import Nanoleaf, NanoleafDigitalTwin, RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE
import random
import time
Defining Components
panel_id_list = nl.get_ids()
color_list = [RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE]
palette_id = panel_id_list[0]
color_id = ""
Touch Event Listener
Monitor whether a touch event occurs.
event_dict = {}
def event_function(event):
print(event)
event_dict.update(event)
nl.register_event(event_function, [4])
Palette Function
Change color when the palette has been touched.
def palette():
color_id = 0
if event_dict['events'][0]['panelId'] == palette_id:
if color_id < 9:
color_id = color_id + 1
if color_id == 9:
color_id = 0
digital_twin.set_color(palette_id,color_list[color_id])
digital_twin.sync()
Paint Function
Copy palette color to panel.
def paint():
paint_panel_id = event_dict['events'][0]['panelId']
if paint_panel_id != palette_id:
paint_color = digital_twin.get_color(palette_id)
digital_twin.set_color(paint_panel_id, paint_color)
digital_twin.sync()
Game Initiation
global palette_id
global paint_panel_id
digital_twin.set_color(palette_id, color_list[0])
Game Structure
while True:
palette()
paint()