Tinder Maths with XinaBox and the Raspberry Pi

Tinder Maths with XinaBox and the Raspberry Pi

Here is a quick demo of xChip OD01 - Display 128x64 (SSD1306) xChip SL06 - Gesture (APDS-9960). I call the demo "Tinder Maths". The program asks the user a maths problem. If the maths problem is correct, you swipe right, otherwise you swipe left. If you are familiar with the social media app called Tinder, you will get the gimmick.

Contents:

  1. What you need
  2. To do
  3. Code
  4. Video guide

 

1. What you need

 

2. To do

  • Install the SL06 library:
pip install apds9960
  • Install the OD01 library:
Check out this Hackster Project. It shows how to install Richard Hull's OLED library.
  • Cut and paste the code below

 

3. Code

from apds9960.const import *
from apds9960 import APDS9960
import RPi.GPIO as GPIO
import smbus2 as smbus
from time import sleep
import random
from luma.core.virtual import terminal
import os
from PIL import ImageFont
from demo_opts import get_device

port = 1
bus = smbus.SMBus(port)

apds = APDS9960(bus)

dirs = {
    APDS9960_DIR_NONE: "none",
    APDS9960_DIR_LEFT: "left",
    APDS9960_DIR_RIGHT: "right",
    APDS9960_DIR_UP: "up",
    APDS9960_DIR_DOWN: "down",
    APDS9960_DIR_NEAR: "near",
    APDS9960_DIR_FAR: "far",
}

def make_font(name, size):
    font_path = os.path.abspath(os.path.join(
                    os.path.dirname(__file__), 'fonts', name))
    return ImageFont.truetype(font_path, size)

device = get_device()
term = terminal(device, make_font("miscfs_.ttf", 12))

def correct():
    global hs
    global term
    hs = hs + 1
    term.println ("CORRECT!")
    term.puts ("Streak Score: %d" % hs)

def incorrect():
    global hs
    global motion
    global term
    hs = 0
    term.println("INCORRECT!")
    term.puts("You swiped %s" % motion)

try:
    apds.setProximityIntLowThreshold(100)
    apds.enableGestureSensor()
    tryagain = False
    hs = 0
    while True:
        sleep(1)
        if not tryagain:
            term.clear()
            term.animate = False
            term.println("Tinder Math")
            term.println("===========")
            term.animate = True
            right=random.choice([True, False])
            oper=random.choice(["-", "+"])
            a = random.randint(1, 10)
            b = random.randint(1, 10)
            if oper == "+":
                c = a+b
            else:
                c = a-b
            if not right:
                r=0
                while r==0:
                    r=random.randint(-5,5)
                c = c + r
            term.puts ("%d %s %d = %d " % (a,oper,b,c))
        while not apds.isGestureAvailable():
            None
        tryagain = False
        motion = dirs.get(apds.readGesture(),"unknown")
        if right and motion=="right":
            correct()
        elif not right and motion=="left":
            correct()
        elif motion<>"left" and motion<>"right":
            tryagain = True
        else:
            incorrect()

finally:
        term.println ("Bye")

 

4. Video guide

 

Please see our Raspberry Pi XK03 kit, which contains all the xChips needed to follow this project. The product page and online instructions contain useful links to getting started, sample code and projects.

Previous article Using Accelerometer to Determine Device Orientation - with the RasPi Kit
Next article Using Microsoft PowerShell and Raspberry Pi to Build a Weather Station with XinaBox