Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
A bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FeralAI committed Sep 21, 2021
1 parent 2409ac0 commit 234af0d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
2 changes: 2 additions & 0 deletions include/pico/config_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "boards/adafruit_itsybitsy_rp2040.h"
#elif BOARD_SPARKFUN_MICRO_RP2040
#include "boards/sparkfun_promicro.h"
#elif BOARD_SPARKFUN_THING_PLUS
#include "boards/sparkfun_thingplus.h"
#elif BOARD_ARDUINO_NANO_CONNECT
#include "boards/arduino_nano_rp2040_connect.h"
#elif BOARD_PIMORONI_PICO_LIPO
Expand Down
10 changes: 1 addition & 9 deletions lib/FlashPROM/include/FlashPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
class FlashPROM
{
public:
FlashPROM();

void start();
void commit();

Expand All @@ -43,14 +41,8 @@ class FlashPROM
memcpy(&cache[index], &value, sizeof(T));
}

static absolute_time_t nextWriteTime;

private:
static spin_lock_t *flashLock;
static uint8_t cache[EEPROM_SIZE_BYTES];

static int64_t writeToFlash(alarm_id_t id, void *user_data);

uint8_t cache[EEPROM_SIZE_BYTES] = { };
};

static FlashPROM EEPROM;
Expand Down
48 changes: 22 additions & 26 deletions lib/FlashPROM/src/FlashPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@

#include "FlashPROM.h"

uint8_t FlashPROM::cache[EEPROM_SIZE_BYTES] = { };
spin_lock_t *FlashPROM::flashLock;
absolute_time_t FlashPROM::nextWriteTime = 0;
alarm_id_t flashWriteAlarm = 0;
static alarm_id_t flashWriteAlarm = 0;
static spin_lock_t *flashLock = nullptr;

FlashPROM::FlashPROM()
int64_t writeToFlash(alarm_id_t id, void *cache)
{
flashLock = spin_lock_instance(spin_lock_claim_unused(true));
multicore_lockout_start_blocking();

while (is_spin_locked(flashLock));
uint32_t interrupts = spin_lock_blocking(flashLock);

flash_range_erase((intptr_t)EEPROM_ADDRESS_START - (intptr_t)XIP_BASE, EEPROM_SIZE_BYTES);
flash_range_program((intptr_t)EEPROM_ADDRESS_START - (intptr_t)XIP_BASE, (uint8_t *)cache, EEPROM_SIZE_BYTES);

spin_unlock(flashLock, interrupts);
flashWriteAlarm = 0;

multicore_lockout_end_blocking();
return 0;
}

void FlashPROM::start()
{
if (flashLock == nullptr)
flashLock = spin_lock_instance(spin_lock_claim_unused(true));

memcpy(cache, reinterpret_cast<uint8_t *>(EEPROM_ADDRESS_START), EEPROM_SIZE_BYTES);
}

Expand All @@ -25,25 +38,8 @@ void FlashPROM::start()
to commit in that timeframe, we'll hold off until the user is done sending changes. */
void FlashPROM::commit()
{
if (flashWriteAlarm)
cancel_alarm(flashWriteAlarm);

flashWriteAlarm = add_alarm_in_ms(EEPROM_WRITE_WAIT, FlashPROM::writeToFlash, NULL, true);
}

int64_t FlashPROM::writeToFlash(alarm_id_t id, void *user_data)
{
multicore_lockout_start_blocking();

while (is_spin_locked(flashLock));
uint32_t interrupts = spin_lock_blocking(flashLock);
// if (flashWriteAlarm)
// cancel_alarm(flashWriteAlarm);

flash_range_erase((intptr_t)EEPROM_ADDRESS_START - (intptr_t)XIP_BASE, EEPROM_SIZE_BYTES);
flash_range_program((intptr_t)EEPROM_ADDRESS_START - (intptr_t)XIP_BASE, cache, EEPROM_SIZE_BYTES);

spin_unlock(flashLock, interrupts);
flashWriteAlarm = 0;

multicore_lockout_end_blocking();
return 0;
// flashWriteAlarm = add_alarm_in_ms(EEPROM_WRITE_WAIT, writeToFlash, cache, true);
}
36 changes: 18 additions & 18 deletions src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ struct GamepadButtonMapping
const uint16_t buttonMask;
};

