Skip to content

Commit

Permalink
jolting initial progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jnitard committed Dec 20, 2020
1 parent 614858b commit 1e1c345
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
pisample
126 changes: 126 additions & 0 deletions Atom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once

/// \file A set of helpers to configure a PreSonus ATOM.
///
/// Note: functions are considered slow path and return vectors by value.
///
/// Note: the functions are returning vector<uint8_t>, those are midi sequences
/// that must be sent to the device via your favourite MIDI api.
/// I used Alsa on Raspberry Pi + linux.

#include <string>
#include <vector>
#include <cstdint>
#include <stdexcept>
#include <algorithm>

namespace atom
{
/// Short hand to avoid passing to many arguments to functions.
/// Each value is interpreted as an intensity by the device where 0 is off
/// and 127 the maximum value.
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
};

/// Values of pads are between these two values.
enum Pad : uint8_t
{
/// Inclusive, bottom right pad.
One = 24,
/// Exclusive
Last = 41
};

void assertPadValid(Pad);

enum PadMode : uint8_t
{
/// Turns the light off
Off = 0x00,
On = 0x7f,

/// Makes the button blink on/off, till disabled
Blink = 0x01,
/// Makes the fades in and out, till disabled
DimAnim = 0x02
};

void assertModeValid(PadMode);

/// For intenal usage, the midi command to set any of the values for colors.
enum CommandPrefix : uint8_t
{
Mode = 0x90,
Red = 0x91,
Green = 0x92,
Blue = 0x93
};


/// Initialize This is required to be done first and changing button colors.
inline std::vector<uint8_t> initSequence()
{
return { 0x8F, 0x00, 0x7F };
}

/// Change a pad mode. For anything to show up it must be set to something
/// not Off a first time.
inline std::vector<uint8_t> changePadMode(Pad pad, PadMode mode)
{
assertPadValid(pad);
assertModeValid(mode);

return { Mode, pad, mode };
}

/// Change the color of a pad to the given value.

inline std::vector<uint8_t> changePadColor(Pad pad, Color c)
{
assertPadValid(p);
assertColorValid(c);

return {
Red, pad, c.r,
Green, pad, c.g,
Blue, pad, c.b
};
}

inline void assertPadValid(Pad p)
{
if (p >= Pad::One or p < Pad::Last) {
return;
}
throw std::out_of_range("Invalid pad value");
}

inline void assertModeValid(PadMode mode)
{
switch (mode) {
case PadMode::On:
case PadMode::Off:
case PadMode::Blink:
case PadMode::DimAnim:
return;
}
throw std::out_of_range("Unknown value for Padmode");
}

inline void assertColorValid(Color c)
{
auto check = [] (uint8_t component, const char* name) {
if (component > 127) {
throw std::out_of_range("Value for "s + name + " must be below 128");
}
};
check(c.r, "red");
check(c.g, "green");
check(c.b, "blue");
}

}
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not ready to use at all but if it helps anybody looking at the PreSonus ATOM bindings, please do.
79 changes: 79 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <utility>
#include <memory>

#include <alsa/asoundlib.h>

#include <wx/wx.h>

using namespace std;

int main(int argc, char** argv)
{
snd_seq_t* seq = nullptr;

int err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
if (err < 0) {
cerr << "Failed to initialize ALSA (open sequencer)\n";
return EXIT_FAILURE;
}

err = snd_seq_set_client_name(seq, "pisample");
if (err < 0) {
cerr << "Failed to initialize ALSA (set client name)\n";
return EXIT_FAILURE;
}

err = snd_seq_create_simple_port(seq, "pisample",
SND_SEQ_PORT_CAP_WRITE |
SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_MIDI_GENERIC |
SND_SEQ_PORT_TYPE_APPLICATION);
if (err < 0) {
cerr << "Failed to initialize ALSA (create simple port)\n";
return EXIT_FAILURE;
}

const char* inputName = "24";

snd_seq_addr_t in;
err = snd_seq_parse_address(seq, &in, inputName);
if (err < 0) {
cerr << "Failed to understand meaning of '" << inputName << "'\n";
return EXIT_FAILURE;
}

err = snd_seq_connect_from(seq, 0, in.client, in.port);
if (err < 0) {
cerr << "Failed to connect to input '" << inputName << "'\n";
return EXIT_FAILURE;
}

int npfds = snd_seq_poll_descriptors_count(seq, POLLIN);
unique_ptr<pollfd[]> pfds = make_unique<pollfd[]>(npfds);

snd_seq_event* event = nullptr;

while (true) {
snd_seq_poll_descriptors(seq, pfds.get(), npfds, POLLIN);
if (poll(pfds.get(), npfds, -1) < 0) {
cerr << "Poll failed, quitting\n";
return EXIT_FAILURE;
}

while (true) {
err = snd_seq_event_input(seq, &event);
if (err < 0) {
cerr << "Failed to get next event in poll, returning\n";
return EXIT_FAILURE;
}
if (event != nullptr) {
cout << "Source: " << (int) event->source.client << ":"
<< (int) event->source.port << "\n";
}
}
}

return EXIT_SUCCESS;
}

37 changes: 37 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


CXXFLAGS+=-O3 -g
CXXFLAGS+=-std=c++17

#### Cross compilation stuff ####
CXX=arm-linux-gnueabihf-g++.exe
# This needs to contain includes and binaries for:
# - alsa
# - wxwidgets
ROOT=C:\\users\\c_r_o\\Desktop\\pi
#### ----------------------- ####

CXXFLAGS+=-I$(ROOT)

# got from wx-config --cxxflags
CXXFLAGS+= -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread

LFLAGS+=-L$(ROOT)\\lib -lasound

BIN = pisample

SRC = main.cpp

OBJ = $(patsubst %.cpp,%.o,$(SRC))

$(BIN): $(OBJ)

$(BIN):
$(CXX) $(CXXFLAGS) $(CFLAGS) -o $(BIN) $(OBJ) $(LFLAGS)

%.o: %.cpp
$(CXX) $(CXXFLAGS) $(CFLAGS) -c -o $@ $^

clean:
rm $(BIN)
rm -rf *.o
13 changes: 13 additions & 0 deletions pisample.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.associations": {
"utility": "cpp",
"memory": "cpp"
}
}
}

0 comments on commit 1e1c345

Please sign in to comment.