Implemented two players on single host, changed io processing to interrupt.
This commit is contained in:
54
src/io.c
54
src/io.c
@@ -1,6 +1,29 @@
|
||||
#include "io.h"
|
||||
|
||||
//-----------Local types & functions--------------------------
|
||||
typedef struct pin_s {
|
||||
GPIO_TypeDef* GPIO;
|
||||
uint16_t pinmask;
|
||||
bool input;
|
||||
} pin_t;
|
||||
|
||||
static void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input);
|
||||
static bool pin_get(pin_t* pin);
|
||||
static void pin_set(pin_t* pin, bool status);
|
||||
static void pin_toggle(pin_t* pin);
|
||||
|
||||
|
||||
//-------------Local Variables-------------------------
|
||||
static pin_t pin_t0;
|
||||
static pin_t pin_t1;
|
||||
static pin_t pin_t2;
|
||||
static pin_t pin_t3;
|
||||
|
||||
static uint8_t new = 0;
|
||||
static uint8_t old = 0;
|
||||
static volatile uint8_t edg = 0;
|
||||
|
||||
//---------------Implementation --------------------------------
|
||||
void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) {
|
||||
GPIO_InitTypeDef gi;
|
||||
GPIO_StructInit(&gi);
|
||||
@@ -41,9 +64,38 @@ void pin_set(pin_t* pin, bool status) {
|
||||
|
||||
void pin_toggle(pin_t* pin) {
|
||||
if(!pin->input) {
|
||||
set_pin(pin,!get_pin(pin));
|
||||
pin_set(pin,!pin_get(pin));
|
||||
}
|
||||
}
|
||||
|
||||
void io_init(void){
|
||||
//gpio init
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE);
|
||||
pin_create(&pin_t0, GPIOC, 7, true);
|
||||
pin_create(&pin_t1, GPIOB, 15, true);
|
||||
pin_create(&pin_t2, GPIOB, 14, true);
|
||||
pin_create(&pin_t3, GPIOI, 0, true);
|
||||
|
||||
}
|
||||
|
||||
void io_process(void) {
|
||||
new = pin_get(&pin_t0) |
|
||||
pin_get(&pin_t1) << 1 |
|
||||
pin_get(&pin_t2) << 2 |
|
||||
pin_get(&pin_t3) << 3;
|
||||
|
||||
edg |= (new ^ old) & new; // detect positive edge
|
||||
old = new;
|
||||
}
|
||||
|
||||
bool io_button_has_edge(uint8_t btnnumber) {
|
||||
|
||||
uint8_t bm = (1 << btnnumber);
|
||||
bool status = ((edg & bm) > 0);
|
||||
|
||||
if(status){
|
||||
edg &= ~bm;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user