Raspberry Pi Digital Hourglass

Trying to get the most out of a day has been big theme of my life lately, as I’m sure it is for many people. I’ve found that I always manage my time better when things are urgent; I’m considerably more productive when I have to be.

I want an ascetically pleasing way to be able to represent how much time is left in the day at a granular scale, like an hourglass. Watching individual seconds disappear will look cool and (hopefully) create that sense of urgency that I want to induce.

Technically, this is a really simple thing to accomplish thanks to python and pygame. Here’s a video of a proof of concept running on my laptop:

At the start of each day, the display is filled with squares at random locations, with a random color. As each second elapses, a square will vanish.

To make it easier to see for the video, I’ve made the squares much bigger than they will actually be for the final build. This is what the display looks like with the squares at their actual size:

The code really really simple, like less than 100 lines simple. Here’s how it works:

Let’s walk through some of the design decisions of this code. The first thing that’s worth talking about is how the data for the squares is handled:

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

classrandom_square(object):

def__init__(self,max_x_location,max_y_location):

self.x_loc=randint(0,max_x_location)

self.y_loc=randint(0,max_y_location)

max_color_value=255

red=randint(0,max_color_value)

green=randint(0,max_color_value)

blue=randint(0,max_color_value)

self.color=[red,green,blue]

It’s just an object with no methods, and on initialization, all the parameters of the square (location and color) are generated randomly as opposed to just floating the raw numbers in arrays around (even though that’s basically what is happening). This let’s us fill the squares array very easily later on in the file here:

Python

1

2

3

# fill the squares with the inital seconds until midnight

forsecond inrange(initial_count):

self.squares.append(random_square(screen_w,screen_h))

and here:

Python

1

2

3

4

5

6

# this will happen once per "day"

iflen(self.squares)==0:

# fill the list of squares to be drawn

forsecond inrange(self.max_count):

self.squares.append(random_square(self.screen_w,self.screen_h))

When it comes time to draw these squares, it also makes that pretty intuitive:

Python

1

2

3

4

# draw the squares

forsquare inself.squares:

rect=(square.x_loc,square.y_loc,self.square_size,self.square_size)

pygame.draw.rect(self.screen,square.color,rect,0)

Again, very simple stuff, but worth it to talk about.

I’ll be back at my place that has the Raspberry Pi and display I would like to use for this project, so more on this then.