#include #include #include /* all the message types that the grid supports. The enum values are also the bytes */ enum messages { SET = 0, ALL, MAP, ROW, COL, LEVEL_SET, LEVEL_ALL, LEVEL_MAP, LEVEL_ROW, LEVEL_COL }; /* lengths of all the messages, corresponding to their enum values */ int length[10] = {3, 1, 10, 3, 3, 3, 1, 66, 10, 10}; /* message */ char msg[68]; /* buffer for read characters. getchar() is an int not a char! */ int c; int main(int argc, char *argv[]) { if(argc != 2) { fprintf(stderr, "Please provide a serial port.\n"); } /* make stdin unbuffered */ setbuf(stdin, NULL); /* connect to monome */ monome_t *monome; if( !(monome = monome_open(argv[1], "8000")) ) return -1; /* in a loop (to recieve every character) */ for(;;) { /* get a character. If it isn't EOF, it's the message type */ c = getchar(); if(c != EOF) { msg[0] = c; } else { return 0; } /* get more characters for every argument of the message type, and populate the rest of msg */ for(int i = 1; i < length[(int)msg[0]]+1; i++) { c = getchar(); if(c != EOF) { msg[i] = c; } else { return 0; } } switch(msg[0]) { /* switch on message type, reading msg and sending the appropriate grid command. */ case SET: monome_led_set(monome, msg[1], msg[2], msg[3]); break; case ALL: monome_led_all(monome, msg[1]); break; case MAP: /* this one is a bit more complex. Copies the 8 map values out of the message to pass into the function. */ int map_data[8]; memcpy(map_data, &msg[3], 8 * sizeof(int)); monome_led_map(monome, msg[1], msg[2], (const uint8_t*)map_data); break; case ROW: /* row and col are the same. they take single bytes of data as arrays, for some reason */ int row_data[0]; memcpy(row_data, &msg[3], sizeof(char)); monome_led_row(monome, msg[1], msg[2], 1, (const uint8_t*)row_data); break; case COL: int col_data[0]; memcpy(col_data, &msg[3], sizeof(char)); monome_led_col(monome, msg[1], msg[2], 1, (const uint8_t*)col_data); break; case LEVEL_SET: monome_led_level_set(monome, msg[1], msg[2], msg[3]); break; case LEVEL_ALL: monome_led_level_all(monome, msg[1]); break; case LEVEL_MAP: int lmap_data[64]; memcpy(lmap_data, &msg[3], 64 * sizeof(char)); monome_led_level_map(monome, msg[1], msg[2], (const uint8_t*)lmap_data); break; case LEVEL_ROW: int lrow_data[8]; memcpy(lrow_data, &msg[3], 8 * sizeof(char)); monome_led_level_row(monome, msg[1], msg[2], 1, (const uint8_t*)lrow_data); break; case LEVEL_COL: int lcol_data[8]; memcpy(lcol_data, &msg[3], 8 * sizeof(char)); monome_led_level_col(monome, msg[1], msg[2], 1, (const uint8_t*)lcol_data); break; } } monome_close(monome); return 0; }