# HG changeset patch # User František Kučera # Date 1513889726 -3600 # Node ID 157bc062efa7fac5216a921de0731a03338b5286 # Parent d4b2ec9ef8bcc32d9f4c47023aae1887fa95b895 template / generic write() function diff -r d4b2ec9ef8bc -r 157bc062efa7 c++/rgb-assembler/rgb-assembler.cpp --- a/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 21:49:15 2017 +0100 +++ b/c++/rgb-assembler/rgb-assembler.cpp Thu Dec 21 21:55:26 2017 +0100 @@ -30,23 +30,13 @@ return *value; } -void writeCommand(command_t * memory, address_t &index, const command_t value) { - // command_t * m = (command_t*) (memory + index); - command_t * m = reinterpret_cast (memory + index); - *m = value; - index += sizeof (value) / sizeof (command_t); -} - -void writeAddress(command_t * memory, address_t &index, const address_t value) { - // command_t * m = (command_t*) (memory + index); - address_t * m = reinterpret_cast (memory + index); - *m = value; - index += sizeof (value) / sizeof (command_t); -} - -void writeSleep(command_t * memory, address_t &index, const sleep_t value) { - // command_t * m = (command_t*) (memory + index); - sleep_t * m = reinterpret_cast (memory + index); +/** + * Writes data to given position in memory and increments the index (position). + */ +template void write(command_t * memory, address_t &index, const T value) { + // TODO: sizeof (command_t) != 1 ? + // T * m = (T*) (memory + index); + T * m = reinterpret_cast (memory + index); *m = value; index += sizeof (value) / sizeof (command_t); } @@ -59,23 +49,21 @@ { address_t a = 0; - writeCommand(memory, a, CMD_SLEEP); - writeSleep(memory, a, 255); - writeCommand(memory, a, CMD_SLEEP); - writeSleep(memory, a, 10); - writeCommand(memory, a, CMD_SLEEP); - writeSleep(memory, a, 255); - writeCommand(memory, a, CMD_GOTO); - writeAddress(memory, a, a + 4); - writeCommand(memory, a, 1); - writeCommand(memory, a, 1); - writeCommand(memory, a, CMD_SLEEP); - writeSleep(memory, a, 255); - writeCommand(memory, a, CMD_END); + write(memory, a, CMD_SLEEP); + write(memory, a, 255); + write(memory, a, CMD_SLEEP); + write(memory, a, 10); + write(memory, a, CMD_SLEEP); + write(memory, a, 255); + write(memory, a, CMD_GOTO); + write(memory, a, a + 4); + write(memory, a, 1); + write(memory, a, 1); + write(memory, a, CMD_SLEEP); + write(memory, a, 255); + write(memory, a, CMD_END); } - - for (address_t i = 0; i < MEMORY_SIZE;) { wprintf(L"command %d = ", i); command_t ch = read(memory, i); @@ -104,8 +92,6 @@ } } - - free(memory); memory = nullptr; wprintf(L"all done\n");