Skip to content

Commit

Permalink
main navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
jnitard committed May 9, 2021
1 parent de6001f commit 2feab19
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 81 deletions.
10 changes: 8 additions & 2 deletions Atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/// Note: the functions are returning vector<Note>, those are midi sequences
/// that must be sent to the device via your favourite MIDI api.
/// I used Alsa on Raspberry Pi + linux.
///
/// NOTE: should only depend on the standard library

#include <string>
#include <vector>
Expand Down Expand Up @@ -59,6 +61,10 @@ namespace atom
/// Exclusive
Last = 0x24 + 16
};
constexpr Pad operator+(Pad lhs, int rhs)
{
return static_cast<Pad>(uint8_t(lhs) + rhs);
}
constexpr uint8_t operator-(Pad lhs, Pad rhs)
{
return uint8_t(lhs) - uint8_t(rhs);
Expand Down Expand Up @@ -180,7 +186,7 @@ namespace atom
/// Initialize This is required to be done first and changing button colors.
inline std::vector<Note> initSequence()
{
return { Note{
return { Note{
.OnOff = false,
.Channel = 0xf,
.Note = 0,
Expand Down Expand Up @@ -239,7 +245,7 @@ namespace atom
inline void assertColorValid(Color c)
{
using namespace std::string_literals;
auto check = [] (uint8_t component, const char* name) {
auto check = [] (uint8_t component, const char* name) {
if (component > 127) {
throw std::out_of_range("Value for "s + name + " must be below 128");
}
Expand Down
2 changes: 2 additions & 0 deletions Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace ps
Device(const Device&) = delete;
~Device();

/// TODO: solve the chicken-egg problem to initialize PiSample and move
/// this to the constructor.
void setSynth(Synth& synth) { _synth = & synth; }

/// Returns true if anything changed, potentially indicating more things
Expand Down
8 changes: 8 additions & 0 deletions Log.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "fmt.h"

#include <iostream>
#include <string>
#include <filesystem>
Expand Down Expand Up @@ -46,6 +48,12 @@ namespace ps
log(Error, format, std::forward<Args>(args)...);
}

template <class ... Args>
[[gnu::noinline]] void throw_(const char* format, Args&& ... args )
{
throw Exception(format, std::forward<Args>(args)...);
}

private:
std::string _prefix;

Expand Down
18 changes: 8 additions & 10 deletions Pads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ Animation ps::caterpillar()
Pads::Pads(Device& device)
: _device(device)
{
forAllPads([this](Pad p) {
setPad(
p,
PadMode::Off,
Color{ .r = 0, .g = 0, .b = 0 }
);
});

startPlaying(caterpillar(), true /*repeat*/);
reset();
}

void Pads::setPad(atom::Pad p, atom::PadMode m, atom::Color c)
Expand Down Expand Up @@ -192,5 +184,11 @@ void Pads::reset()
{
_stepIndex = 0;
_animation.clear();
// TODO : set initial state
forAllPads([this](Pad p) {
setPad(
p,
PadMode::Off,
Color{ .r = 0, .g = 0, .b = 0 }
);
});
}
9 changes: 8 additions & 1 deletion Pads.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ps
{
namespace c = std::chrono;
class PadsAccess;

struct PadState
{
Expand Down Expand Up @@ -37,13 +38,18 @@ namespace ps

void setPad(atom::Pad, atom::PadMode, atom::Color);

/// all pads to off, stop any playing animation
void reset();

void startPlaying(Animation, bool repeat);

void poll();

private:
void step(); // TODO: is this the same as poll exactly ?
void reset();

friend class PadsAccess;
PadsAccess* _access = nullptr;

Device& _device;
std::array<PadState, 16> _pads;
Expand All @@ -54,5 +60,6 @@ namespace ps
c::system_clock::time_point _start = {};
};

/// A default animation
Animation caterpillar();
}
50 changes: 50 additions & 0 deletions PadsAccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include "Pads.h"

#include <stdexcept>

namespace ps
{
class PiSample;

/// Ensure only one component displays data on the pads at a given time.
/// Only PiSample can grant access to the pads to other components.
/// Access can be given and taken at any time, do not store states in the pads.
class PadsAccess
{
public:
PadsAccess(Pads& pads) : _pads(pads)
{ }

protected:
bool isAccessing() const { return _pads._access == this; }
Pads& pads();

private:
friend class PiSample;
void receiveAccess();

virtual void onAccess() {};
Pads& _pads;
};

inline void PadsAccess::receiveAccess()
{
if (not isAccessing()) {
_pads._access = this;
onAccess();
}
else {
_pads._access = this;
}
}

inline Pads& PadsAccess::pads()
{
if (not isAccessing()) {
throw std::logic_error("Trying to get pads without being given access");
}
return _pads;
}
}
Loading

0 comments on commit 2feab19

Please sign in to comment.