Thorsten Kimmeskamp
Published © GPL3+

Calliope mini als Motorrad-Controller

Verwandle deinen Calliope in einen Game-Controller und fahre selbst Motorrad!

AdvancedFull instructions provided1 hour527
Calliope mini als Motorrad-Controller

Things used in this project

Hardware components

Calliope mini
Calliope mini
×1

Story

Read more

Code

callimoto_pc.py

Python
import serial
from pykeyboard import PyKeyboard

# Verbindung mit dem Calliope herstellen; der Name des COM-Ports ist systemabhaengig anzupassen
try:
    ser = serial.Serial(port = 'COM3', baudrate = 115200, bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, timeout = 20000)
    print('Serial port open.')
except Exception as e:
    print('Error opening serial port: ' + str(e))
    exit()

threshold_x = 200 # Schwellwert fuer Neigung auf x-Achse
threshold_y = 200 # Schwellwert fuer Neigung auf y-Achse

kbd = PyKeyboard()

while True: # dauerhaft:
    s = ser.readline().split(',') # Calliope liefert Neigungswerte als Textzeile im Format <x>,<y> 
    x, y = int(s[0]), int(s[1]) # Einzelwerte extrahieren

    if y < -threshold_y: # Calliope ist nach vorne geneigt
            kbd.press_key(kbd.up_key) # entspr. Pfeiltaste druecken
    else:
            kbd.release_key(kbd.up_key) # entspr. Pfeiltaste loslassen
    if y > threshold_y: # Calliope ist nach hinten geneigt
            kbd.press_key(kbd.down_key)
    else:
            kbd.release_key(kbd.down_key)
    if x > threshold_x: # Calliope ist nach rechts geneigt
            kbd.press_key(kbd.right_key)
    else:
            kbd.release_key(kbd.right_key)
    if x < -threshold_x: # Calliope ist nach links geneigt
            kbd.press_key(kbd.left_key)
    else:            
	    kbd.release_key(kbd.left_key)

callimoto_controller.js

JavaScript
let y = 0 // Neigung der x-Achse
let x = 0 // Neigung der y-Achse
let threshold_y = 200 // Schwellwert fuer x-Achse
let threshold_x = 200 // Schwellwert fuer y-Achse
while (true) { // dauerhaft:
    x = input.acceleration(Dimension.X) // Neigung der x-Achse messen
    y = input.acceleration(Dimension.Y) // Neigung der y-Achse messen
    serial.writeLine(x + "," + y) // Werte im Format <x>,<y> auf serielle Schnittstelle schreiben
    if (y < -1 * threshold_y) { // Calliope ist nach vorne geneigt
        if (x < -1 * threshold_x) { // Calliope ist nach links geneigt
            basic.showArrow(ArrowNames.NorthWest) // zeige Neigung als Pfeil nach vorne links auf dem LED-Display an
        } else if (x > threshold_x) { // nach selbem Schema alle weiteren moeglichen Richtungen behandeln...
            basic.showArrow(ArrowNames.NorthEast)
        } else {
            basic.showArrow(ArrowNames.North)
        }
    } else if (y > threshold_y) {
        if (x < -1 * threshold_x) {
            basic.showArrow(ArrowNames.SouthWest)
        } else if (x > threshold_x) {
            basic.showArrow(ArrowNames.SouthEast)
        } else {
            basic.showArrow(ArrowNames.South)
        }
    } else {
        if (x < -1 * threshold_x) {
            basic.showArrow(ArrowNames.West)
        } else if (x > threshold_x) {
            basic.showArrow(ArrowNames.East)
        } else { // Calliope ist nirgendwohin geneigt
            basic.clearScreen() // Bildschirminhalt loeschen
        }
    }
}

Credits

Thorsten Kimmeskamp

Thorsten Kimmeskamp

18 projects • 11 followers

Comments