-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMBR3.c
304 lines (264 loc) · 8.64 KB
/
MBR3.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/******************************************************************************
* File Name: MBR3.c
*
* AUTHOR: Matthias Ernst
*
* Infineon Technologies AG
*
*
*****************************************************************************
* Copyright (C) 2024 Infineon Technologies AG. All rights reserved.
*
* Infineon Technologies AG (INFINEON) is supplying this file for use
* exclusively with Infineon's products. This file can be freely
* distributed within development tools and software supporting such microcontroller
* products.
*
* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
* INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR DIRECT, INDIRECT, INCIDENTAL,
* ASPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "MBR3.h"
#include "i2c.h"
#include "gameController_boardControl.h"
#include "TLE75008.h"
#include "WM.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/*******************************************************************************
* Global Variables
*******************************************************************************/
uint8_t buffer[PACKET_SIZE];
unsigned char dummyData[2] = {
0x00, 0x00
};
unsigned char configData[129] = {
0xFFu, 0x03u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
0xFCu, 0x03u, 0x0Fu, 0x00u, 0x80u, 0xC8u, 0xC8u, 0xC8u,
0xC8u, 0xC8u, 0xC8u, 0xC8u, 0x1Fu, 0x1Fu, 0x7Fu, 0x7Fu,
0x7Fu, 0x7Fu, 0x7Fu, 0x7Fu, 0x01u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x01u, 0x80u,
0x06u, 0x00u, 0x00u, 0x02u, 0x00u, 0x02u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x1Eu, 0x1Eu, 0x00u,
0x00u, 0x1Eu, 0x1Eu, 0x00u, 0x00u, 0x1Bu, 0x01u, 0x01u,
0x05u, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x0Fu, 0x0Fu,
0xFFu, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x01u, 0x50u,
0x00u, 0x37u, 0x06u, 0x00u, 0x00u, 0x0Au, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u,
0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0x00u, 0xAFu, 0x10u
};
unsigned char saveCmd[2] = {
CTRL_CMD, SAVE_CHECK_CRC
};
unsigned char swResetCmd[2] = {
CTRL_CMD, SW_RESET
};
uint8_t pressedButton = 0;
uint8_t buttonReg = 0;
uint8_t homePressed = 0;
uint8_t controlActivated = 0;
uint8_t controlFlag = 0;
uint8_t Capsense_presence = 1;
uint8_t Capsense_absence_counter = 0;
uint8_t Capsense_presence_activated = 0;
uint8_t menu_item_id_selected = 0;
uint8_t menu_item_id_entered = ITEM_ID_MAINMENU;
uint8_t menu_enter = 0;
uint8_t menu_enter_flag = 0;
mbr3_button_rumble_state_t button0_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button1_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button2_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button3_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button8_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button12_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button13_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button14_state = BUTTON_STATE_INACTIVE;
mbr3_button_rumble_state_t button15_state = BUTTON_STATE_INACTIVE;
/*******************************************************************************
* Function Definitions
*******************************************************************************/
void MBR3_init(void)
{
/*Dummy writes to wake up MBR3 via i2c*/
cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, dummyData, 2, 0, true);
cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, dummyData, 2, 0, true);
uint8_t uploadCounter = 0;
printf("MBR3 configuration at 0x%02x: ", MBR3_IIC_ADDR);
while (uploadCounter<3 && CY_RSLT_SUCCESS != cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, configData, 129, 10, true))
{
uploadCounter++;
if(uploadCounter>=2)
{
printf("upload failed!\r\n");
}
}
if (uploadCounter<=2 && CY_RSLT_SUCCESS == cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, saveCmd, 2, 10, true))
{
printf("Success! \r\n");
}
if (CY_RSLT_SUCCESS == cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, swResetCmd, 2, 10, true))
{
}
}
uint16_t MBR3_getButtonStates()
{
uint8_t readValues[2];
unsigned char readCmd[1] = {
PROX_STAT
};
readCmd[0] = BUTTON_STATUS;
if (CY_RSLT_SUCCESS == cyhal_i2c_master_write(&mI2C, MBR3_IIC_ADDR, readCmd, 1, 0, true))
{
}
cyhal_i2c_master_read(&mI2C, MBR3_IIC_ADDR, readValues, 2, 3, true);
//printf("pressed: %i, %i\r\n", readValues[0], readValues[1]);
buttonStates = 0;
uint8_t states = readValues[0];
buttonRumble((states&=0x02), &button1_state);
buttonStates |= states; //Button X
states = readValues[0];
buttonRumble((states&=0x04), &button0_state);
buttonStates |= states>>2; //Button F
states = readValues[0];
buttonRumble((states&=0x08), &button2_state);
buttonStates |= states>>1; //Button I
states = readValues[0];
buttonRumble((states&=0x10), &button3_state);
buttonStates |= states>>1; //Button T
/*Following buttons have double use: BLE HID + Display menu*/
if(!controlActivated) //BLE HID
{
states = readValues[0];
buttonRumble((states&=0x20), &button12_state);
buttonStates |= states<<7; //Button Up
states = readValues[0];
buttonRumble((states&=0x40), &button14_state);
buttonStates |= states<<8; //Button Left
states = readValues[0];
buttonRumble((states&=0x80), &button13_state);
buttonStates |= states<<6; //Button Down
states = readValues[1];
buttonRumble((states&=0x01), &button15_state);
buttonStates |= states<<15; //Button Right
}
else if(controlActivated) //Display control
{
states = readValues[0];
if((states&=0x20)&&!button12_state) GUI_SendKeyMsg(GUI_KEY_UP, 1); //Button Up
buttonRumble((states), &button12_state);
states = readValues[0];
if((states&=0x40)&&!button14_state) menu_item_id_entered = ITEM_ID_MAINMENU; //Button Left
buttonRumble(states, &button14_state);
states = readValues[0];
if((states&=0x80)&&!button13_state) GUI_SendKeyMsg(GUI_KEY_DOWN, 1); //Button Down
buttonRumble((states), &button13_state);
states = readValues[1];
if((states&=0x01)&&!button15_state){ //Button Right
menu_item_id_entered = menu_item_id_selected;
if(!menu_enter_flag) menu_enter = 1;
}else
{
menu_enter_flag = 0;
}
buttonRumble(states, &button15_state);
}
states = readValues[1];
buttonRumble((states&=0x02), &button8_state);
buttonStates |= states<<7; //Button HOME
states = readValues[1];
if(states&=0x02) homePressed = 1;
if(states&&controlActivated == 0&&controlFlag==0)
{
printf("control activated\r\n");
controlActivated = 1;
controlFlag = 1;
}
else if((states)&&controlActivated == 1&&controlFlag==0)
{
printf("control deactivated\r\n");
controlActivated = 0;
controlFlag = 1;
}else if(states == 0)
{
controlFlag = 0;
}
states = readValues[0];
if(states&=0x1E)
{
TLE75008_LED_activate(LED_BUTTONS_RIGHT);
}
else
{
TLE75008_LED_deactivate(LED_BUTTONS_RIGHT);
}
states = readValues[0];
uint8_t states2 = readValues[1];
if((states&=0xE0)!=0||(states2&=0x01)!=0)
{
TLE75008_LED_activate(LED_BUTTONS_LEFT);
}
else
{
TLE75008_LED_deactivate(LED_BUTTONS_LEFT);
}
/*Capsense presence detection*/
states = readValues[0];
states&=0x01;
if(states == 0 && Capsense_presence_activated == 1)
{
Capsense_absence_counter++;
if(Capsense_absence_counter >70)
{
if(Capsense_presence)
{
home_led_state_change_flag = 1;
printf("change to sleep\r\n");
}
else if(!Capsense_presence)
{
Capsense_absence_counter = 0;
}
Capsense_presence = 0;
}
}
else
{
if(!Capsense_presence)
{
home_led_state_change_flag = 1;
}
Capsense_presence = 1;
Capsense_absence_counter = 0;
}
return buttonStates;
}
uint8_t getHomePressedState(void)
{
return homePressed;
}
void resetHomePressedState(void)
{
homePressed = 0;
}
void buttonRumble(uint8_t buttonState, mbr3_button_rumble_state_t *rumbleState)
{
if(buttonState!=0&&*rumbleState!=BUTTON_STATE_ACTIVE_RUMBLED)
{
buttonPress_haptic_feedback();
*rumbleState = BUTTON_STATE_ACTIVE_RUMBLED;
}
else if(buttonState==0)
{
*rumbleState = BUTTON_STATE_INACTIVE;
}
}