From dc8037db6ab2175dfb40bd823e3bdabb33115707 Mon Sep 17 00:00:00 2001 From: David Campey Date: Thu, 4 Sep 2025 11:34:23 +0200 Subject: [PATCH 1/2] Create sense_show.py example --- examples/sense_show.py | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/sense_show.py diff --git a/examples/sense_show.py b/examples/sense_show.py new file mode 100644 index 0000000..0d5cea5 --- /dev/null +++ b/examples/sense_show.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +""" +This module is an example of driving the sense hat with blue dot. + +It shows an array of dots on the ui each corresponding to a pixel on the 16x16 array on the sense hat. + +Tapping on a dot toggles that pixel, it is very satisfying. +""" + +from sense_hat import SenseHat +from time import sleep +from bluedot import BlueDot +from signal import pause + +sense = SenseHat() +bd=BlueDot(cols=8, rows=8) +for x in range(8): + for y in range(8): + bd[x,y].color=(50,50,50) + +def pressed(pos): + "handler for bd.when_pressed that toggles a pixel" + print("button {}.{} pressed".format(pos.col, pos.row)) + pixel = sense.get_pixel(pos.col, pos.row) + if(pixel[2]): # is there blue in the pixel? + sense.set_pixel(pos.col, pos.row, 0,0,0) + bd[pos.col, pos.row].color = (50,50,50) + else: + sense.set_pixel(pos.col, pos.row, 0,0,255) + bd[pos.col, pos.row].color = (0,0,255) + + +bd.when_pressed = pressed + +sense.clear(0,0,255) +sleep(1) +# sense.show_message("PotatoGirl!") + +sense.show_message("BlueDot!") +sense.clear(0,0,0) +pause() + + From a3c3fa51a9e9f8a77348c2e2eed6e4bbd62dc3cb Mon Sep 17 00:00:00 2001 From: David Campey Date: Fri, 5 Sep 2025 10:10:14 +0200 Subject: [PATCH 2/2] Added in a color pallet, and improved comments / output. --- examples/sense_show.py | 69 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/examples/sense_show.py b/examples/sense_show.py index 0d5cea5..cbffb3b 100644 --- a/examples/sense_show.py +++ b/examples/sense_show.py @@ -13,31 +13,80 @@ from signal import pause sense = SenseHat() -bd=BlueDot(cols=8, rows=8) + +# draw an array of dots corresponding to the pixels on the Sense HAT +bd=BlueDot(cols=8, rows=10) for x in range(8): for y in range(8): bd[x,y].color=(50,50,50) +# add in dots for the controls +for x in range(8): + for y in range(8,10): + bd[x,y].color=(255,255,255) + bd[x,y].visible = False + +# set up the brush dot that defines what color will be painted + +brush=(0,0,0) +brush_dot = bd[0,9] +brush_dot.visible = True + +def change_brush(color): + global brush + print(f"change_brush{color}") + brush_dot.color = color + brush=color + print(f'Brush: {brush}') + +# set up the pallet of colours you can change the brush to + +pallet = [(248, 248, 248), (248, 0, 248), (248, 0, 0), (248, 248, 0), (0, 248, 0), (0, 248, 248), (0, 0, 248)] +change_brush(pallet[-1]) + +def change_brush_pressed(pos): + print('change_brush') + color_idx = pos.col-1 + color = pallet[color_idx] + change_brush(color) + +for idx, color in enumerate(pallet): + dot = bd[idx+1, 9] + dot.color=color + dot.when_pressed = change_brush_pressed + dot.visible = True + dot.square = True + +# handle interactions with the pixels + def pressed(pos): "handler for bd.when_pressed that toggles a pixel" - print("button {}.{} pressed".format(pos.col, pos.row)) + + print("dot {}.{} pressed".format(pos.col, pos.row)) + + if pos.row > 7: + return + pixel = sense.get_pixel(pos.col, pos.row) - if(pixel[2]): # is there blue in the pixel? + print(f'Current pixel:{pixel}, toggling brush color: {brush}') + + if tuple(pixel) == brush: # is the pixel set to the brush? + print(f'Pixel already set to {brush}, turning off.') sense.set_pixel(pos.col, pos.row, 0,0,0) bd[pos.col, pos.row].color = (50,50,50) else: - sense.set_pixel(pos.col, pos.row, 0,0,255) - bd[pos.col, pos.row].color = (0,0,255) + print(f'Pixel not set to {brush}, setting.') + sense.set_pixel(pos.col, pos.row, brush[0], brush[1], brush[2]) + bd[pos.col, pos.row].color = brush - bd.when_pressed = pressed +# blink blue to signal ready + sense.clear(0,0,255) sleep(1) -# sense.show_message("PotatoGirl!") - -sense.show_message("BlueDot!") sense.clear(0,0,0) -pause() +# pause to keep the script running +pause() \ No newline at end of file