-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding in skeleton of steering wheel interface
- Loading branch information
Showing
5 changed files
with
124 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,35 @@ | ||
Button::Button(){} | ||
|
||
Button::~Button(){} | ||
|
||
void Button::checkButtonPin() | ||
{ | ||
if(isPressed == true && state == NOT_PRESSED) | ||
{ | ||
state = DEBOUNCING; | ||
debounce.startTimer(BUTTON_DEBOUNCE_TIME); | ||
} | ||
else if(isPressed == false) | ||
{ | ||
debounce.cancelTimer(); | ||
state = NOT_PRESSED; | ||
} | ||
} | ||
|
||
bool Button::isButtonPressed() | ||
{ | ||
if(state == PRESSED) return true; | ||
|
||
/** | ||
* Returns true if | ||
* 1. The button has successfully passed the debounce period | ||
* 2. The debounce timer has not been canceled/reset | ||
* 3. The button is still being held | ||
*/ | ||
if(isPressed && debounce.isTimerExpired() && !debounce.isTimerReset()) | ||
{ | ||
state = PRESSED; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool Button::isButtonPressed_Pulse() | ||
{ | ||
if(state == PRESSED) return false; | ||
|
||
/** | ||
* Returns true if | ||
* 1. The button has successfully passed the debounce period | ||
* 2. The debounce timer has not been canceled/reset | ||
* 3. The button is still being held | ||
* | ||
* AND | ||
* | ||
* 4. The state of the button has not been read since the button has passed the debounce | ||
*/ | ||
if(isPressed && debounce.isTimerExpired() && !debounce.isTimerReset()) | ||
{ | ||
state = PRESSED; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void Button::setButtonState(bool buttonState) | ||
{ | ||
isPressed = buttonState; | ||
} | ||
|
||
void DriverIO::wheelIO_cb(const CAN_message_t &msg) | ||
{ | ||
union | ||
{ | ||
uint8_t msg[8]; | ||
|
||
struct | ||
{ | ||
uint16_t pot_l; | ||
uint16_t pot_r; | ||
bool button5 : 1; | ||
bool button6 : 1; | ||
bool button3 : 1; | ||
uint8_t x1 : 1; | ||
bool paddle_r : 1; | ||
bool paddle_l : 1; | ||
bool button4 : 1; | ||
bool button2 : 1; | ||
uint8_t x2; | ||
uint8_t x3; | ||
uint8_t x4; | ||
} io; | ||
} wheelio; | ||
|
||
for(uint8_t byte = 0; byte < 8; byte++) | ||
{ | ||
wheelio.msg[byte] = msg.buf[byte]; | ||
} | ||
|
||
wheelio.io.pot_l = SWITCHBYTES(wheelio.io.pot_l); | ||
wheelio.io.pot_r = SWITCHBYTES(wheelio.io.pot_r); | ||
|
||
decrButton.setButtonState(wheelio.io.button2); | ||
incrButton.setButtonState(wheelio.io.button4); | ||
regenButton.setButtonState(wheelio.io.button5); | ||
torqueIncreasePaddle.setButtonState(wheelio.io.paddle_r); | ||
torqueDecreasePaddle.setButtonState(wheelio.io.paddle_l); | ||
accumulatorFanDial.setDialValue(wheelio.io.pot_l); | ||
motorFanDial.setDialValue(wheelio.io.pot_r); | ||
} | ||
#ifndef STEERING_H | ||
#define STEERING_H | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include "ringbuffer.h" | ||
#include "cmsis_os.h" | ||
|
||
typedef enum { | ||
STEERING_PADDLE_LEFT, | ||
STEERING_PADDLE_RIGHT, | ||
NERO_BUTTON_UP, | ||
NERO_BUTTON_DOWN, | ||
NERO_BUTTON_LEFT, | ||
NERO_BUTTON_RIGHT, | ||
NERO_BUTTON_SELECT, | ||
MAX_STEERING_BUTTONS | ||
} steeringio_button_t; | ||
|
||
typedef struct { | ||
osMutexId_t *button_mutex; /* Necessary to allow multiple threads to access same data */ | ||
osMutexId_t *ringbuffer_mutex; | ||
osTimerId_t debounce_timers[MAX_STEERING_BUTTONS]; | ||
ringbuffer_t* debounce_buffer; | ||
} steeringio_t; | ||
|
||
/* Creates a new Steering Wheel interface */ | ||
steeringio_t *steeringio_init(); | ||
|
||
bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button); | ||
|
||
/* For updating values via the wheel's CAN message */ | ||
void steeringio_update(steeringio_t *wheel, uint8_t wheel_data[], uint8_t len); | ||
|
||
#endif /* STEERING_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "steeringio.h" | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include "cmsis_os.h" | ||
#include "cerberus_conf.h" | ||
|
||
static osMutexAttr_t steeringio_data_mutex_attributes; | ||
static osMutexAttr_t steeringio_ringbuffer_mutex_attributes; | ||
|
||
static void debounce_cb(void const *arg); | ||
osTimerDef(debounce_timer, debounce_cb); | ||
|
||
/* Creates a new Steering Wheel interface */ | ||
steeringio_t *steeringio_init() | ||
{ | ||
steeringio_t *steeringio = malloc(sizeof(steeringio_t)); | ||
assert(steeringio); | ||
|
||
/* Create Mutexes */ | ||
steeringio->ringbuffer_mutex = osMutexNew(&steeringio_data_mutex_attributes); | ||
assert(steeringio->ringbuffer_mutex); | ||
|
||
steeringio->button_mutex = osMutexNew(&steeringio_ringbuffer_mutex_attributes); | ||
assert(steeringio->button_mutex); | ||
|
||
//TODO: Error handler for allocation | ||
for (uint8_t i = 0; i < MAX_STEERING_BUTTONS; i++) { | ||
steeringio->debounce_timers[i] = osTimerCreate(osTimer(debounce_timer), osTimerOnce, steeringio); | ||
assert(steeringio->debounce_timers[i]); | ||
} | ||
|
||
steeringio->debounce_buffer = ringbuffer_create(MAX_STEERING_BUTTONS, sizeof(uint8_t)); | ||
assert(steeringio->debounce_buffer); | ||
|
||
return steeringio; | ||
} | ||
|
||
bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button) | ||
{ | ||
bool ret; | ||
|
||
osMutexAcquire(wheel->button_mutex, osWaitForever); | ||
//ret = getdatasomehow; | ||
osRelease(wheel->button_mutex); | ||
|
||
return ret; | ||
} | ||
|
||
/* Callback to see if we have successfully debounced */ | ||
static void debounce_cb(void const *arg) | ||
{ | ||
steeringio_t *wheel = (steeringio_t *)arg; | ||
steeringio_button_t button; | ||
|
||
/* | ||
* Pop off most recent button being debounced | ||
* Note: timers are started sequentially, therefore buttons | ||
* will be popped off in order | ||
* | ||
* Also needs to be atomic | ||
*/ | ||
osMutexAcquire(wheel->ringbuffer_mutex, osWaitForever); | ||
ringbuffer_dequeue(wheel->debounce_buffer, &button); | ||
osRelease(wheel->ringbuffer_mutex); | ||
|
||
if (!button) | ||
return; | ||
|
||
// debounce logic | ||
} | ||
|
||
/* For updating values via the wheel's CAN message */ | ||
void steeringio_update(steeringio_t *wheel, uint8_t wheel_data[], uint8_t len) | ||
{ | ||
if (!wheel || !wheel_data) | ||
return; | ||
|
||
/* Copy message data to wheelio buffer */ | ||
//memcpy(&io, wheel_data, len); | ||
|
||
/* If the value was set high and its not being debounced, trigger timer for debounce */ | ||
//for () | ||
} |
Submodule Embedded-Base
updated
3 files
+1 −0 | .gitignore | |
+24 −0 | middleware/include/ringbuffer.h | |
+89 −0 | middleware/src/ringbuffer.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters