Skip to content

Commit

Permalink
refactor(Battery): use analog float sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Jan 27, 2024
1 parent 9e33f0b commit 750494a
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 84 deletions.
3 changes: 3 additions & 0 deletions firmware/mode_configs/bhaptics/tactsuit_x16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include <Wire.h>

#include "senseshift.h"

#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
3 changes: 3 additions & 0 deletions firmware/mode_configs/bhaptics/tactsuit_x40.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <Wire.h>

#include "senseshift.h"

#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/arduino/output/pca9685.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
22 changes: 12 additions & 10 deletions lib/arduino/senseshift/arduino/input/sensor/analog.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#pragma once

#include <cstdint>

#include <senseshift/input/sensor.hpp>

#include <Arduino.h>

namespace SenseShift::Arduino::Input {
using IAnalogSensor = ::SenseShift::Input::ISimpleSensor<uint16_t>;

template<bool Invert = false>
class AnalogSensor : public IAnalogSensor {
uint8_t pin_;
class AnalogSensor : public ::SenseShift::Input::IFloatSensor {
std::uint8_t pin_;

public:
AnalogSensor(const uint8_t pin) : pin_(pin) {}
AnalogSensor(const std::uint8_t pin) : pin_(pin) {}

void init() override { pinMode(this->pin_, INPUT); };

[[nodiscard]] auto getValue() -> uint16_t override;
[[nodiscard]] auto getValue() -> float override;
};

template<>
[[nodiscard]] inline auto AnalogSensor<false>::getValue() -> uint16_t
[[nodiscard]] inline auto AnalogSensor<false>::getValue() -> float
{
return analogRead(this->pin_);
const auto raw = analogRead(this->pin_);
return static_cast<float>(raw) / ANALOG_MAX;
}

template<>
[[nodiscard]] inline auto AnalogSensor<true>::getValue() -> uint16_t
[[nodiscard]] inline auto AnalogSensor<true>::getValue() -> float
{
return ANALOG_MAX - analogRead(this->pin_);
const auto raw = ANALOG_MAX - analogRead(this->pin_);
return static_cast<float>(raw) / ANALOG_MAX;
}
} // namespace SenseShift::Arduino::Input
34 changes: 0 additions & 34 deletions lib/arduino/senseshift/arduino/output/actuator/servo.hpp

This file was deleted.

5 changes: 3 additions & 2 deletions lib/arduino/senseshift/arduino/output/analog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace SenseShift::Arduino::Output {
/// Arduino analog output
class AnalogOutput : public ::SenseShift::Output::FloatOutput {
class AnalogOutput : public ::SenseShift::Output::IFloatOutput {
public:
static inline constexpr std::uint16_t MAX_INTENSITY = 255;

Expand All @@ -20,7 +20,8 @@ namespace SenseShift::Arduino::Output {
}

void writeState(const float value) override {
analogWrite(this->pin_, value * MAX_INTENSITY);
const auto duty = static_cast<int>(value * MAX_INTENSITY);
analogWrite(this->pin_, duty);
}

private:
Expand Down
10 changes: 4 additions & 6 deletions lib/arduino/senseshift/arduino/output/pca9685.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <Wire.h>

namespace SenseShift::Arduino::Output {
class PCA9685Output : public ::SenseShift::Output::FloatOutput {
class PCA9685Output : public ::SenseShift::Output::IFloatOutput {
public:
static inline constexpr std::uint16_t MAX_INTENSITY = 4095;

Expand All @@ -20,12 +20,10 @@ namespace SenseShift::Arduino::Output {
this->driver_->begin();
}

void writeState(const ValueType intensity) override
void writeState(const ValueType value) override
{
this->driver_->setPin(
this->channel_,
intensity * MAX_INTENSITY
);
const auto duty = static_cast<std::uint16_t>(value * MAX_INTENSITY);
this->driver_->setPin(this->channel_, duty);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion lib/arduino_esp32/senseshift/arduino/output/ledc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SenseShift::Arduino::Output {
static const char *const TAG = "output.ledc";

/// Arduino analog output
class LedcOutput : public ::SenseShift::Output::FloatOutput {
class LedcOutput : public ::SenseShift::Output::IFloatOutput {
public:
explicit LedcOutput(
const std::uint8_t pin,
Expand Down
1 change: 1 addition & 0 deletions lib/battery/senseshift/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace SenseShift::Battery {
});

struct BatteryState {
static constexpr std::uint8_t MAX_LEVEL = 255;
uint8_t level;
};

Expand Down
12 changes: 8 additions & 4 deletions lib/battery/senseshift/battery/sensor.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cstdint>

#include "senseshift/battery.hpp"

#include <senseshift/freertos/task.hpp>
Expand All @@ -18,17 +20,19 @@ namespace SenseShift::Battery {

class NaiveBatterySensor : public IBatterySensor {
public:
NaiveBatterySensor(::SenseShift::Input::ISimpleSensor<uint16_t>* sensor) : sensor(sensor){};
explicit NaiveBatterySensor(::SenseShift::Input::IFloatSensor* sensor) : sensor(sensor){};

[[nodiscard]] auto getValue() -> BatteryState override
{
return { .level =
static_cast<uint8_t>(::SenseShift::simpleMap<uint16_t>(this->sensor->getValue(), 4095, 255)) };
const auto level = static_cast<std::uint8_t>(this->sensor->getValue() * BatteryState::MAX_LEVEL);

return { .level = level};
};

void init() override { this->sensor->init(); }

private:
ISimpleSensor<uint16_t> * sensor;
::SenseShift::Input::IFloatSensor* sensor;
};

class BatterySensor : public ::SenseShift::Input::MemoizedSensor<::SenseShift::Battery::BatteryState> {
Expand Down
3 changes: 2 additions & 1 deletion lib/bhaptics/senseshift/bh/encoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ namespace SenseShift::BH {
private:
static auto effectDataFromByte(const uint8_t byte, const uint8_t maxValue = 100) -> VibroEffectData
{
return VibroEffectData(byte / maxValue);
const auto value = static_cast<float>(byte) / static_cast<float>(maxValue);
return VibroEffectData(value);
}
};
} // namespace SenseShift::BH
8 changes: 4 additions & 4 deletions lib/freertos/senseshift/freertos/input/sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ namespace SenseShift::FreeRTOS::Input {
}
};

template<typename _Tp>
class TaskedSensor : public SensorUpdateTask, public ::SenseShift::Input::ISimpleSensor<_Tp> {
template<typename Tp>
class TaskedSensor : public SensorUpdateTask, public ::SenseShift::Input::ISimpleSensor<Tp> {
friend class SensorUpdateTask;

private:
using Sensor = ::SenseShift::Input::MemoizedSensor<_Tp>;
using Sensor = ::SenseShift::Input::MemoizedSensor<Tp>;

public:
TaskedSensor(Sensor* sensor, std::uint32_t updateDelay, TaskConfig taskConfig) :
Expand All @@ -51,7 +51,7 @@ namespace SenseShift::FreeRTOS::Input {

void init() override { this->sensor->init(); };

_Tp getValue() override { return this->sensor->getValue(); };
Tp getValue() override { return this->sensor->getValue(); };

private:
Sensor* sensor;
Expand Down
2 changes: 1 addition & 1 deletion lib/haptics/senseshift/body/haptics/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ namespace SenseShift::Body::Haptics {
find->second->effect(pos, val);
}

template class OutputBody<Position::Value, Output::FloatOutput::ValueType>;
template class OutputBody<Position::Value, Output::IFloatOutput::ValueType>;
} // namespace SenseShift::Body::Haptics
4 changes: 2 additions & 2 deletions lib/haptics/senseshift/body/haptics/body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <senseshift/output/output.hpp>

namespace SenseShift::Body::Haptics {
/// Output body, contains all the output planes.
/// IOutput body, contains all the output planes.
///
/// \tparam Tc The type of the coordinate.
/// \tparam To The type of the output value.
Expand Down Expand Up @@ -40,5 +40,5 @@ namespace SenseShift::Body::Haptics {
TargetPlaneMap targets_{};
};

using FloatBody = OutputBody<Position::Value, Output::FloatOutput::ValueType>;
using FloatBody = OutputBody<Position::Value, Output::IFloatOutput::ValueType>;
} // namespace SenseShift::Body::Haptics
4 changes: 2 additions & 2 deletions lib/haptics/senseshift/body/haptics/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ namespace SenseShift::Body::Haptics {
return nearest->second;
}

template class OutputPlane<Position::Value, Output::FloatOutput::ValueType>;
template class OutputPlane_Closest<Position::Value, Output::FloatOutput::ValueType>;
template class OutputPlane<Position::Value, Output::IFloatOutput::ValueType>;
template class OutputPlane_Closest<Position::Value, Output::IFloatOutput::ValueType>;
} // namespace SenseShift::Body::Haptics
6 changes: 3 additions & 3 deletions lib/haptics/senseshift/body/haptics/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace SenseShift::Body::Haptics {
/// The type of the output value (e.g. float) for the plane.
using Value = To;
/// The type of the actuator for the plane.
using Actuator = Output::Output<Value>;
using Actuator = Output::IOutput<Value>;

using ActuatorMap = std::map<Position, Actuator*>;
using PositionStateMap = std::map<Position, Value>;
Expand Down Expand Up @@ -73,8 +73,8 @@ namespace SenseShift::Body::Haptics {
[[nodiscard]] static auto findClosestPoint(const PositionSet&, const Position&) -> const Position&;
};

using FloatPlane = OutputPlane<Position::Value, Output::FloatOutput::ValueType>;
using FloatPlane_Closest = OutputPlane_Closest<Position::Value, Output::FloatOutput::ValueType>;
using FloatPlane = OutputPlane<Position::Value, Output::IFloatOutput::ValueType>;
using FloatPlane_Closest = OutputPlane_Closest<Position::Value, Output::IFloatOutput::ValueType>;

// TODO: configurable margin
class PlaneMapper_Margin {
Expand Down
5 changes: 3 additions & 2 deletions lib/io/senseshift/input/sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ namespace SenseShift::Input {
/// \tparam Tp Type of the sensor value
template<typename Tp>
class ISimpleSensor : virtual public IInitializable {
public:
public:
using ValueType = Tp;

/// Get the current sensor value
[[nodiscard]] virtual auto getValue() -> ValueType = 0;
};

using IBinarySensor = ISimpleSensor<bool>;
using IFloatSensor = ISimpleSensor<float>;

template<typename Tp>
class ISensor : virtual ISimpleSensor<Tp>, ITickable {};
class ISensor : public virtual ISimpleSensor<Tp>, public ITickable {};

/// Memoized sensor decorator. Stores the last read value and returns it on subsequent calls
/// \tparam Tp Type of the sensor value
Expand Down
10 changes: 0 additions & 10 deletions lib/io/senseshift/output/binary_output.hpp

This file was deleted.

5 changes: 3 additions & 2 deletions lib/io/senseshift/output/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace SenseShift::Output {
template<typename Tp>
class Output : public IInitializable {
class IOutput : public IInitializable {
public:
using ValueType = Tp;

virtual void writeState(ValueType value) = 0;
};

using FloatOutput = Output<float>;
using IBinaryOutput = IOutput<bool>;
using IFloatOutput = IOutput<float>;
} // namespace SenseShift::Output

0 comments on commit 750494a

Please sign in to comment.