-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
49 lines (45 loc) · 1.24 KB
/
input.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Abstraction layer for input device (e.g. joystick, keyboard)
// Handle initialisation of input device with constructor / destructor
// Deliver 8 bit unsigned integer value with detected input (2^0 -> left, 2^1 -> right, 2^2 -> up, 2^3 -> down, 2^4 -> fire)
//
// Variant for RaspberryPi SenseHat joystick
// Only one bit is set (e.g. no combination of directions in case of diagonal movement of joystick)
//
// Written by Stefan Abendroth <sab@ab-solut.com>
// Last updated: 2022-02-09
#include "input.hpp"
Input::Input()
{
open_joystick_device();
}
Input::~Input()
{
close_joystick_device();
}
uint8_t Input::read()
{
uint8_t result=0;
ev=read_joystick_input();
if (ev.type!=JOYSTICK_RELEASE)
{
switch (ev.direction)
{
case DIRECTION_WEST:
result|=INPUT_LEFT;
break;
case DIRECTION_EAST:
result|=INPUT_RIGHT;
break;
case DIRECTION_NORTH:
result|=INPUT_UP;
break;
case DIRECTION_SOUTH:
result|=INPUT_DOWN;
break;
case DIRECTION_DOWN:
result|=INPUT_FIRE;
break;
}
}
return result;
}