Axelerometersnek

September 2019 · Last updated 2021-02-07 · 329 words

Already over a month ago I visited the Chaos Communication Camp 2019 and it was a total blast! My first CCC Camp experience, and surely not my last. Highly recommended!

The badge team outdid themselves again with the card10 smart watch. I loved all the sci-fi backstory about how they heard what the thing would be able to do from recovered info from the future :)

Anyhow, you can program the card10 in MicroPython. At the camp, I started to write a tiny Snake clone using the card10’s accelerometer. It’s only half done, but I loved how much I could do with so few lines of (readable) code.

Today a friend who just started programming asked me when it was the last time coding gave me a great feeling of accomplishment, I said it was the axelerometersnek. Here’s the source code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import display
import bhi160
import utime

sens = bhi160.BHI160Accelerometer()
disp = display.open()
q = [] # queue
QLEN = 10 # enuff is enuff!

def clamp(val):
    return -1.0 if val < -1.0 else 1.0 if val > 1.0 else val

while True:
    utime.sleep(0.03) # 30 fps

    samples = sens.read()
    
    for s in samples:
        if len(q) == QLEN:
            q.pop(0)
        x = 160 - int((clamp(s.x) + 1.0) * 80)
        y = int((clamp(s.y) + 1.0) * 40)
        q.append((x, y))
    
    disp.clear()
    
    for i in range(0, len(q) - 1):
        c = int(20 + 235 * i / (len(q) - 1)) # color
        disp.line(q[i][0], q[i][1], q[i+1][0], q[i+1][1],
                  col = (c,c,c), size = 1)
    
    disp.update()

So few lines, so many stupid bugs. This is already revision 3 😹

Find the card10 API docs here.

Here’s a nice video by Alvar Freude speed run kinda video of the CCCamp 2019.

Portrait

Greetings! You are reading the personal web page of Florian Sesser.

I always like to hear from people. Please have a look at the imprint for ways to get in touch.