-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_input.c
89 lines (72 loc) · 3.3 KB
/
user_input.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "user_input.h"
#include <unistd.h>
#include "hardware_communication.h"
#define RED_KNOB_MASK 0x00ff0000
#define GREEN_KNOB_MASK 0x0000ff00
#define BLUE_KNOB_MASK 0x000000ff
#define KNOBS_CLICK_MASK 0x0f000000
#define RED_KNOB_CLICK 0x04000000
#define GREEN_KNOB_CLICK 0x02000000
#define BLUE_KNOB_CLICK 0x01000000
unsigned int previous_red_knob_value = 0;
unsigned int previous_green_knob_value = 0;
unsigned int previous_blue_knob_value = 0;
void update_knobs_direction(direction *red_knob_direction, direction *green_knob_direction, direction *blue_knob_direction){
// Access register holding 8 bit relative knobs position
unsigned int rgb_knobs_value = get_rgb_knobs_value();
unsigned int new_RED_knob_value = (unsigned int) rgb_knobs_value & RED_KNOB_MASK;
unsigned int new_GREEN_knob_value = (unsigned int) rgb_knobs_value & GREEN_KNOB_MASK;
unsigned int new_BLUE_knob_value = (unsigned int) rgb_knobs_value & BLUE_KNOB_MASK;
*red_knob_direction = NULL_DIRECTION;
*green_knob_direction = NULL_DIRECTION;
*blue_knob_direction = NULL_DIRECTION;
if(previous_red_knob_value - new_RED_knob_value >= 0x00030000 && previous_red_knob_value - new_RED_knob_value < 0x000f0000){
*red_knob_direction = LEFT;
}
else if(new_RED_knob_value - previous_red_knob_value >= 0x00030000 && new_RED_knob_value - previous_red_knob_value < 0x000f0000){
*red_knob_direction = RIGHT;
}
if(previous_green_knob_value - new_GREEN_knob_value >= 0x00000300 && previous_green_knob_value - new_GREEN_knob_value < 0x00000f00){
*green_knob_direction = LEFT;
}
else if(new_GREEN_knob_value - previous_green_knob_value >= 0x00000300 && new_GREEN_knob_value - previous_green_knob_value < 0x00000f00){
*green_knob_direction = RIGHT;
}
if(previous_blue_knob_value - new_BLUE_knob_value >= 0x00000003 && previous_blue_knob_value - new_BLUE_knob_value < 0x0000000f){
*blue_knob_direction = LEFT;
}
else if(new_BLUE_knob_value - previous_blue_knob_value >= 0x00000003 && new_BLUE_knob_value - previous_blue_knob_value < 0x0000000f){
*blue_knob_direction = RIGHT;
}
if( (rgb_knobs_value & KNOBS_CLICK_MASK) == RED_KNOB_CLICK){
*red_knob_direction = UP;
}
if( (rgb_knobs_value & KNOBS_CLICK_MASK) == GREEN_KNOB_CLICK){
*green_knob_direction = UP;
}
if( (rgb_knobs_value & KNOBS_CLICK_MASK) == BLUE_KNOB_CLICK){
*blue_knob_direction = UP;
}
previous_red_knob_value = new_RED_knob_value;
previous_green_knob_value = new_GREEN_knob_value;
previous_blue_knob_value = new_BLUE_knob_value;
}
keyboard_action read_from_keyboard(){
keyboard_action input_symbol = NOTHING;
if(read(STDIN_FILENO, &input_symbol, 1) == 1){
if(input_symbol == PAUSE || input_symbol == PAUSE_CAP){
get_pause();
}
}
return input_symbol;
}
void get_pause(){
keyboard_action input_symbol = NOTHING;
direction red_knob_direction, green_knob_direction, blue_knob_direction;
while(read(STDIN_FILENO, &input_symbol, 1) != 1){
update_knobs_direction(&red_knob_direction, &green_knob_direction, &blue_knob_direction);
if(red_knob_direction != NULL_DIRECTION || green_knob_direction != NULL_DIRECTION || blue_knob_direction != NULL_DIRECTION){
break;
}
}
}