2. The Cell
The core building block of Trig is the cell. A cell is mainly comprised of 2 32-bit words, which will eventually be displayed on a 8x8 grid. The topmost word holds the data, the other will hold the command.
typedef struct trig_cell trig_cell;
struct trig_cell {
uint32_t cmd;
uint32_t data;
};
Get these values directly with trig_cell_cmd
and
trig_cell_data
.
uint32_t trig_cell_cmd(trig_cell *c);
uint32_t trig_cell_data(trig_cell *c);
uint32_t trig_cell_cmd(trig_cell *c)
{
return c->cmd;
}
uint32_t trig_cell_data(trig_cell *c)
{
return c->data;
}
The main way to program cells is to set bits. This is done by determining the word and the local XY position. Word 0 is the top word. Word 1 is the bottom word. This is how programming will be done on the monome (bit-by-bit, literally).
void trig_cell_set(trig_cell *c, int w, int pos, int s);
void trig_cell_set(trig_cell *c, int w, int pos, int s)
{
uint32_t *x;
if (w) {
x = &c->data;
} else {
x = &c->cmd;
}
if (s) {
*x |= 1 << pos;
} else {
*x &= ~(1 << pos);
}
}
In practice, bits will be toggled, not set. Do this with
trig_cell_tog
. It's similar to trig_cell_set
, except
that the state value isn't supplied.
void trig_cell_tog(trig_cell *c, int w, int pos);
void trig_cell_tog(trig_cell *c, int w, int pos)
{
uint32_t *x;
if (w) {
x = &c->data;
} else {
x = &c->cmd;
}
*x ^= 1 << pos;
}
Printing a cell is useful for debugging. It will also provide a good preview for what a cell will look like displayed on a monome.
void trig_cell_print(trig_cell *c);
void trig_cell_print(trig_cell *c)
{
int i;
for (i = 0; i < 64; i++) {
int s;
uint32_t byte;
int pos;
if (i < 32) {
byte = c->cmd;
pos = i;
} else {
byte = c->data;
pos = i - 32;
}
if (i == 32) {
printf("\n");
}
s = (byte >> pos) & 1;
if (s) {
printf("#");
} else {
printf("-");
}
if (((i + 1) % 8) == 0) {
printf("\n");
}
}
printf("\n");
}
prev | home | next