-
Notifications
You must be signed in to change notification settings - Fork 12
/
comm_nunchuk.c
executable file
·167 lines (140 loc) · 4.44 KB
/
comm_nunchuk.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "comm_nunchuk.h"
#include "ch.h"
#include "hal.h"
#include "hw_conf.h"
#include "comm_usb.h"
#include <string.h>
#include "config.h"
#include "controller.h"
#include "stm32f4xx_conf.h"
static const I2CConfig i2ccfg = {
OPMODE_I2C,
100000,
STD_DUTY_CYCLE,
};
static uint8_t rxbuf[10];
static uint8_t txbuf[10];
static msg_t status = MSG_OK;
static i2caddr_t addr = 0x52;
static systime_t tmo = MS2ST(10);
static volatile Config *config;
void comm_nunchuk_init(void)
{
config = config_get_configuration();
i2cAcquireBus(&I2C_DEV);
palSetPadMode(I2C_GPIO, I2C_SCL_PIN,
PAL_MODE_ALTERNATE(4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
palSetPadMode(I2C_GPIO, I2C_SDA_PIN,
PAL_MODE_ALTERNATE(4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
i2cStart(&I2C_DEV, &i2ccfg);
i2cReleaseBus(&I2C_DEV);
}
void comm_nunchuk_update(void)
{
bool is_ok = true;
txbuf[0] = 0xF0;
txbuf[1] = 0x55;
i2cAcquireBus(&I2C_DEV);
status = i2cMasterTransmitTimeout(&I2C_DEV, addr, txbuf, 2, rxbuf, 0, tmo);
i2cReleaseBus(&I2C_DEV);
is_ok = status == MSG_OK;
if (is_ok) {
txbuf[0] = 0xFB;
txbuf[1] = 0x00;
i2cAcquireBus(&I2C_DEV);
status = i2cMasterTransmitTimeout(&I2C_DEV, addr, txbuf, 2, rxbuf, 0, tmo);
i2cReleaseBus(&I2C_DEV);
is_ok = status == MSG_OK;
}
if (is_ok) {
txbuf[0] = 0x00;
i2cAcquireBus(&I2C_DEV);
status = i2cMasterTransmitTimeout(&I2C_DEV, addr, txbuf, 1, rxbuf, 0, tmo);
i2cReleaseBus(&I2C_DEV);
is_ok = status == MSG_OK;
}
if (is_ok) {
chThdSleepMilliseconds(3);
i2cAcquireBus(&I2C_DEV);
status = i2cMasterReceiveTimeout(&I2C_DEV, addr, rxbuf, 6, tmo);
i2cReleaseBus(&I2C_DEV);
is_ok = status == MSG_OK;
}
if (is_ok) {
static uint8_t last_buffer[6];
int same = 1;
for (int i = 0;i < 6;i++) {
if (last_buffer[i] != rxbuf[i]) {
same = 0;
}
}
memcpy(last_buffer, rxbuf, 6);
if (!same) {
int x = rxbuf[0];
int y = rxbuf[1];
int acc_x = (rxbuf[2] << 2) | ((rxbuf[5] >> 2) & 3);
int acc_y = (rxbuf[3] << 2) | ((rxbuf[5] >> 4) & 3);
int acc_z = (rxbuf[4] << 2) | ((rxbuf[5] >> 6) & 3);
bool btn_z = !((rxbuf[5] >> 0) & 1);
bool btn_c = !((rxbuf[5] >> 1) & 1);
if (btn_c)
{
controller_set_current((((float)y - 128.0) / 128.0) * config->maxCurrent);
/*controller_set_speed((((float)y - 128.0) / 128.0) * 20000.0);*/
}
else
{
controller_disable();
}
}
} else {
controller_disable();
i2cAcquireBus(&I2C_DEV);
i2cStop(&I2C_DEV);
palSetPadMode(I2C_GPIO, I2C_SCL_PIN,
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
palSetPadMode(I2C_GPIO, I2C_SDA_PIN,
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
palSetPad(I2C_GPIO, I2C_SCL_PIN);
palSetPad(I2C_GPIO, I2C_SDA_PIN);
chThdSleep(1);
for(int i = 0; i < 16; i++) {
palClearPad(I2C_GPIO, I2C_SCL_PIN);
chThdSleep(1);
palSetPad(I2C_GPIO, I2C_SCL_PIN);
chThdSleep(1);
}
palClearPad(I2C_GPIO, I2C_SDA_PIN);
chThdSleep(1);
palClearPad(I2C_GPIO, I2C_SCL_PIN);
chThdSleep(1);
palSetPad(I2C_GPIO, I2C_SCL_PIN);
chThdSleep(1);
palSetPad(I2C_GPIO, I2C_SDA_PIN);
palSetPadMode(I2C_GPIO, I2C_SCL_PIN,
PAL_MODE_ALTERNATE(4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
palSetPadMode(I2C_GPIO, I2C_SDA_PIN,
PAL_MODE_ALTERNATE(4) |
PAL_STM32_OTYPE_OPENDRAIN |
PAL_STM32_PUPDR_PULLUP |
PAL_STM32_OSPEED_MID1);
I2C_DEV.state = I2C_STOP;
i2cStart(&I2C_DEV, &i2ccfg);
i2cReleaseBus(&I2C_DEV);
chThdSleepMilliseconds(100);
}
chThdSleepMilliseconds(10);
}