Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firmware/connections/bhaptics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void BHapticsBLEConnection::setup() {
void BHapticsBLEConnection::loop() {
BLEConnection::loop();

auto now_ms = millis();
App.getOutput()->loopOutput(OUTPUT_PATH_ACCESSORY);

#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true
if (now_ms - this->lastBatteryUpdate >= BATTERY_SAMPLE_RATE) {
Expand Down
5 changes: 3 additions & 2 deletions firmware/modes/bhaptics/tactosy2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "connections/bhaptics.h"
#include "output_components/closest.h"
#include "output_writers/ledc.h"
#include "output_writers/dio.h"

#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true
#include "battery/abstract_battery.h"
Expand Down Expand Up @@ -47,8 +48,8 @@ void setupMode() {
// Configure PWM pins to their positions on the forearm
auto forearmOutputs = transformAutoOutput({
// clang-format off
{new LEDCOutputWriter(32), new LEDCOutputWriter(33), new LEDCOutputWriter(25)},
{new LEDCOutputWriter(26), new LEDCOutputWriter(27), new LEDCOutputWriter(14)},
{new DIOOutputWriter(32, UINT8_MAX * 0.5), new DIOOutputWriter(33, UINT8_MAX * 0.5), new DIOOutputWriter(25, UINT8_MAX * 0.5)},
{new DIOOutputWriter(26, UINT8_MAX * 0.5), new DIOOutputWriter(27, UINT8_MAX * 0.5), new DIOOutputWriter(14, UINT8_MAX * 0.5)},
// clang-format on
});

Expand Down
11 changes: 11 additions & 0 deletions firmware/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ void Output::writeOutput(outputPath_t path, outputData_t& data) {

(*componentSearch).second->writeOutput(data);
}

void Output::loopOutput(outputPath_t path) {
auto componentSearch = this->getComponents()->find(path);

if (componentSearch == this->getComponents()->end()) {
// if no requested component exists, skip
return;
}

(*componentSearch).second->loop();
}
6 changes: 6 additions & 0 deletions firmware/output_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ void OutputComponent::setup() {
kv.second->setup();
}
}

void OutputComponent::loop() {
for (const auto& kv : this->writers) {
kv.second->loop();
}
}
53 changes: 53 additions & 0 deletions firmware/output_writers/dio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <Arduino.h>

#include "config/all.h"
#include "output_writers/dio.h"

#define SOL_ON 0x1
#define SOL_OFF 0x0
#define SOL_COOLDOWN 0x2

void DIOOutputWriter::setup() {
pinMode(this->pin, OUTPUT);

this->highTime = 0;
this->coolDownTime = 0;
this->state = SOL_OFF;
this->desiredState = SOL_OFF;

digitalWrite(pin, LOW);

// Serial.printf("> %s: attached to pin %2u\n",
// __PRETTY_FUNCTION__, this->pin);
}

void DIOOutputWriter::writeOutput(outputIntensity_t intensity) {
if (intensity > this->intensityThreshold) {
this->desiredState = SOL_ON;
}
}

void DIOOutputWriter::loop() {
unsigned long now = millis();

switch(this->state) {
case SOL_ON:
if (now - this->highTime > 25) {
digitalWrite(pin, LOW);
this->coolDownTime = now;
this->state = SOL_COOLDOWN;
}
break;
case SOL_COOLDOWN:
if (now - this->coolDownTime > 25) {
this->state = SOL_OFF;
}
break;
case SOL_OFF:
if (desiredState == SOL_ON) {
this->state = SOL_ON;
digitalWrite(pin, HIGH);
this->highTime = now;
this->desiredState = SOL_OFF; }
}
}
2 changes: 2 additions & 0 deletions include/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class Output {
std::map<outputPath_t, OutputComponent*>* getComponents();

void writeOutput(outputPath_t, outputData_t&);

void loopOutput(outputPath_t);
};
2 changes: 2 additions & 0 deletions include/output_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OutputWriter {
public:
virtual void setup(){};
virtual void writeOutput(outputIntensity_t intensity) = 0;
virtual void loop(){};
};

typedef std::map<outputPoint_t, OutputWriter*> outputMap_t;
Expand All @@ -58,4 +59,5 @@ class OutputComponent : public Component {
};
virtual void writeOutput(outputData_t&) = 0;
void setup() override;
void loop() override;
};
19 changes: 19 additions & 0 deletions include/output_writers/dio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "output.h"

class DIOOutputWriter : public OutputWriter {
private:
uint8_t pin, intensityThreshold;
unsigned long highTime;
unsigned long coolDownTime;
uint8_t state;
uint8_t desiredState;

public:
DIOOutputWriter(const uint8_t pin, const uint8_t intensityThreshold) : pin(pin), intensityThreshold(intensityThreshold){};

void setup() override;
void writeOutput(outputIntensity_t intensity) override;
void loop() override;
};