# cpb_demo.py

# General function demo for the Adafruit Circuit Playground Bluefruit board.
# The current sensor data is displayed on the serial output stream.
# This program uses only onboard hardware.
# Bits and pieces are taken from the Adafruit sample code.

# Import the board-specific input/output library.
from adafruit_circuitplayground import cp

# Import the standard time module.
import time

# Initialize global variables.
led_index = 0

# Main event loop.
while True:
    
    # Read the accelerometer, which functions primarily as a tilt sensor.
    x, y, z = cp.acceleration

    # Assemble all the touch True/False values into a list.
    touches = [cp.touch_A1, cp.touch_A2, cp.touch_A3, cp.touch_A4, cp.touch_A5, cp.touch_A6, cp.touch_TX]
        
    # Print a one-line status report to the serial console.  This uses a Python
    # 'formatted string literal' to concisely generate a print string from
    # attributes in the environment.
    print(f"But: {cp.button_a} {cp.button_b}  Sw: {cp.switch}  Tilt: {x:6.3f} {y:6.3f} {z:6.3f}  Light: {cp.light}  Temp: {cp.temperature} degC  Touch: {touches}")

    # Treat the pushbuttons as a two-tone keyboard.
    if cp.button_a:
        cp.start_tone(440)
    elif cp.button_b:
        cp.start_tone(660)
    else:
        cp.stop_tone()
        
    # Chase the LEDs by lighting one at a time in index order.
    cp.pixels[led_index] = (0,0,0)     # clear the previous LED to dark
    led_index = (led_index + 1) % 10   # choose the next LED pixel index
    cp.pixels[led_index] = (0,0,255)   # set one blue at a time
    
    # Pause to reduce the rate of output.
    time.sleep(0.2)
