Skip to content

Commit

Permalink
Refactoring common gfx function
Browse files Browse the repository at this point in the history
DrawKnobAt -> DrawSlider in base class
  • Loading branch information
djphazer committed Jan 8, 2024
1 parent 64d73c1 commit d01f754
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 72 deletions.
26 changes: 10 additions & 16 deletions software/o_c_REV/HEM_BootsNCat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include "vector_osc/HSVectorOscillator.h"
#include "vector_osc/WaveformManager.h"

#define BNC_MAX_PARAM 63

class BootsNCat : public HemisphereApplet {
public:

static constexpr uint8_t BNC_MAX_PARAM = 63;

const char* applet_name() {
return "BootsNCat";
}
Expand Down Expand Up @@ -169,34 +169,28 @@ private:
int8_t blend;

void DrawInterface() {
const uint8_t w = 16;
const uint8_t x = 45;

gfxPrint(1, 15, "BD Tone");
DrawKnobAt(15, tone[0], cursor == 0);
DrawSlider(x, 15, w, tone[0], BNC_MAX_PARAM, cursor == 0);

gfxPrint(1, 25, " Decay");
DrawKnobAt(25, decay[0], cursor == 1);
DrawSlider(x, 25, w, decay[0], BNC_MAX_PARAM, cursor == 1);

gfxPrint(1, 35, "SD Tone");
DrawKnobAt(35, tone[1], cursor == 2);
DrawSlider(x, 35, w, tone[1], BNC_MAX_PARAM, cursor == 2);

gfxPrint(1, 45, " Decay");
DrawKnobAt(45, decay[1], cursor == 3);
DrawSlider(x, 45, w, decay[1], BNC_MAX_PARAM, cursor == 3);

gfxPrint(1, 55, "Blend");
DrawKnobAt(55, blend, cursor == 4);
DrawSlider(x, 55, w, blend, BNC_MAX_PARAM, cursor == 4);

// Level indicators
ForEachChannel(ch) gfxInvert(1, 14 + (20 * ch), ProportionCV(levels[ch], 42), 9);
}

void DrawKnobAt(byte y, byte value, bool is_cursor) {
byte x = 45;
byte w = Proportion(value, BNC_MAX_PARAM, 16);
byte p = is_cursor ? 1 : 3;
gfxDottedLine(x, y + 4, 62, y + 4, p);
gfxRect(x + w, y, 2, 7);
if (EditMode() && is_cursor) gfxInvert(x, y, 18, 7);
}

void SetBDFreq() {
bass.SetFrequency(Proportion(tone[0], BNC_MAX_PARAM, 3000) + 3000);
}
Expand Down
44 changes: 19 additions & 25 deletions software/o_c_REV/HEM_DrumMap.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
class DrumMap : public HemisphereApplet {
public:

static constexpr int MAX_VAL = 255;

const char* applet_name() {
return "DrumMap";
}
Expand All @@ -51,18 +53,18 @@ public:

switch (cv_mode) {
case 0:
Modulate(_fill[0], 0, 0, 255);
Modulate(_fill[1], 1, 0, 255);
Modulate(_fill[0], 0, 0, MAX_VAL);
Modulate(_fill[1], 1, 0, MAX_VAL);
break;

case 1:
Modulate(_x, 0, 0, 255);
Modulate(_y, 1, 0, 255);
Modulate(_x, 0, 0, MAX_VAL);
Modulate(_y, 1, 0, MAX_VAL);
break;

case 2:
Modulate(_fill[0], 0, 0, 255);
Modulate(_chaos, 1, 0, 255);
Modulate(_fill[0], 0, 0, MAX_VAL);
Modulate(_chaos, 1, 0, MAX_VAL);
break;
}

Expand All @@ -80,7 +82,7 @@ public:
// accent on ch 1 will be for whatever part ch 0 is set to
uint8_t part = (ch == 1 && mode[ch] == 3) ? mode[0] : mode[ch];
int level = ReadDrumMap(step, part, _x, _y);
level = constrain(level + randomness[part], 0, 255);
level = constrain(level + randomness[part], 0, MAX_VAL);
// use ch 0 fill if ch 1 is in accent mode
uint8_t threshold = (ch == 1 && mode[ch] == 3) ? ~_fill[0] : ~_fill[ch];
if (level > threshold) {
Expand Down Expand Up @@ -160,21 +162,21 @@ public:
break;
// fill
case 2:
fill[0] = constrain(fill[0] + (direction * accel), 0, 255);
fill[0] = constrain(fill[0] + (direction * accel), 0, MAX_VAL);
break;
case 3:
fill[1] = constrain(fill[1] + (direction * accel), 0, 255);
fill[1] = constrain(fill[1] + (direction * accel), 0, MAX_VAL);
break;
// x/y
case 4:
x = constrain(x + (direction * accel), 0, 255);
x = constrain(x + (direction * accel), 0, MAX_VAL);
break;
case 5:
y = constrain(y + (direction * accel), 0, 255);
y = constrain(y + (direction * accel), 0, MAX_VAL);
break;
// chaos
case 6:
chaos = constrain(chaos + (direction * accel), 0, 255);
chaos = constrain(chaos + (direction * accel), 0, MAX_VAL);
break;
// cv assign
case 7:
Expand Down Expand Up @@ -300,22 +302,22 @@ private:

// fill
gfxPrint(1,25,"F");
DrawKnobAt(9,25,20,_fill[0],cursor == 2);
DrawSlider(9,25,20,_fill[0], MAX_VAL, cursor == 2);
// don't show fill for channel b if it is an accent mode
if (mode[1] < 3) {
gfxPrint(32,25,"F");
DrawKnobAt(40,25,20,_fill[1],cursor == 3);
DrawSlider(40,25,20,_fill[1], MAX_VAL, cursor == 3);
}

// x & y
gfxPrint(1,35,"X");
DrawKnobAt(9,35,20,_x,cursor == 4);
DrawSlider(9,35,20,_x, MAX_VAL, cursor == 4);
gfxPrint(32,35,"Y");
DrawKnobAt(40,35,20,_y,cursor == 5);
DrawSlider(40,35,20,_y, MAX_VAL, cursor == 5);

// chaos
gfxPrint(1,45,"CHAOS");
DrawKnobAt(32,45,28,_chaos,cursor == 6);
DrawSlider(32,45,28,_chaos, MAX_VAL, cursor == 6);

// step count in header
gfxPrint((step < 9 ? 49 : 43),2,step+1);
Expand Down Expand Up @@ -344,14 +346,6 @@ private:

}

void DrawKnobAt(byte x, byte y, byte len, byte value, bool is_cursor) {
byte w = Proportion(value, 255, len-1); // minus 1 because width is 2
byte p = is_cursor ? 1 : 3;
gfxDottedLine(x, y + 4, x + len, y + 4, p);
gfxRect(x + w, y, 2, 8);
if (is_cursor && EditMode()) gfxInvert(x, y, len+1, 8);
}

void Reset() {
step = 0;
}
Expand Down
14 changes: 3 additions & 11 deletions software/o_c_REV/HEM_PolyDiv.ino
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ private:
ForEachChannel(ch) {
for(int i = 0; i < NUM_STEPS; i++) {
gfxPrint(1 + 31*ch, 15 + (i*10), div_seq[ch].steps[i]);
DrawKnobAt(14 + 31*ch, 15 + (i*10), 12, div_seq[ch].steps[i], cursor == i+ch*NUM_STEPS);
if (div_seq[ch].step_index == i)
gfxIcon(27 + 31*ch, 15 + i*10, LEFT_BTN_ICON);
DrawSlider(14 + 31*ch, 15 + (i*10), 14, div_seq[ch].steps[i], MAX_DIV, cursor == i+ch*NUM_STEPS);

if (div_seq[ch].step_index == i)
gfxIcon(28 + 31*ch, 15 + i*10, LEFT_BTN_ICON);
}
// flash division when triggered
if (pulse_animation[ch] > 0 && div_seq[ch].step_index >= 0) {
Expand All @@ -193,14 +193,6 @@ private:
}
}

void DrawKnobAt(uint8_t x, uint8_t y, uint8_t len, uint8_t value, bool is_cursor) {
uint8_t p = is_cursor ? 1 : 3;
uint8_t w = Proportion(value, MAX_DIV, len-1);
gfxDottedLine(x, y + 3, x + len, y + 3, p);
gfxRect(x + w, y, 2, 7);
if (EditMode() && is_cursor) gfxInvert(x-1, y, len+3, 7);
}

};


Expand Down
32 changes: 12 additions & 20 deletions software/o_c_REV/HEM_ProbabilityDivider.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

#include "HSProbLoopLinker.h" // singleton for linking ProbDiv and ProbMelo

#define HEM_PROB_DIV_MAX_WEIGHT 15
#define HEM_PROB_DIV_MAX_LOOP_LENGTH 32

class ProbabilityDivider : public HemisphereApplet {
public:

static constexpr uint8_t MAX_WEIGHT = 15;
static constexpr uint8_t MAX_LOOP_LENGTH = 32;

enum ProbDivCursor {
WEIGHT1, WEIGHT2, WEIGHT4, WEIGHT8,
LOOP_LENGTH,
Expand Down Expand Up @@ -56,7 +56,7 @@ public:
// CV 1 control over loop length
loop_length_mod = loop_length;
if (DetentedIn(0)) {
Modulate(loop_length_mod, 0, 0, HEM_PROB_DIV_MAX_LOOP_LENGTH);
Modulate(loop_length_mod, 0, 0, MAX_LOOP_LENGTH);
// TODO: regenerate if changing from 0?
}

Expand Down Expand Up @@ -147,13 +147,13 @@ public:
}

switch ((ProbDivCursor)cursor) {
case WEIGHT1: weight_1 = constrain(weight_1 + direction, 0, HEM_PROB_DIV_MAX_WEIGHT); break;
case WEIGHT2: weight_2 = constrain(weight_2 + direction, 0, HEM_PROB_DIV_MAX_WEIGHT); break;
case WEIGHT4: weight_4 = constrain(weight_4 + direction, 0, HEM_PROB_DIV_MAX_WEIGHT); break;
case WEIGHT8: weight_8 = constrain(weight_8 + direction, 0, HEM_PROB_DIV_MAX_WEIGHT); break;
case WEIGHT1: weight_1 = constrain(weight_1 + direction, 0, MAX_WEIGHT); break;
case WEIGHT2: weight_2 = constrain(weight_2 + direction, 0, MAX_WEIGHT); break;
case WEIGHT4: weight_4 = constrain(weight_4 + direction, 0, MAX_WEIGHT); break;
case WEIGHT8: weight_8 = constrain(weight_8 + direction, 0, MAX_WEIGHT); break;
case LOOP_LENGTH: {
int old = loop_length;
loop_length = constrain(loop_length + direction, 0, HEM_PROB_DIV_MAX_LOOP_LENGTH);
loop_length = constrain(loop_length + direction, 0, MAX_LOOP_LENGTH);
if (old == 0 && loop_length > 0) {
// seed loop
GenerateLoop(true);
Expand Down Expand Up @@ -209,7 +209,7 @@ private:
int weight_4;
int weight_8;
int loop_length, loop_length_mod;
int loop[HEM_PROB_DIV_MAX_LOOP_LENGTH];
int loop[MAX_LOOP_LENGTH];
int loop_index;
int loop_step;
// used to keep track of reseed cv inputs so it only reseeds on rising edge
Expand All @@ -231,7 +231,7 @@ private:
for(int i = 0; i < 4; i++) {
gfxPrint(1, 15 + (i*10), "/");
gfxPrint(divs[i]);
DrawKnobAt(20, 15 + (i*10), 40, *weights[i], cursor == i);
DrawSlider(20, 15 + (i*10), 40, *weights[i], MAX_WEIGHT, cursor == i);

// flash division when triggered
if (pulse_animation > 0 && skip_steps == divs[i]) {
Expand All @@ -257,14 +257,6 @@ private:
}
}

void DrawKnobAt(byte x, byte y, byte len, byte value, bool is_cursor) {
byte p = is_cursor ? 1 : 3;
byte w = Proportion(value, HEM_PROB_DIV_MAX_WEIGHT, len-1);
gfxDottedLine(x, y + 3, x + len, y + 3, p);
gfxRect(x + w, y, 2, 7);
if (EditMode() && is_cursor) gfxInvert(x-1, y, len+3, 7);
}

int GetNextWeightedDiv() {
int total_weights = 0;

Expand All @@ -290,7 +282,7 @@ private:
}
int index = 0;
int counter = 0;
while (counter < HEM_PROB_DIV_MAX_LOOP_LENGTH) {
while (counter < MAX_LOOP_LENGTH) {
int div = GetNextWeightedDiv();
if (div == 0) {
break;
Expand Down
8 changes: 8 additions & 0 deletions software/o_c_REV/HemisphereApplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ class HemisphereApplet {
//gfxLine(0, 11, 62, 11);
}

void DrawSlider(uint8_t x, uint8_t y, uint8_t len, uint8_t value, uint8_t max_val, bool is_cursor) {
uint8_t p = is_cursor ? 1 : 3;
uint8_t w = Proportion(value, max_val, len-1);
gfxDottedLine(x, y + 4, x + len, y + 4, p);
gfxRect(x + w, y, 2, 8);
if (EditMode() && is_cursor) gfxInvert(x-1, y, len+3, 8);
}

//////////////// Offset I/O methods
////////////////////////////////////////////////////////////////////////////////
int In(int ch) {
Expand Down

0 comments on commit d01f754

Please sign in to comment.