12 typedef uint16_t address_t;
13 typedef uint8_t command_t;
14 typedef uint8_t sleep_t;
15 typedef uint8_t color_t;
16 typedef uint8_t led_t;
18 // TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
20 const address_t MEMORY_SIZE = 1024;
23 * Skip to the given address.
24 * parameter: address_t
26 const command_t CMD_GOTO = 0x70;
29 * Wait given time in ms.
32 const command_t CMD_SLEEP = 0xFF;
36 * parameter: led_t LED number
37 * parameter: color_t red
38 * parameter: color_t green
39 * parameter: color_t blue
41 const command_t CMD_COLOR = 0xAA;
46 const command_t CMD_END = 0xED;
49 * Placeholder for unsupported command.
52 const command_t CMD_INVALID = 0x1;
55 * Reads data on given position in memory and increments the index (position).
57 template<typename T> T read(command_t * memory, address_t &index) {
58 // TODO: for addresses: map higher memory to static hardcoded areas or peripherals
59 // TODO: sizeof (command_t) != 1 ?
60 T * value = reinterpret_cast<T*> (memory + index);
61 index += sizeof (*value) / sizeof (command_t);
66 * Writes data to given position in memory and increments the index (position).
68 template<typename T> void write(command_t * memory, address_t &index, const T value) {
69 // TODO: sizeof (command_t) != 1 ?
70 // T * m = (T*) (memory + index);
71 T * m = reinterpret_cast<T*> (memory + index);
73 index += sizeof (value) / sizeof (command_t);
76 int main(int argc, char* argv[]) {
78 setlocale(LC_ALL, "");
80 command_t * memory = (command_t*) malloc(MEMORY_SIZE);
84 write<command_t>(memory, a, CMD_SLEEP);
85 write<sleep_t>(memory, a, 255);
86 write<command_t>(memory, a, CMD_SLEEP);
87 write<sleep_t>(memory, a, 10);
88 write<command_t>(memory, a, CMD_SLEEP);
89 write<sleep_t>(memory, a, 255);
90 write<command_t>(memory, a, CMD_GOTO);
91 write<address_t>(memory, a, a + 4);
92 write<command_t>(memory, a, CMD_INVALID);
93 write<command_t>(memory, a, CMD_INVALID);
94 write<command_t>(memory, a, CMD_SLEEP);
95 write<sleep_t>(memory, a, 255);
96 write<command_t>(memory, a, CMD_COLOR);
97 write<led_t>(memory, a, 23);
98 write<color_t>(memory, a, 0);
99 write<color_t>(memory, a, 200);
100 write<color_t>(memory, a, 255);
101 write<command_t>(memory, a, CMD_END);
104 for (address_t i = 0; i < MEMORY_SIZE;) {
105 wprintf(L"command %*d = ", 4, i);
106 command_t ch = read<command_t>(memory, i);
107 wprintf(L"%02X ", ch);
112 i = read<address_t>(memory, i);
113 wprintf(L"GOTO %*d\n", 5, i);
118 sleep_t delay = read<sleep_t>(memory, i);
119 wprintf(L"SLEEP %*d ms\n", 4, delay);
120 this_thread::sleep_for(chrono::milliseconds(delay));
125 led_t led = read<led_t>(memory, i);
126 color_t r = read<color_t>(memory, i);
127 color_t g = read<color_t>(memory, i);
128 color_t b = read<color_t>(memory, i);
129 wprintf(L"COLOR %02X %02X %02X → %d\n", r, g, b, led);
140 wprintf(L"invalid command\n");
148 wprintf(L"all done\n");