Skip to content

Commit

Permalink
Merge branch 'master' into v3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Jan 5, 2020
2 parents ea9eeaf + e8ca2c2 commit 77d6149
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 63 deletions.
4 changes: 2 additions & 2 deletions blades/blade_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct SnapshotBladeID {
template<int PIN, int PULLUP_PIN>
struct BridgedPullupBladeID {
float id() {
pinMode(PIN, INPUT_PULLUP);
pinMode(PULLUP_PIN, INPUT_PULLUP);
#ifdef TEENSYDUINO
ExternalPullupBladeID<PIN, 33000> ID;
#else
Expand All @@ -51,7 +51,7 @@ struct BridgedPullupBladeID {
#if PROFFIEBOARD_VERSION == 2
ret += 470;
#endif
pinMode(PIN, INPUT_ANALOG);
pinMode(PULLUP_PIN, INPUT_ANALOG);
return ret;
}
};
Expand Down
25 changes: 21 additions & 4 deletions blades/power_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class PowerPinInterface {
public:
virtual void Init() = 0;
virtual void DeInit() = 0;
virtual void Power(bool on) = 0;
};

Expand All @@ -16,6 +17,9 @@ class PowerPinWrapper : public PowerPinInterface {
void Init() override {
pinMode(PIN, OUTPUT);
}
void DeInit() override {
pinMode(PIN, INPUT_ANALOG);
}
void Power(bool power) override {
digitalWrite(PIN, power);
}
Expand All @@ -27,10 +31,14 @@ template<int PIN>
class PowerPinSingleton {
public:
static void Init() {
if (refs_ == 255) {
if (init_refs_++ == 0) {
pinMode(PIN, OUTPUT);
digitalWrite(PIN, 0);
refs_ = 0;
}
}
staic void DeInit() {
if (--init_refs_ == 0) {
pinMode(PIN, INPUT_ANALOG);
}
}
static void Power(bool on) {
Expand All @@ -39,16 +47,20 @@ class PowerPinSingleton {
}
private:
static uint8_t refs_;
static uint8_t init_refs_;
};
template<int PIN>
uint8_t PowerPinSingleton<PIN>::refs_ = 255;
template<int PIN> uint8_t PowerPinSingleton<PIN>::refs_ = 0;
template<int PIN> uint8_t PowerPinSingleton<PIN>::init_refs_ = 0;

template<int PIN>
class PowerPinWrapper : public PowerPinInterface {
public:
void Init() override {
PowerPinSingleton<PIN>::Init();
}
void DeInit() override {
PowerPinSingleton<PIN>::DeInit();
}
void Power(bool power) override {
if (power == on_) return;
on_ = power;
Expand All @@ -66,6 +78,7 @@ template<>
class PowerPINS<> : public PowerPinInterface {
public:
void Init() override {}
void DeInit() override {}
void Power(bool power) override {
battery_monitor.SetLoad(power);
}
Expand All @@ -78,6 +91,10 @@ class PowerPINS<PIN, PINS...> : public PowerPinInterface {
pin_.Init();
rest_.Init();
}
void DeInit() override {
pin_.DeInit();
rest_.DeInit();
}
void Power(bool power) override {
pin_.Power(power);
rest_.Power(power);
Expand Down
4 changes: 2 additions & 2 deletions blades/ws2811_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class WS2811_Blade : public AbstractBlade, CommandParser, Looper {
void Power(bool on) {
if (on) EnableBooster();
if (!powered_ && on) {
power_->Init();
TRACE("Power on");
pinMode(pin_.pin(), OUTPUT);
pin_.BeginFrame();
Expand All @@ -67,6 +68,7 @@ class WS2811_Blade : public AbstractBlade, CommandParser, Looper {
pin_.EndFrame();
power_->Power(on);
pinMode(pin_.pin(), INPUT_ANALOG);
power_->DeInit();
current_blade = NULL;
}
powered_ = on;
Expand All @@ -78,7 +80,6 @@ class WS2811_Blade : public AbstractBlade, CommandParser, Looper {
STDOUT.print("WS2811 Blade with ");
STDOUT.print(pin_.num_leds());
STDOUT.println(" leds.");
power_->Init();
Power(true);
CommandParser::Link();
Looper::Link();
Expand All @@ -88,7 +89,6 @@ class WS2811_Blade : public AbstractBlade, CommandParser, Looper {
void Deactivate() override {
TRACE("Deactivate");
Power(false);
// de-init power pin?
CommandParser::Unlink();
Looper::Unlink();
AbstractBlade::Deactivate();
Expand Down
12 changes: 4 additions & 8 deletions buttons/debounced_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ class DebouncedButton {
void Update() {
STATE_MACHINE_BEGIN();
while (true) {
while (!Read()) YIELD();
pushed_ = true;
do {
if (Read()) last_on_ = millis();
YIELD();
} while (millis() - last_on_ < timeout());
pushed_ = false;
if (!Read()) last_off_ = millis();
pushed_ = millis() - last_off_ > timeout();
YIELD();
}
STATE_MACHINE_END();
}
Expand All @@ -30,7 +26,7 @@ class DebouncedButton {
virtual bool Read() = 0;

private:
uint32_t last_on_;
uint32_t last_off_;
bool pushed_ = false;
StateMachineState state_machine_;
};
Expand Down
2 changes: 1 addition & 1 deletion buttons/stm32l4_touchbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class TouchButton : public ButtonBase {
while (!TSC->ISR.get<TSC_IER_TYPE::EOA>()) YIELD();

if (TSC->ISR.get<TSC_IER_TYPE::MCE>()) {
STDOUT.print("Touch error!");
STDOUT.print("Touch error!\n");
SLEEP(100);
// Error
} else {
Expand Down
25 changes: 16 additions & 9 deletions common/battery_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ BatteryMonitor() : reader_(batteryLevelPin,
void SetLoad(bool on) {
loaded_ = on;
}
bool low() const {
// Battery isn't low if it's not connected at all.
return battery() < (loaded_ ? 2.6 : 3.0) && battery() > 0.5;
}
bool low() const { return millis() - last_ok_ > 1000; }
float battery_percent() {
// Energy is roughly proportional to voltage squared.
float v = battery();
Expand All @@ -46,15 +43,17 @@ BatteryMonitor() : reader_(batteryLevelPin,
}
protected:
void Setup() override {
really_old_voltage_ = old_voltage_ = last_voltage_ = battery_now();
last_voltage_ = battery_now();
SetPinHigh(false);
last_ok_ = millis();
}
void Loop() override {
if (reading_) {
if (!reader_.Done()) return;
float v = battery_now();
last_voltage_ = last_voltage_ * 0.999 + v * 0.001;
last_voltage_ = last_voltage_ * 0.997 + v * 0.003;
reading_ = false;
if (!IsLow()) last_ok_ = millis();
}
uint32_t now = micros();
if (now - last_voltage_read_time_ >= 1000) {
Expand All @@ -71,6 +70,16 @@ BatteryMonitor() : reader_(batteryLevelPin,
}
}

bool IsLow() {
#if VERSION_MAJOR >= 4
if (USBD_Connected()) return false;
#endif
// Battery isn't low if it's not connected at all.
if (battery() < 0.5) return false;

return battery() < (loaded_ ? 2.6 : 3.0);
}

bool Parse(const char* cmd, const char* arg) override {
if (!strcmp(cmd, "battery_voltage")) {
STDOUT.println(battery());
Expand Down Expand Up @@ -128,10 +137,8 @@ BatteryMonitor() : reader_(batteryLevelPin,
bool loaded_ = false;
float last_voltage_ = 0.0;
uint32_t last_voltage_read_time_ = 0;
float old_voltage_ = 0.0;
float really_old_voltage_ = 0.0;
uint32_t last_print_millis_;
uint32_t last_beep_ = 0;
uint32_t last_ok_;
AnalogReader reader_;
bool reading_ = false;
};
Expand Down
63 changes: 63 additions & 0 deletions config/proffieboard_v1_graflex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This is a sample configuration file.
// This saber has:
// o Proffieboard V1 hardware.
// o A touchbutton and a regular button
// o An 8-pin blade connector
// o All blades and presets are stored in common_config.h
// If you have a saber similar to this one, make a copy and use the copy.

#ifdef CONFIG_TOP

// Proffieboard V1 electronics
#include "proffieboard_v1_config.h"

// Number of simultaneously connected blades.
// (For interchangeable blades, see the blades[] array.)
#define NUM_BLADES 1

// Number of buttons
#define NUM_BUTTONS 2

// Dual power buttons means that clicking AUX will also turn the saber on.
// If not defined, AUX will go to next preset when off.
#define DUAL_POWER_BUTTONS

// Volume, useful range is about 0-2000.
#define VOLUME 1800

// If you have two 144 LED/m strips in your blade, connect
// both of them to bladePin and drive them in parallel.
const unsigned int maxLedsPerStrip = 144;

// This defines how sensitive the clash detection is.
#define CLASH_THRESHOLD_G 1.0

// Define this if your power button is a touch button.
#define POWER_TOUCHBUTTON_SENSITIVITY 1700
// #define AUX_TOUCHBUTTON_SENSITIVITY 1700
// #define AUX2_TOUCHBUTTON_SENSITIVITY 1700

#define BLADE_ID_CLASS BridgedPullupBladeID<bladeIdentifyPin, blade2Pin>

// Feature defines, these let you turn off large blocks of code
// used for debugging.
#define ENABLE_AUDIO
#define ENABLE_MOTION
#define ENABLE_WS2811
#define ENABLE_SD

#endif

#include "common_presets.h"

#ifdef CONFIG_BUTTONS

// There are currently three available button classes:
// Button (standard momentary button)
// TouchButton (similar to momentary button, but reacts to touch).
// LatchingButton (on/off button, always controls ignition)

TouchButton PowerButton(BUTTON_POWER, powerButtonPin, 2500, "pow");
Button AuxButton(BUTTON_AUX, auxPin, "aux");
#endif

8 changes: 7 additions & 1 deletion props/blaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Blaster : public PropBase {
}

// Pull in parent's SetPreset, but turn the blaster on.
virtual void SetPreset(int preset_num, bool announce) override {
void SetPreset(int preset_num, bool announce) override {
PropBase::SetPreset(preset_num, announce);

if (!SFX_poweron) {
Expand All @@ -135,6 +135,12 @@ class Blaster : public PropBase {
}
}

void LowBatteryOff() override {
if (SFX_poweron) {
PropBase::LowBatteryOff();
}
}

// Self-destruct pulled from detonator
bool armed_ = false;

Expand Down
44 changes: 27 additions & 17 deletions props/prop_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,32 @@ class PropBase : CommandParser, Looper, protected SaberBase {
#endif
}

virtual void LowBatteryOff() {
if (SaberBase::IsOn()) {
STDOUT.print("Battery low, turning off. Battery voltage: ");
STDOUT.println(battery_monitor.battery());
Off();
}
}

virtual void CheckLowBattery() {
if (battery_monitor.low()) {
// TODO: FIXME
if (current_style() && !current_style()->Charging()) {
LowBatteryOff();

if (millis() - last_beep_ > 5000) {
#ifdef ENABLE_AUDIO
// TODO: allow this to be replaced with WAV file
talkie.Say(talkie_low_battery_15, 15);
#endif
STDOUT << "Battery low :" << battery_monitor.battery() << "\n";
last_beep_ = millis();
}
}
}
}

uint32_t last_beep_;
float current_tick_angle_ = 0.0;

Expand All @@ -735,23 +761,7 @@ class PropBase : CommandParser, Looper, protected SaberBase {
clash_pending_ = false;
Clash2(pending_clash_is_stab_);
}
if (battery_monitor.low()) {
// TODO: FIXME
if (current_style() && !current_style()->Charging()) {
if (SaberBase::IsOn()) {
STDOUT.print("Battery low, turning off. Battery voltage: ");
STDOUT.println(battery_monitor.battery());
Off();
} else if (millis() - last_beep_ > 5000) {
STDOUT.println("Battery low beep");
#ifdef ENABLE_AUDIO
// TODO: allow this to be replaced with WAV file
talkie.Say(talkie_low_battery_15, 15);
#endif
last_beep_ = millis();
}
}
}
CheckLowBattery();
#ifdef ENABLE_AUDIO
if (track_player_ && !track_player_->isPlaying()) {
track_player_.Free();
Expand Down
Loading

0 comments on commit 77d6149

Please sign in to comment.