GamepadButtonMapping mapDpadUp = { .pin = PIN_DPAD_UP, .pinMask = (1 << PIN_DPAD_UP), .buttonMask = GAMEPAD_MASK_UP };
GamepadButtonMapping mapDpadDown = { .pin = PIN_DPAD_DOWN, .pinMask = (1 << PIN_DPAD_DOWN), .buttonMask = GAMEPAD_MASK_DOWN };
GamepadButtonMapping mapDpadLeft = { .pin = PIN_DPAD_LEFT, .pinMask = (1 << PIN_DPAD_LEFT), .buttonMask = GAMEPAD_MASK_LEFT };
GamepadButtonMapping mapDpadRight = { .pin = PIN_DPAD_RIGHT, .pinMask = (1 << PIN_DPAD_RIGHT), .buttonMask = GAMEPAD_MASK_RIGHT };
GamepadButtonMapping mapButtonB1 = { .pin = PIN_BUTTON_B1, .pinMask = (1 << PIN_BUTTON_B1), .buttonMask = GAMEPAD_MASK_B1 };
GamepadButtonMapping mapButtonB2 = { .pin = PIN_BUTTON_B2, .pinMask = (1 << PIN_BUTTON_B2), .buttonMask = GAMEPAD_MASK_B2 };
GamepadButtonMapping mapButtonB3 = { .pin = PIN_BUTTON_B3, .pinMask = (1 << PIN_BUTTON_B3), .buttonMask = GAMEPAD_MASK_B3 };
GamepadButtonMapping mapButtonB4 = { .pin = PIN_BUTTON_B4, .pinMask = (1 << PIN_BUTTON_B4), .buttonMask = GAMEPAD_MASK_B4 };
GamepadButtonMapping mapButtonL1 = { .pin = PIN_BUTTON_L1, .pinMask = (1 << PIN_BUTTON_L1), .buttonMask = GAMEPAD_MASK_L1 };
GamepadButtonMapping mapButtonR1 = { .pin = PIN_BUTTON_R1, .pinMask = (1 << PIN_BUTTON_R1), .buttonMask = GAMEPAD_MASK_R1 };
GamepadButtonMapping mapButtonL2 = { .pin = PIN_BUTTON_L2, .pinMask = (1 << PIN_BUTTON_L2), .buttonMask = GAMEPAD_MASK_L2 };
GamepadButtonMapping mapButtonR2 = { .pin = PIN_BUTTON_R2, .pinMask = (1 << PIN_BUTTON_R2), .buttonMask = GAMEPAD_MASK_R2 };
GamepadButtonMapping mapButtonS1 = { .pin = PIN_BUTTON_S1, .pinMask = (1 << PIN_BUTTON_S1), .buttonMask = GAMEPAD_MASK_S1 };
GamepadButtonMapping mapButtonS2 = { .pin = PIN_BUTTON_S2, .pinMask = (1 << PIN_BUTTON_S2), .buttonMask = GAMEPAD_MASK_S2 };
GamepadButtonMapping mapButtonL3 = { .pin = PIN_BUTTON_L3, .pinMask = (1 << PIN_BUTTON_L3), .buttonMask = GAMEPAD_MASK_L3 };
GamepadButtonMapping mapButtonR3 = { .pin = PIN_BUTTON_R3, .pinMask = (1 << PIN_BUTTON_R3), .buttonMask = GAMEPAD_MASK_R3 };
GamepadButtonMapping mapButtonA1 = { .pin = PIN_BUTTON_A1, .pinMask = (1 << PIN_BUTTON_A1), .buttonMask = GAMEPAD_MASK_A1 };
GamepadButtonMapping mapButtonA2 = { .pin = PIN_BUTTON_A2, .pinMask = (1 << PIN_BUTTON_A2), .buttonMask = GAMEPAD_MASK_A2 };
static GamepadButtonMapping mapDpadUp = { .pin = PIN_DPAD_UP, .pinMask = (1 << PIN_DPAD_UP), .buttonMask = GAMEPAD_MASK_UP };
static GamepadButtonMapping mapDpadDown = { .pin = PIN_DPAD_DOWN, .pinMask = (1 << PIN_DPAD_DOWN), .buttonMask = GAMEPAD_MASK_DOWN };
static GamepadButtonMapping mapDpadLeft = { .pin = PIN_DPAD_LEFT, .pinMask = (1 << PIN_DPAD_LEFT), .buttonMask = GAMEPAD_MASK_LEFT };
static GamepadButtonMapping mapDpadRight = { .pin = PIN_DPAD_RIGHT, .pinMask = (1 << PIN_DPAD_RIGHT), .buttonMask = GAMEPAD_MASK_RIGHT };
static GamepadButtonMapping mapButtonB1 = { .pin = PIN_BUTTON_B1, .pinMask = (1 << PIN_BUTTON_B1), .buttonMask = GAMEPAD_MASK_B1 };
static GamepadButtonMapping mapButtonB2 = { .pin = PIN_BUTTON_B2, .pinMask = (1 << PIN_BUTTON_B2), .buttonMask = GAMEPAD_MASK_B2 };
static GamepadButtonMapping mapButtonB3 = { .pin = PIN_BUTTON_B3, .pinMask = (1 << PIN_BUTTON_B3), .buttonMask = GAMEPAD_MASK_B3 };
static GamepadButtonMapping mapButtonB4 = { .pin = PIN_BUTTON_B4, .pinMask = (1 << PIN_BUTTON_B4), .buttonMask = GAMEPAD_MASK_B4 };
static GamepadButtonMapping mapButtonL1 = { .pin = PIN_BUTTON_L1, .pinMask = (1 << PIN_BUTTON_L1), .buttonMask = GAMEPAD_MASK_L1 };
static GamepadButtonMapping mapButtonR1 = { .pin = PIN_BUTTON_R1, .pinMask = (1 << PIN_BUTTON_R1), .buttonMask = GAMEPAD_MASK_R1 };
static GamepadButtonMapping mapButtonL2 = { .pin = PIN_BUTTON_L2, .pinMask = (1 << PIN_BUTTON_L2), .buttonMask = GAMEPAD_MASK_L2 };
static GamepadButtonMapping mapButtonR2 = { .pin = PIN_BUTTON_R2, .pinMask = (1 << PIN_BUTTON_R2), .buttonMask = GAMEPAD_MASK_R2 };
static GamepadButtonMapping mapButtonS1 = { .pin = PIN_BUTTON_S1, .pinMask = (1 << PIN_BUTTON_S1), .buttonMask = GAMEPAD_MASK_S1 };
static GamepadButtonMapping mapButtonS2 = { .pin = PIN_BUTTON_S2, .pinMask = (1 << PIN_BUTTON_S2), .buttonMask = GAMEPAD_MASK_S2 };
static GamepadButtonMapping mapButtonL3 = { .pin = PIN_BUTTON_L3, .pinMask = (1 << PIN_BUTTON_L3), .buttonMask = GAMEPAD_MASK_L3 };
static GamepadButtonMapping mapButtonR3 = { .pin = PIN_BUTTON_R3, .pinMask = (1 << PIN_BUTTON_R3), .buttonMask = GAMEPAD_MASK_R3 };
static GamepadButtonMapping mapButtonA1 = { .pin = PIN_BUTTON_A1, .pinMask = (1 << PIN_BUTTON_A1), .buttonMask = GAMEPAD_MASK_A1 };
static GamepadButtonMapping mapButtonA2 = { .pin = PIN_BUTTON_A2, .pinMask = (1 << PIN_BUTTON_A2), .buttonMask = GAMEPAD_MASK_A2 };

void MPG::setup()
{
Expand Down

0 comments on commit 234af0d

Please sign in to comment.