Skip to content

Commit c9b7ddf

Browse files
committed
small updates
1 parent defc471 commit c9b7ddf

File tree

4 files changed

+219
-97
lines changed

4 files changed

+219
-97
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# pico_test_synth
22

3-
A simple [Raspberry Pi Pico RP2040](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html)-based
4-
synth to experiment with [`synthio`](https://github.com/todbot/circuitpython-synthio-tricks).
3+
A simple
4+
[Raspberry Pi Pico RP2040](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html)-based
5+
synth to experiment with [`synthio`](https://github.com/todbot/circuitpython-synthio-tricks) and [Mozzi](https://github.com/sensorium/Mozzi).
6+
7+
<a href="https://www.tindie.com/stores/todbot/products/pico_test_synth?ref=offsite_badges&utm_source=sellers_todbot&utm_medium=badges&utm_campaign=badge_small"><img src="https://d2ss6ovg47m0r5.cloudfront.net/badges/tindie-smalls.png" alt="I sell on Tindie" width="200" height="55"></a>
8+
59

610
<img src="./docs/pico_test_synth_case1_web.jpg" width=500>
711
<img src="./docs/pico_test_synth2.jpg" width=500>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#ifndef SYNTHPATCH_H
2+
#define SYNTHPATCH_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <typeinfo>
8+
9+
enum ParamType { Range,
10+
Choice };
11+
12+
#define ParamRange(name, desc, val, ... /* min, max, fmt */) \
13+
Param(Range, name, desc, val, ##__VA_ARGS__)
14+
15+
#define ParamChoice(name, desc, val, choice_strs, num_choices) \
16+
Param(Choice, name, desc, val, 0, num_choices, "%s", choice_strs)
17+
18+
/*!
19+
* @brief A Param is a value that can be set/get, with a range (if type Range)
20+
* or from a list of choices (if type Choice)
21+
*/
22+
class Param {
23+
public:
24+
/*!
25+
* @brief Instantiate a Param from another Param
26+
* @param p a Param object
27+
*/
28+
Param(Param* p) {
29+
type = p->type;
30+
name = p->name;
31+
desc = p->desc;
32+
val = p->val;
33+
minval = p->minval;
34+
maxval = p->maxval;
35+
fmt = p->fmt;
36+
choice_strs = p->choice_strs;
37+
}
38+
39+
/*!
40+
* @brief Instantiates a Param object
41+
* @param aname
42+
* name of this Param
43+
* @param aval
44+
* the current value of this Param
45+
*/
46+
Param(ParamType atype,
47+
const char* aname, const char* adesc, float aval, float aminval = 0, float amaxval = 255,
48+
const char* afmt = "%.0f", const char** achoice_strs = NULL) {
49+
type = atype;
50+
name = aname;
51+
desc = adesc;
52+
val = aval;
53+
minval = aminval;
54+
maxval = amaxval;
55+
fmt = afmt;
56+
choice_strs = achoice_strs;
57+
}
58+
59+
// return the 0.0-1.0 "percentage" the val is in between minval and maxval
60+
float percent() {
61+
return (val - minval) / (maxval - minval);
62+
}
63+
64+
// set val based on the "percentage" it should be between minval and maxval
65+
void set_percent(float pct) {
66+
val = pct * (maxval - minval) + minval;
67+
}
68+
69+
// turn the Param's value into a string,
70+
void val_to_str(char* s, int slen) {
71+
if (choice_strs != NULL) { // it's a ParamChoice
72+
snprintf(s, slen, "%s", choice_strs[(int)val]);
73+
} else {
74+
snprintf(s, slen, fmt, val); // else ParamRange
75+
}
76+
}
77+
78+
ParamType type; // type of this param, Range or Choice
79+
const char* name; // name of param
80+
const char* desc;
81+
float val; // value of param, even if a byte value from 0-255
82+
float minval; // the minimum allowed value
83+
float maxval; // maximum allowed value, or num_choices
84+
const char* fmt; // how to format the value as a string
85+
const char** choice_strs; // list of choices, if it's a ParamChoice
86+
};
87+
88+
89+
/*!
90+
* @brief A Patch is a collection of Params with a name
91+
*/
92+
class Patch {
93+
public:
94+
Patch(const char* aname, Param* aparams, int nparams) {
95+
strncpy(name, aname, 20);
96+
num_params = nparams;
97+
params = (Param*)malloc(sizeof(Param) * nparams);
98+
for (int i = 0; i < nparams; i++) {
99+
params[i] = Param(aparams[i]);
100+
}
101+
}
102+
char name[20];
103+
Param* params;
104+
int num_params;
105+
};
106+
107+
#endif

arduino/hwtest/hwtest2_guitest/SynthUI.h

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef SYNTHUI_H
22
#define SYNTHUI_H
33

4+
#include <stdio.h>
5+
#include <stdarg.h>
46
#include <Adafruit_GFX.h>
57

68
#define KEY_NONE 0 // No key presses are detected
@@ -11,62 +13,17 @@
1113
#define KEY_CANCEL 5 // Cancel key is pressed (navigate to the previous (parent) menu page, exit edit mode without saving the variable, exit context loop if allowed within context's settings)
1214
#define KEY_OK 6 // Ok/Apply key is pressed (toggle bool menu item, enter edit mode of the associated non-bool variable, exit edit mode with saving the variable, execute code associated with button)
1315

14-
class Param {
15-
public:
16-
// construct a Param from another Param
17-
Param(Param* p) {
18-
name = p->name;
19-
val = p->val;
20-
minval = p->minval;
21-
maxval = p->maxval;
22-
fmt = p->fmt;
23-
}
24-
25-
Param(const char* aname, float aval, float aminval = 0, float amaxval = 255, const char* afmt = "%.0f") {
26-
name = aname;
27-
val = aval;
28-
minval = aminval;
29-
maxval = amaxval;
30-
fmt = afmt;
31-
}
32-
// return the 0.0-1.0 "percentage" the val is in between minval and maxval
33-
float percent() {
34-
return (val - minval) / (maxval - minval);
35-
}
36-
// set val based on the "percentage" it should be between minval and maxval
37-
void set_percent(float pct) {
38-
val = pct * (maxval - minval) + minval;
39-
}
40-
41-
const char* name; // name of param
42-
float val; // value of param, even if a byte value from 0-255
43-
float minval; // the minimum allowed value
44-
float maxval; // maximum allowed value
45-
const char* fmt; // how to format the value as a string
46-
};
47-
48-
class Patch {
49-
public:
50-
Patch(const char* aname, Param* aparams, int nparams) {
51-
snprintf(name, 20, aname);
52-
num_params = nparams;
53-
params = (Param*) malloc(sizeof(Param) * nparams);
54-
for(int i=0; i<nparams; i++ ) {
55-
params[i] = Param(aparams[i]);
56-
}
57-
}
58-
char name[20];
59-
Param* params;
60-
int num_params;
61-
};
62-
6316

6417
class SynthUI {
6518
public:
6619
SynthUI(Adafruit_GFX* adisplay) {
6720
display = adisplay;
6821
}
6922

23+
void set_font(const GFXfont* fnt = NULL) {
24+
display->setFont(fnt);
25+
}
26+
7027
/**
7128
* Print text at a given x,y, using an optionally specified font
7229
*/
@@ -77,6 +34,15 @@ class SynthUI {
7734
display->print(str);
7835
}
7936

37+
void print_textf(int x, int y, const char* fmt, ...) {
38+
char buf[80];
39+
va_list va;
40+
va_start(va, fmt);
41+
vsnprintf(buf, 80, fmt, va);
42+
va_end(va);
43+
print_text(x, y, buf);
44+
}
45+
8046
/**
8147
* Draw vertical slider with a "thumb" position identifying value
8248
* thumpos ranges 0.0-1.0

0 commit comments

Comments
 (0)