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

Commit

Permalink
Feature/mpg update (#11)
Browse files Browse the repository at this point in the history
* Update MPG

* Fix typo and update MPG
  • Loading branch information
FeralAI authored Sep 12, 2021
1 parent f164095 commit 78b2503
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lib/TinyUSB_Gamepad/include/usb_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C"

InputMode get_input_mode(void);
void initialize_driver(InputMode mode);
void send_report(uint8_t *report, uint8_t report_size);
void send_report(void *report, uint16_t report_size);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion lib/TinyUSB_Gamepad/src/tusb_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void initialize_driver(InputMode mode)
tusb_init();
}

void send_report(uint8_t *report, uint8_t report_size)
void send_report(void *report, uint16_t report_size)
{
static uint8_t previous_report[CFG_TUD_ENDPOINT0_SIZE] = { };

Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build_flags =
lib_deps =
pico_bootsel_via_double_reset
pico_multicore
https://github.com/FeralAI/MPG.git#d7b1a653a122afeea778771612c849e7d512aeef
https://github.com/FeralAI/MPG.git#efd130a8739368320ea8da473916dfe54dbfe3dd
board_build.pio = lib/NeoPico/src/ws2812.pio

[env:debug]
Expand Down
6 changes: 5 additions & 1 deletion src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-FileCopyrightText: Copyright (c) 2021 Jason Skuby (mytechtoybox.com)
*/

#include <MPG.h>
#include <MPGS.h>

#include "pico/stdlib.h"

Expand Down Expand Up @@ -37,8 +37,12 @@ GamepadButtonMapping mapButtonR3 = { .pin = PIN_BUTTON_R3, .pinMask = (1 << PI
GamepadButtonMapping mapButtonA1 = { .pin = PIN_BUTTON_A1, .pinMask = (1 << PIN_BUTTON_A1), .buttonMask = GAMEPAD_MASK_A1, .ledPos = LEDS_BUTTON_13 };
GamepadButtonMapping mapButtonA2 = { .pin = PIN_BUTTON_A2, .pinMask = (1 << PIN_BUTTON_A2), .buttonMask = GAMEPAD_MASK_A2, .ledPos = LEDS_BUTTON_14 };

uint32_t GamepadDebouncer::getMillis() { return to_ms_since_boot(get_absolute_time()); }

void MPG::setup()
{
GamepadStore.start();

GamepadButtonMapping *gamepadMappings[] =
{
&mapDpadUp, &mapDpadDown, &mapDpadLeft, &mapDpadRight,
Expand Down
18 changes: 8 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "pico/multicore.h"
#include "pico/util/queue.h"

#include <MPG.h>
#include <MPGS.h>

#include "NeoPico.hpp"
#include "AnimationStation.hpp"
Expand All @@ -19,9 +19,7 @@
#include "BoardConfig.h"
#include "LEDConfig.h"

uint32_t getMillis() { return to_ms_since_boot(get_absolute_time()); }

MPG gamepad;
MPGS gamepad(GAMEPAD_DEBOUNCE_MILLIS);

#ifdef BOARD_LEDS_PIN
NeoPico leds(BOARD_LEDS_PIN, BOARD_LEDS_COUNT);
Expand Down Expand Up @@ -74,12 +72,12 @@ void setup()

void loop()
{
static uint8_t *report;
static const uint8_t reportSize = gamepad.getReportSize();
static void *report;
static const uint16_t reportSize = gamepad.getReportSize();
static const uint32_t intervalMS = 1;
static uint32_t nextRuntime = 0;

if (getMillis() - nextRuntime < 0)
if (GamepadDebouncer::getMillis() - nextRuntime < 0)
return;

gamepad.read();
Expand All @@ -101,7 +99,7 @@ void loop()
send_report(report, reportSize);

// Ensure next runtime ahead of current time
nextRuntime = getMillis() + intervalMS;
nextRuntime = GamepadDebouncer::getMillis() + intervalMS;
}

void core1()
Expand Down Expand Up @@ -135,7 +133,7 @@ void core1()

while (1)
{
if (getMillis() - nextRuntime < 0)
if (GamepadDebouncer::getMillis() - nextRuntime < 0)
return;

#ifdef BOARD_LEDS_PIN
Expand All @@ -152,6 +150,6 @@ void core1()
#endif

// Ensure next runtime ahead of current time
nextRuntime = getMillis() + intervalMS;
nextRuntime = GamepadDebouncer::getMillis() + intervalMS;
}
}
66 changes: 28 additions & 38 deletions src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,77 @@
* SPDX-FileCopyrightText: Copyright (c) 2021 Jason Skuby (mytechtoybox.com)
*/

#define STORAGE_INPUT_MODE_INDEX 0 // 1 byte
#define STORAGE_DPAD_MODE_INDEX 1 // 1 byte
#define STORAGE_SOCD_MODE_INDEX 2 // 1 byte
#define STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX 3 // 1 byte
#define STORAGE_LEDS_BRIGHTNESS_INDEX 4 // 1 byte

#include <GamepadStorage.h>
#include "FlashPROM.h"
#include "AnimationStorage.hpp"
#include "LEDConfig.h"

GamepadStorage::GamepadStorage()
{
EEPROM.start();
}
#define STORAGE_LEDS_BRIGHTNESS_INDEX (STORAGE_FIRST_AVAILBLE_INDEX) // 1 byte
#define STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX (STORAGE_FIRST_AVAILBLE_INDEX + 1) // 4 bytes

void GamepadStorage::save()
static void getStorageValue(int index, void *data, uint16_t size)
{
EEPROM.commit();
}
uint8_t buffer[size] = { };
for (int i = 0; i < size; i++)
EEPROM.get(index + i, buffer[i]);

DpadMode GamepadStorage::getDpadMode()
{
DpadMode mode = DPAD_MODE_DIGITAL;
EEPROM.get(STORAGE_DPAD_MODE_INDEX, mode);
return mode;
memcpy(data, buffer, size);
}

void GamepadStorage::setDpadMode(DpadMode mode)
static void setStorageValue(int index, void *data, uint16_t size)
{
EEPROM.set(STORAGE_DPAD_MODE_INDEX, mode);
uint8_t buffer[size] = { };
memcpy(buffer, data, size);
for (int i = 0; i < size; i++)
EEPROM.set(index + i, buffer[i]);
}

InputMode GamepadStorage::getInputMode()
/* Gamepad stuffs */

void GamepadStorage::start()
{
InputMode mode = INPUT_MODE_XINPUT;
EEPROM.get(STORAGE_INPUT_MODE_INDEX, mode);
return mode;
EEPROM.start();
}

void GamepadStorage::setInputMode(InputMode mode)
void GamepadStorage::save()
{
EEPROM.set(STORAGE_INPUT_MODE_INDEX, mode);
EEPROM.commit();
}

SOCDMode GamepadStorage::getSOCDMode()
void GamepadStorage::get(int index, void *data, uint16_t size)
{
SOCDMode mode = SOCD_MODE_UP_PRIORITY;
EEPROM.get(STORAGE_SOCD_MODE_INDEX, mode);
return mode;
getStorageValue(index, data, size);
}

void GamepadStorage::setSOCDMode(SOCDMode mode)
void GamepadStorage::set(int index, void *data, uint16_t size)
{
EEPROM.set(STORAGE_SOCD_MODE_INDEX, mode);
setStorageValue(index, data, size);
}

/* Animation stuffs */

AnimationMode AnimationStorage::getBaseAnimation()
{
AnimationMode mode = RAINBOW;
EEPROM.get(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, mode);
getStorageValue(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, &mode, sizeof(AnimationMode));
return mode;
}

void AnimationStorage::setBaseAnimation(AnimationMode mode)
{
EEPROM.set(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, mode);
setStorageValue(STORAGE_LEDS_BASE_ANIMATION_MODE_INDEX, &mode, sizeof(AnimationMode));
}

uint8_t AnimationStorage::getBrightness()
{
uint8_t brightness = LEDS_BRIGHTNESS;
EEPROM.get(STORAGE_LEDS_BRIGHTNESS_INDEX, brightness);
getStorageValue(STORAGE_LEDS_BRIGHTNESS_INDEX, &brightness, sizeof(uint8_t));
return brightness;
}

void AnimationStorage::setBrightness(uint8_t brightness)
{
EEPROM.set(STORAGE_LEDS_BRIGHTNESS_INDEX, brightness);
setStorageValue(STORAGE_LEDS_BRIGHTNESS_INDEX, &brightness, sizeof(uint8_t));
}

void AnimationStorage::setup()
Expand All @@ -91,8 +83,6 @@ void AnimationStorage::setup()
StaticColor::SetDefaultColor(LEDS_STATIC_COLOR_COLOR);
}

/* We don't want to couple our event calls directly to what AS is doing. That means we need to
let it handle its business, and then afterwards save any changes we find. */
void AnimationStorage::save(AnimationStation as)
{
bool dirty = false;
Expand Down

0 comments on commit 78b2503

Please sign in to comment.