From e056631e4784cf947258b16c23458f0e5728852e Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 3 Jan 2020 19:09:24 -0500 Subject: [PATCH 1/4] Update examples. --- examples/circuitplayground_acceleration.py | 8 +++++--- .../circuitplayground_bluefruit_loud_sound.py | 15 +++++++++++++++ ...itplayground_bluefruit_loud_sound_threshold.py | 15 +++++++++++++++ .../circuitplayground_bluefruit_sound_level.py | 12 ++++++++++++ ...uitplayground_bluefruit_sound_level_plotter.py | 14 ++++++++++++++ examples/circuitplayground_light_neopixels.py | 2 +- examples/circuitplayground_neopixel_0.py | 7 +++++++ examples/circuitplayground_neopixels_fill.py | 5 +++++ examples/circuitplayground_shake.py | 2 +- examples/circuitplayground_shake_red_led.py | 9 +++++++++ examples/circuitplayground_tapdetect.py | 2 ++ .../circuitplayground_tapdetect_single_double.py | 2 ++ examples/circuitplayground_touch_a1.py | 6 ++++++ ..._touched.py => circuitplayground_touch_all.py} | 0 14 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 examples/circuitplayground_bluefruit_loud_sound.py create mode 100644 examples/circuitplayground_bluefruit_loud_sound_threshold.py create mode 100644 examples/circuitplayground_bluefruit_sound_level.py create mode 100644 examples/circuitplayground_bluefruit_sound_level_plotter.py create mode 100644 examples/circuitplayground_neopixel_0.py create mode 100644 examples/circuitplayground_neopixels_fill.py create mode 100644 examples/circuitplayground_shake_red_led.py create mode 100644 examples/circuitplayground_touch_a1.py rename examples/{circuitplayground_touched.py => circuitplayground_touch_all.py} (100%) diff --git a/examples/circuitplayground_acceleration.py b/examples/circuitplayground_acceleration.py index 7101769..6b3d618 100644 --- a/examples/circuitplayground_acceleration.py +++ b/examples/circuitplayground_acceleration.py @@ -1,10 +1,12 @@ -"""This example uses the accelerometer on the Circuit Playground. It prints the values. Try moving -the board to see the values change.""" +""" +This example uses the accelerometer on the Circuit Playground. It prints the values. Try moving +the board to see the values change. If you're using Mu, open the plotter to see the values plotted. +""" import time from adafruit_circuitplayground import cp while True: x, y, z = cp.acceleration - print(x, y, z) + print((x, y, z)) time.sleep(0.1) diff --git a/examples/circuitplayground_bluefruit_loud_sound.py b/examples/circuitplayground_bluefruit_loud_sound.py new file mode 100644 index 0000000..198f3be --- /dev/null +++ b/examples/circuitplayground_bluefruit_loud_sound.py @@ -0,0 +1,15 @@ +""" +This example lights up the NeoPixels on a Circuit Playground Bluefruit in response to a loud sound. +Try snapping or clapping near the board to trigger the LEDs. + +NOTE: This example does NOT support Circuit Playground Express. +""" +import time +from adafruit_circuitplayground import cp + +while True: + if cp.loud_sound(): + cp.pixels.fill((50, 0, 50)) + time.sleep(0.2) + else: + cp.pixels.fill((0, 0, 0)) diff --git a/examples/circuitplayground_bluefruit_loud_sound_threshold.py b/examples/circuitplayground_bluefruit_loud_sound_threshold.py new file mode 100644 index 0000000..e982f87 --- /dev/null +++ b/examples/circuitplayground_bluefruit_loud_sound_threshold.py @@ -0,0 +1,15 @@ +""" +This example lights up the NeoPixels on a Circuit Playground Bluefruit in response to a loud sound. +Try snapping or clapping near the board to trigger the LEDs. + +NOTE: This example does NOT support Circuit Playground Express. +""" +import time +from adafruit_circuitplayground import cp + +while True: + if cp.loud_sound(sound_threshold=250): + cp.pixels.fill((50, 0, 50)) + time.sleep(0.2) + else: + cp.pixels.fill((0, 0, 0)) diff --git a/examples/circuitplayground_bluefruit_sound_level.py b/examples/circuitplayground_bluefruit_sound_level.py new file mode 100644 index 0000000..d12d992 --- /dev/null +++ b/examples/circuitplayground_bluefruit_sound_level.py @@ -0,0 +1,12 @@ +""" +This example prints out sound levels using the sound sensor on a Circuit Playground Bluefruit. +Try making sounds towards the board to see the values change. + +NOTE: This example does NOT support Circuit Playground Express. +""" +import time +from adafruit_circuitplayground import cp + +while True: + print("Sound level:", cp.sound_level) + time.sleep(0.1) diff --git a/examples/circuitplayground_bluefruit_sound_level_plotter.py b/examples/circuitplayground_bluefruit_sound_level_plotter.py new file mode 100644 index 0000000..d3d1e08 --- /dev/null +++ b/examples/circuitplayground_bluefruit_sound_level_plotter.py @@ -0,0 +1,14 @@ +""" +This example prints out sound levels using the sound sensor on a Circuit Playground Bluefruit. If +you are using Mu, open the plotter to see the sound level plotted. Try making sounds towards the +board to see the values change. + +NOTE: This example does NOT support Circuit Playground Express. +""" +import time +from adafruit_circuitplayground import cp + +while True: + print("Sound level:", cp.sound_level) + print((cp.sound_level,)) + time.sleep(0.1) diff --git a/examples/circuitplayground_light_neopixels.py b/examples/circuitplayground_light_neopixels.py index 8427e66..23a330c 100644 --- a/examples/circuitplayground_light_neopixels.py +++ b/examples/circuitplayground_light_neopixels.py @@ -13,7 +13,7 @@ def scale_range(value): - """Scale a value from 0-320 (light range) to 0-10 (the number of NeoPixels). + """Scale a value from 0-320 (light range) to 0-9 (NeoPixel range). Allows remapping light value to pixel position.""" return int(value / 320 * 10) diff --git a/examples/circuitplayground_neopixel_0.py b/examples/circuitplayground_neopixel_0.py new file mode 100644 index 0000000..2b2e8f1 --- /dev/null +++ b/examples/circuitplayground_neopixel_0.py @@ -0,0 +1,7 @@ +"""This example lights up the first NeoPixel red.""" +from adafruit_circuitplayground import cp + +cp.pixels.brightness = 0.3 + +while True: + cp.pixels[0] = (255, 0, 0) diff --git a/examples/circuitplayground_neopixels_fill.py b/examples/circuitplayground_neopixels_fill.py new file mode 100644 index 0000000..640cf94 --- /dev/null +++ b/examples/circuitplayground_neopixels_fill.py @@ -0,0 +1,5 @@ +"""This example lights up all the NeoPixel LEDs red.""" +from adafruit_circuitplayground import cp + +while True: + cp.pixels.fill((50, 0, 0)) diff --git a/examples/circuitplayground_shake.py b/examples/circuitplayground_shake.py index 423341a..984f441 100644 --- a/examples/circuitplayground_shake.py +++ b/examples/circuitplayground_shake.py @@ -2,5 +2,5 @@ from adafruit_circuitplayground import cp while True: - if cp.shake(shake_threshold=20): + if cp.shake(): print("Shake detected!") diff --git a/examples/circuitplayground_shake_red_led.py b/examples/circuitplayground_shake_red_led.py new file mode 100644 index 0000000..7399680 --- /dev/null +++ b/examples/circuitplayground_shake_red_led.py @@ -0,0 +1,9 @@ +"""This example flashes the little red LED when the Circuit Playground is shaken.""" +from adafruit_circuitplayground import cp + +while True: + if cp.shake(shake_threshold=20): + print("Shake detected!") + cp.red_led = True + else: + cp.red_led = False diff --git a/examples/circuitplayground_tapdetect.py b/examples/circuitplayground_tapdetect.py index fc1e688..e32aa39 100644 --- a/examples/circuitplayground_tapdetect.py +++ b/examples/circuitplayground_tapdetect.py @@ -1,4 +1,5 @@ """This example prints to the serial console when the board is double-tapped.""" +import time from adafruit_circuitplayground import cp # Change to 1 for single-tap detection. @@ -7,3 +8,4 @@ while True: if cp.tapped: print("Tapped!") + time.sleep(0.05) diff --git a/examples/circuitplayground_tapdetect_single_double.py b/examples/circuitplayground_tapdetect_single_double.py index 77cf024..c1da0bd 100644 --- a/examples/circuitplayground_tapdetect_single_double.py +++ b/examples/circuitplayground_tapdetect_single_double.py @@ -22,3 +22,5 @@ tap_count += 1 print("Reached 2 double-taps!") print("Done.") +while True: + cp.red_led = True diff --git a/examples/circuitplayground_touch_a1.py b/examples/circuitplayground_touch_a1.py new file mode 100644 index 0000000..df59cbf --- /dev/null +++ b/examples/circuitplayground_touch_a1.py @@ -0,0 +1,6 @@ +"""This example prints to the serial console when you touch capacitive touch pad A1.""" +from adafruit_circuitplayground import cp + +while True: + if cp.touch_A1: + print('Touched pad A1') diff --git a/examples/circuitplayground_touched.py b/examples/circuitplayground_touch_all.py similarity index 100% rename from examples/circuitplayground_touched.py rename to examples/circuitplayground_touch_all.py From 8ec12ef85e7512c6778ad3b8a09cf287695c2bb4 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 3 Jan 2020 19:18:10 -0500 Subject: [PATCH 2/4] Update example name. --- docs/examples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples.rst b/docs/examples.rst index 9d11b71..e882b92 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -27,7 +27,7 @@ Ensure your device works with this simple test. :caption: examples/circuitplayground_tone.py :linenos: -.. literalinclude:: ../examples/circuitplayground_touched.py +.. literalinclude:: ../examples/circuitplayground_touch_all.py :caption: examples/circuitplayground_touched.py :linenos: From 9b1a4bd9cb1da543dd0da7b9a5ccacc66383a68f Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 3 Jan 2020 20:05:07 -0500 Subject: [PATCH 3/4] Update to avoid potential out of range error. --- examples/circuitplayground_light_neopixels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/circuitplayground_light_neopixels.py b/examples/circuitplayground_light_neopixels.py index 23a330c..04fa4f1 100644 --- a/examples/circuitplayground_light_neopixels.py +++ b/examples/circuitplayground_light_neopixels.py @@ -15,7 +15,7 @@ def scale_range(value): """Scale a value from 0-320 (light range) to 0-9 (NeoPixel range). Allows remapping light value to pixel position.""" - return int(value / 320 * 10) + return int(value / 320 * 9) while True: From fdf4fdf73b1a0bdb0729a853580120e9a6a2d0e1 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 3 Jan 2020 20:12:01 -0500 Subject: [PATCH 4/4] Use round instead of int --- examples/circuitplayground_light_neopixels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/circuitplayground_light_neopixels.py b/examples/circuitplayground_light_neopixels.py index 04fa4f1..68b9edf 100644 --- a/examples/circuitplayground_light_neopixels.py +++ b/examples/circuitplayground_light_neopixels.py @@ -15,7 +15,7 @@ def scale_range(value): """Scale a value from 0-320 (light range) to 0-9 (NeoPixel range). Allows remapping light value to pixel position.""" - return int(value / 320 * 9) + return round(value / 320 * 9) while True: