Skip to content

Commit be7c917

Browse files
Merge pull request #3 from mtusnio/master
joystick: example added
2 parents 0f1284a + 5700139 commit be7c917

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ add_executable(letmecreate_test_relay2 relay2/main.c)
1010
target_link_libraries(letmecreate_test_relay2 letmecreate_click letmecreate_core)
1111
install(TARGETS letmecreate_test_relay2 RUNTIME DESTINATION bin)
1212

13+
add_executable(letmecreate_test_joystick joystick/main.c)
14+
target_link_libraries(letmecreate_test_joystick letmecreate_click letmecreate_core)
15+
install(TARGETS letmecreate_test_joystick RUNTIME DESTINATION bin)
16+
1317
add_executable(letmecreate_test_accel accel/main.c)
1418
target_link_libraries(letmecreate_test_accel letmecreate_click letmecreate_core)
1519
install(TARGETS letmecreate_test_accel RUNTIME DESTINATION bin)

examples/joystick/main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <stdio.h>
2+
#include <core/common.h>
3+
#include <core/led.h>
4+
#include <core/i2c.h>
5+
#include <click/joystick.h>
6+
7+
/* Roughly the variable that will be reached when the joystick is farthest
8+
left or right */
9+
#define OFFSET 98
10+
#define MAXIMUM (OFFSET * 2)
11+
12+
/* We have 7 LEDs + the heartbeat LED which we skip */
13+
#define LED_CNT 7
14+
15+
int get_led_mask(float perc)
16+
{
17+
int mask = 0;
18+
int div = perc * LED_CNT;
19+
int i;
20+
if (div > LED_CNT)
21+
div = LED_CNT;
22+
23+
for (i = 0; i < div; i++)
24+
mask |= (1 << i);
25+
26+
return mask;
27+
}
28+
29+
int main()
30+
{
31+
i2c_init();
32+
i2c_select_bus(MIKROBUS_1);
33+
led_init();
34+
35+
/* Use Ctrl-C to break the loop */
36+
while (1) {
37+
int8_t x, y;
38+
int mask;
39+
40+
if (joystick_click_get_position(&x, &y) == -1)
41+
break;
42+
43+
printf("%i %i\n", (int)x, (int)y);
44+
45+
mask = get_led_mask((float)(x + OFFSET)/(float)MAXIMUM);
46+
led_switch_on(mask);
47+
led_switch_off(~mask);
48+
}
49+
50+
led_release();
51+
i2c_release();
52+
53+
return 0;
54+
}

0 commit comments

Comments
 (0)