Skip to content

Commit

Permalink
Fixed silly error I made
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfEleven committed Oct 5, 2023
1 parent bd9f337 commit 40d0959
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 124 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ endif

# May cause unhelpful build failures
#CFLAGS += -Wpadded

# catch any and all warnings
CFLAGS += -Wextra

CFLAGS += -DPRINTF_INCLUDE_CONFIG_H
Expand Down
2 changes: 1 addition & 1 deletion app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ void APP_CheckKeys(void)
Key == KEY_DOWN ||
Key == KEY_EXIT ||
Key == KEY_MENU ||
Key <= KEY_9) // keys 0-9 can be held down to bypass pressing the F-Key
(Key >= KEY_0 && Key <= KEY_9)) // keys 0-9 can be held down to bypass pressing the F-Key
{
gKeyBeingHeld = true;
APP_ProcessKey(Key, true, true);
Expand Down
2 changes: 1 addition & 1 deletion app/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ static void MENU_Key_0_to_9(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)

if (edit_index < 10)
{
if (/* Key >= KEY_0 && */ Key <= KEY_9)
if (Key >= KEY_0 && Key <= KEY_9)
{
edit[edit_index] = '0' + Key - KEY_0;

Expand Down
194 changes: 98 additions & 96 deletions driver/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,69 @@ uint16_t gDebounceCounter;
bool gWasFKeyPressed;

static const struct {
// Using a 16 bit pre-calculated shift and invert is cheaper
// than using 8 bit and doing shift and invert in code.
uint16_t set_to_zero_mask;

//We are very fortunate.
//The key and pin defines fit together in a single u8,
//making this very efficient
struct {
uint8_t key : 5; //Key 23 is highest
uint8_t pin : 3; //Pin 6 is highest
} pins[4];
} keyboard[5] = {
/* Zero row */
{
//Set to zero to handle special case of nothing pulled down.
.set_to_zero_mask = 0,
.pins = {
{ .key = KEY_SIDE1, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_SIDE2, .pin = GPIOA_PIN_KEYBOARD_1},
/* Duplicate to fill the array with valid values */
{ .key = KEY_SIDE2, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_SIDE2, .pin = GPIOA_PIN_KEYBOARD_1},
}
},
/* First row */
{
.set_to_zero_mask = (uint16_t)(~(1u << GPIOA_PIN_KEYBOARD_4)),
.pins = {
{ .key = KEY_MENU, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_1, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_4, .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_7, .pin = GPIOA_PIN_KEYBOARD_3},
}
},
/* Second row */
{
.set_to_zero_mask = (uint16_t)(~(1u << GPIOA_PIN_KEYBOARD_5)),
.pins = {
{ .key = KEY_UP, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_2 , .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_5 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_8 , .pin = GPIOA_PIN_KEYBOARD_3},
}
},
/* Third row */
{
.set_to_zero_mask = (uint16_t)(~(1u << GPIOA_PIN_KEYBOARD_6)),
.pins = {
{ .key = KEY_DOWN, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_3 , .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_6 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_9 , .pin = GPIOA_PIN_KEYBOARD_3},
}
},
/* Fourth row */
{
.set_to_zero_mask = (uint16_t)(~(1u << GPIOA_PIN_KEYBOARD_7)),
.pins = {
{ .key = KEY_EXIT, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_STAR, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_0 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_F , .pin = GPIOA_PIN_KEYBOARD_3},
}
},

// Using a 16 bit pre-calculated shift and invert is cheaper
// than using 8 bit and doing shift and invert in code.
uint16_t set_to_zero_mask;

// We are very fortunate.
// The key and pin defines fit together in a single u8, making this very efficient
struct {
uint8_t key : 5; // Key 23 is highest
uint8_t pin : 3; // Pin 6 is highest
} pins[4];

} keyboard[] = {

// Zero row
{
// Set to zero to handle special case of nothing pulled down.
.set_to_zero_mask = 0xffff,
.pins = {
{ .key = KEY_SIDE1, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_SIDE2, .pin = GPIOA_PIN_KEYBOARD_1},

// Duplicate to fill the array with valid values
{ .key = KEY_INVALID, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_INVALID, .pin = GPIOA_PIN_KEYBOARD_1}
}
},
{ // First row
.set_to_zero_mask = ~(1u << GPIOA_PIN_KEYBOARD_4) & 0xffff,
.pins = {
{ .key = KEY_MENU, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_1, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_4, .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_7, .pin = GPIOA_PIN_KEYBOARD_3}
}
},
{ // Second row
.set_to_zero_mask = ~(1u << GPIOA_PIN_KEYBOARD_5) & 0xffff,
.pins = {
{ .key = KEY_UP, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_2 , .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_5 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_8 , .pin = GPIOA_PIN_KEYBOARD_3}
}
},
{ // Third row
.set_to_zero_mask = ~(1u << GPIOA_PIN_KEYBOARD_6) & 0xffff,
.pins = {
{ .key = KEY_DOWN, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_3 , .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_6 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_9 , .pin = GPIOA_PIN_KEYBOARD_3}
}
},
{ // Fourth row
.set_to_zero_mask = ~(1u << GPIOA_PIN_KEYBOARD_7) & 0xffff,
.pins = {
{ .key = KEY_EXIT, .pin = GPIOA_PIN_KEYBOARD_0},
{ .key = KEY_STAR, .pin = GPIOA_PIN_KEYBOARD_1},
{ .key = KEY_0 , .pin = GPIOA_PIN_KEYBOARD_2},
{ .key = KEY_F , .pin = GPIOA_PIN_KEYBOARD_3}
}
}
};

KEY_Code_t KEYBOARD_Poll(void)
Expand All @@ -100,41 +99,44 @@ KEY_Code_t KEYBOARD_Poll(void)

// if (!GPIO_CheckBit(&GPIOC->DATA, GPIOC_PIN_PTT))
// return KEY_PTT;


// *****************
for (unsigned int j = 0; j < ARRAY_SIZE(keyboard); j++)

for (unsigned int j = 0; j < ARRAY_SIZE(keyboard); j++)
{
uint16_t reg;

// Set all high
GPIOA->DATA |= 1u << GPIOA_PIN_KEYBOARD_4 |
1u << GPIOA_PIN_KEYBOARD_5 |
1u << GPIOA_PIN_KEYBOARD_6 |
1u << GPIOA_PIN_KEYBOARD_7;

// Clear the pin we are selecting
GPIOA->DATA &= keyboard[j].set_to_zero_mask;

// Wait for the pins to stabilize
SYSTICK_DelayUs(1);

// Read all 4 GPIO pins at once
reg = GPIOA->DATA;
for (unsigned int i = 0; i < ARRAY_SIZE(keyboard[j].pins); i++)
{
//Set all high
GPIOA->DATA |= 1u << GPIOA_PIN_KEYBOARD_4 |
1u << GPIOA_PIN_KEYBOARD_5 |
1u << GPIOA_PIN_KEYBOARD_6 |
1u << GPIOA_PIN_KEYBOARD_7 ;
//Clear the pin we are selecting
GPIOA->DATA &= keyboard[j].set_to_zero_mask;

//Wait for the pins to stabilize. 1 works for me.
SYSTICK_DelayUs(2);

// Read all 4 GPIO pins at once
uint16_t reg = GPIOA->DATA;
for (unsigned int i = 0; i < ARRAY_SIZE(keyboard[j].pins); i++)
{
uint16_t mask = 1u << keyboard[j].pins[i].pin;
if (!(reg & mask))
{
Key = keyboard[j].pins[i].key;
break;
}
}

if (Key != KEY_INVALID)
break;
}

//Create I2C stop condition. Since we might have toggled I2C pins.
//This leaves GPIOA_PIN_KEYBOARD_4 and GPIOA_PIN_KEYBOARD_5 high
I2C_Stop();
const uint16_t mask = 1u << keyboard[j].pins[i].pin;
if (!(reg & mask))
{
Key = keyboard[j].pins[i].key;
break;
}
}

if (Key != KEY_INVALID)
break;
}

// Create I2C stop condition. Since we might have toggled I2C pins.
// This leaves GPIOA_PIN_KEYBOARD_4 and GPIOA_PIN_KEYBOARD_5 high
I2C_Stop();

// Reset VOICE pins
GPIO_ClearBit(&GPIOA->DATA, GPIOA_PIN_KEYBOARD_6);
Expand Down
46 changes: 22 additions & 24 deletions driver/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,28 @@
#include <stdbool.h>
#include <stdint.h>

enum KEY_Code_t {
KEY_0 = 0,
KEY_1 = 1,
KEY_2 = 2,
KEY_3 = 3,
KEY_4 = 4,
KEY_5 = 5,
KEY_6 = 6,
KEY_7 = 7,
KEY_8 = 8,
KEY_9 = 9,
KEY_MENU = 10,
KEY_UP = 11,
KEY_DOWN = 12,
KEY_EXIT = 13,
KEY_STAR = 14,
KEY_F = 15,
KEY_PTT = 21,
KEY_SIDE2 = 22,
KEY_SIDE1 = 23,
KEY_INVALID = 255
};

typedef enum KEY_Code_t KEY_Code_t;
typedef enum {
KEY_INVALID = 0,
KEY_0,
KEY_1,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8,
KEY_9,
KEY_MENU,
KEY_UP,
KEY_DOWN,
KEY_EXIT,
KEY_STAR,
KEY_F,
KEY_PTT,
KEY_SIDE2,
KEY_SIDE1
} KEY_Code_t;

extern KEY_Code_t gKeyReading0;
extern KEY_Code_t gKeyReading1;
Expand Down
Binary file modified firmware.bin
Binary file not shown.
Binary file modified firmware.packed.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
#define IS_NOAA_CHANNEL(x) ((x) >= NOAA_CHANNEL_FIRST && (x) <= NOAA_CHANNEL_LAST)
#define IS_NOT_NOAA_CHANNEL(x) ((x) >= MR_CHANNEL_FIRST && (x) <= FREQ_CHANNEL_LAST)

typedef enum {
enum {
MR_CHANNEL_FIRST = 0,
MR_CHANNEL_LAST = 199u,
FREQ_CHANNEL_FIRST = 200u,
FREQ_CHANNEL_LAST = 206u,
NOAA_CHANNEL_FIRST = 207u,
NOAA_CHANNEL_LAST = 216u,
LAST_CHANNEL
} channel_num_t;
};

enum {
FLASHLIGHT_OFF = 0,
Expand Down

0 comments on commit 40d0959

Please sign in to comment.