123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
-
-
- #ifndef F_CPU
- #define F_CPU 16000000L
- #endif
-
- #define OK 0x42
- #define ERROR 0x23
-
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <avr/wdt.h>
-
- #include "serial.h"
- #include "cube.h"
- #include "time.h"
- #include "audio.h"
- #include "mem.h"
- #include "memLayer.h"
- #include "twi.h"
- #include "strings.h"
- #include "visualizer.h"
- #include "animations.h"
-
- #define NOERROR 0
-
- #define AUDIOERROR 1
-
- #define MEMORYERROR 2
-
- #define MEMORYWRITEERROR 4
-
- #define ISERROR(x, e) ((x) & (e))
-
-
- #define IDLELENGTH 48
-
- void serialHandler(char c);
- void sendAudioData(void);
- void recieveAnimations(void);
- void transmitAnimations(void);
- uint8_t audioModeSelected(void);
- #ifdef DEBUG
- void printErrors(uint8_t e);
- uint8_t selfTest(void);
- void printTime(void);
-
- #include "snake.c"
- #endif
-
- uint8_t shouldRestart = 0;
- uint8_t refreshAnimationCount = 1;
- uint8_t lastButtonState = 0;
- uint8_t mcusr_mirror;
- char buffer[11];
-
- #include "builtInFrames.c"
-
- uint8_t DebugDone = 0;
-
-
- int main(void) {
- uint8_t *audioData = NULL;
- uint8_t *imageData = NULL;
- uint8_t i, length = 0, lastMode;
- uint16_t count;
- uint64_t lastChecked;
- uint8_t idleCounter = 0;
- #ifdef DEBUG
- uint32_t temp;
- #endif
-
- mcusr_mirror = MCUCSR;
- MCUCSR = 0;
- wdt_disable();
-
- DDRA = 0xFF;
- DDRD = 0xFC; DDRB = 24;
- DDRC = 0xFC; DDRB |= 6;
- DDRB &= ~(1 << PB0);
-
- initCube();
- serialInit(25, 8, NONE, 1);
- i2c_init();
- initSystemTimer();
- sei();
-
- wdt_enable(WDTO_1S);
-
- setImage(defaultImageCube);
-
- #ifdef DEBUG
-
-
- setAnimationCount(0);
-
- i = selfTest();
- if (i) {
- serialWriteString(getString(1));
- serialWriteString(itoa(i, buffer, 2));
- serialWrite('\n');
- printErrors(i);
- }
-
- serialWriteString(getString(2));
- serialWriteString(getString(0));
-
-
-
-
- if (mcusr_mirror & WDRF) {
- serialWriteString(getString(31));
- } else if (mcusr_mirror & BORF) {
- serialWriteString(getString(32));
- } else if (mcusr_mirror & EXTRF) {
- serialWriteString(getString(34));
- } else if (mcusr_mirror & JTRF) {
- serialWriteString(getString(35));
- } else if (mcusr_mirror & PORF) {
- serialWriteString(getString(36));
- } else {
- serialWriteString(getString(33));
- }
- #endif
-
- lastMode = audioModeSelected();
- lastChecked = getSystemTime();
-
- i = 0;
- count = getAnimationCount();
-
- while (1) {
-
- if (!shouldRestart) {
- wdt_reset();
- }
-
- if(lastMode) {
-
- if (isFinished()) {
- audioData = getAudioData();
- if (audioData != NULL) {
- simpleVisualization(audioData);
- }
- }
- } else {
- if (refreshAnimationCount) {
-
- count = getAnimationCount();
- refreshAnimationCount = 0;
- i = 0;
- }
-
- if (count > 0) {
- if (isFinished() > length) {
-
- if (i < (count - 1)) {
- i++;
- } else {
- i = 0;
- }
-
- imageData = getFrame(i);
- length = imageData[64];
- setImage(imageData);
- free(imageData);
- }
- } else {
- if (!(DebugDone & 4)) {
- if (DebugDone & 2) {
- if (idleCounter < numOfAnimations()) {
- executeAnimation(idleCounter++);
- } else {
- idleCounter = 0;
- DebugDone &= ~(2);
- }
- } else {
-
- if (isFinished() >= IDLELENGTH) {
-
- setImage(idleAnimation[idleCounter]);
- if (idleCounter < (IDLEANIMATIONCOUNT - 1)) {
- idleCounter++;
- } else {
- idleCounter = 0;
- DebugDone |= 2;
- }
- }
- }
- }
- }
- }
-
- if (serialHasChar()) {
- serialHandler((char)(serialGet()));
- }
-
- #ifdef DEBUG
-
- if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
- temp = getTriggerCount();
- serialWriteString(ltoa(temp, buffer, 10));
- serialWriteString(getString(27));
- serialWriteString(ltoa((temp / 8), buffer, 10));
- serialWriteString(getString(28));
- DebugDone |= 1;
- }
-
-
- if (((getSystemTime() % 60000) == 0) && (getSystemTime() > 0)) {
-
- printTime();
- }
- #endif
-
- if ((getSystemTime() - lastChecked) > 150) {
- lastMode = audioModeSelected();
- lastChecked = getSystemTime();
- }
- }
-
- close();
- return 0;
- }
-
- #ifdef DEBUG
- uint8_t selfTest(void) {
- uint8_t result = NOERROR;
-
- uint8_t *data = getAudioData();
- if (data == NULL) {
- result |= AUDIOERROR;
- } else {
- free(data);
- }
-
- data = memGetBytes(0, 1);
- if (data == NULL) {
- result |= MEMORYERROR;
- } else {
- free(data);
- }
-
- setGeneralPurposeByte(0, 0x23);
- if (getGeneralPurposeByte(0) != 0x23) {
- result |= MEMORYWRITEERROR;
- }
-
- return result;
- }
-
- void printErrors(uint8_t e) {
- if (ISERROR(e, AUDIOERROR)) {
- serialWriteString(getString(3));
- }
- if (ISERROR(e, MEMORYERROR)) {
- serialWriteString(getString(4));
- }
- if (ISERROR(e, MEMORYWRITEERROR)) {
- serialWriteString(getString(5));
- }
- }
-
- void randomAnimation(void) {
- uint8_t *b = (uint8_t *)malloc(64);
- uint8_t x, y, z;
- if (b == NULL) {
- serialWriteString(getString(24));
- return;
- }
- for (x = 0; x < 64; x++) {
- b[x] = 0;
- }
- while(1) {
- setImage(b);
- while(isFinished() == 0);
- x = rand() / 4096;
- y = rand() / 4096;
- z = rand() / 4096;
- b[x + (8 * y)] ^= (1 << z);
-
- if (serialHasChar()) {
- serialWriteString(getString(25));
- free(b);
- serialHandler(serialGet());
- return;
- }
- }
- free(b);
- }
- #endif
-
- void serialHandler(char c) {
-
-
- #ifdef DEBUG
- uint8_t i, y, z;
- serialWrite(c);
- serialWriteString(": ");
- #endif
-
- switch(c) {
- case OK:
- serialWrite(OK);
- break;
-
- case 'h': case 'H': case '?':
- serialWriteString(getString(6));
- #ifdef DEBUG
- serialWriteString(getString(7));
- serialWriteString(getString(8));
- serialWriteString(getString(9));
- serialWriteString(getString(10));
- serialWriteString(getString(11));
- serialWriteString(getString(12));
- serialWriteString(getString(13));
- serialWriteString(getString(26));
- #endif
- break;
-
- case 'd': case 'D':
- clearMem();
- #ifndef DEBUG
- serialWrite(OK);
- #endif
- #ifdef DEBUG
- serialWriteString(getString(29));
- #endif
- break;
-
- #ifndef DEBUG
- case 'g': case 'G':
- transmitAnimations();
- break;
-
- case 's': case 'S':
- recieveAnimations();
- break;
- #endif
-
- case 'v': case 'V':
- serialWriteString(getString(0));
- break;
-
- #ifdef DEBUG
- case 'm': case 'M':
- lastButtonState = !lastButtonState;
- if (lastButtonState) {
- serialWriteString(getString(41));
- } else {
- serialWriteString(getString(40));
- }
- break;
-
- case 'q': case 'Q':
- shouldRestart = 1;
- serialWriteString(getString(30));
- break;
-
- case 'r': case 'R':
- randomAnimation();
- break;
-
- case 't': case 'T':
- printTime();
- break;
-
- case 'a': case 'A':
- sendAudioData();
- break;
-
- case 'c': case 'C':
- serialWriteString(itoa(getAnimationCount(), buffer, 10));
- serialWriteString(getString(15));
- break;
-
- case 'x': case 'X':
-
- serialWriteString(getString(16));
- while (!serialHasChar());
- c = serialGet();
- setAnimationCount(c);
- serialWriteString(itoa(c, buffer, 10));
- serialWriteString(getString(17));
- break;
-
- case 'y': case 'Y':
- setAnimationCount(0x2201);
- serialWriteString(getString(18));
- break;
-
- case 'e': case 'E':
- c = selfTest();
- serialWriteString(getString(19));
- serialWriteString(itoa(c, buffer, 2));
- serialWrite('\n');
- printErrors(c);
- break;
-
- case 'n': case 'N':
- snake();
- break;
-
- case '0':
- fillBuffer(0);
- DebugDone |= 4;
- break;
-
- case '1':
- fillBuffer(0xFF);
- DebugDone |= 4;
- break;
-
- case '3':
- setImage(defaultImageCube);
- DebugDone |= 4;
- break;
-
- case '2':
- DebugDone |= 4;
- fillBuffer(0);
- for (i = 0; i < 64; i++) {
- defaultImageA[i] = 0;
- }
- while(1) {
- for (i = 0; i < 8; i++) {
- for (y = 0; y < 8; y++) {
- defaultImageA[y + (i * 8)] = 0;
- for (z = 0; z < 8; z++) {
- defaultImageA[y + (i * 8)] |= (1 << z);
- setImage(defaultImageA);
- while (isFinished() == 0) {
- if (serialHasChar()) {
- goto killMeForIt;
-
- }
- }
- }
- defaultImageA[y + (i * 8)] = 0;
- }
- }
- }
- break;
- killMeForIt:
- serialGet();
- serialWriteString(getString(25));
- break;
-
- case 'I': case 'i':
- serialWriteString(ltoa(getTriggerCount(), buffer, 10));
- serialWrite('\n');
- break;
- #endif
-
- default:
- serialWrite(ERROR);
- break;
- }
-
- }
-
- #ifdef DEBUG
- void printTime(void) {
- serialWriteString(getString(14));
- serialWriteString(ltoa(getSystemTime(), buffer, 10));
- serialWriteString("ms");
- if (getSystemTime() > 60000) {
- serialWriteString(" (");
- serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
- serialWriteString(" min)");
- }
- if (getSystemTime() > 1000) {
- serialWriteString(" (");
- serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
- itoa(getSystemTime() % 1000, buffer, 10);
- if (buffer[0] != '\0')
- serialWrite('.');
- if (buffer[2] == '\0')
- serialWrite('0');
- if (buffer[1] == '\0')
- serialWrite('0');
- if (buffer[0] != '\0')
- serialWriteString(buffer);
- serialWriteString("s)\n");
- } else {
- serialWrite('\n');
- }
- }
-
- void sendAudioData(void) {
- uint8_t i;
- uint8_t *audioData = getAudioData();
- if (audioData == NULL) {
- serialWriteString(getString(21));
- } else {
- serialWriteString(getString(22));
- for (i = 0; i < 7; i++) {
- serialWrite(i + '0');
- serialWriteString(": ");
- itoa(audioData[i], buffer, 10);
- serialWriteString(buffer);
- serialWrite('\n');
- }
- }
- }
- #endif
-
- #define TRANSTIMEOUT 10000
-
- void recieveAnimations() {
- uint8_t animCount, a, frameCount, f, i, c;
- uint16_t completeCount = 0;
- uint8_t *frame = (uint8_t *)malloc(65);
- uint64_t timestamp = getSystemTime();
-
- serialWrite(OK);
-
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- animCount = c;
- serialWrite(OK);
-
- for (a = 0; a < animCount; a++) {
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- frameCount = c;
- serialWrite(OK);
-
- for (f = 0; f < frameCount; f++) {
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- frame[64] = c;
- serialWrite(OK);
-
- for (i = 0; i < 64; i++) {
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- frame[i] = c;
- }
- serialWrite(OK);
-
- setFrame(completeCount++, frame);
- }
- }
- free(frame);
-
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
- while (!serialHasChar()) {
- if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
- wdt_reset();
- }
- }
- c = serialGet();
-
- serialWrite(OK);
-
- setAnimationCount(completeCount);
- refreshAnimationCount = 1;
- }
-
- void transmitAnimations() {
-
-
-
- uint8_t animationsToGo;
- uint16_t framesToGo = getAnimationCount();
- uint16_t character;
- uint8_t a;
- uint8_t f, fMax, i;
- uint8_t *frame;
-
- if ((framesToGo % 255) == 0) {
- animationsToGo = framesToGo / 255;
- } else {
- animationsToGo = (framesToGo / 255) + 1;
- }
-
- serialWrite(OK);
- serialWrite(animationsToGo);
- while ((character = serialGet()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- return;
- }
-
- for (a = 0; a < animationsToGo; a++) {
- if (framesToGo > 255) {
- fMax = 255;
- } else {
- fMax = framesToGo;
- }
-
- serialWrite(fMax);
- while ((character = serialGet()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- return;
- }
-
- for (f = 0; f < fMax; f++) {
- frame = getFrame(f + (255 * a));
-
- serialWrite(frame[64]);
- while ((character = serialGet()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- free(frame);
- return;
- }
-
- for (i = 0; i < 64; i++) {
- serialWrite(frame[i]);
- }
- while ((character = serialGet()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- free(frame);
- return;
- }
-
- free(frame);
- }
- framesToGo -= fMax;
- }
-
- serialWrite(OK);
- serialWrite(OK);
- serialWrite(OK);
- serialWrite(OK);
-
- while ((character = serialGet()) & 0xFF00);
-
- }
-
- uint8_t audioModeSelected(void) {
-
-
- if (!(PINB & (1 << PB0))) {
-
- if (lastButtonState == 0) {
- lastButtonState = 1;
- } else {
- lastButtonState = 0;
- }
-
- #ifdef DEBUG
- if (lastButtonState) {
- serialWriteString(getString(38));
- } else {
- serialWriteString(getString(39));
- }
- #endif
-
- }
- return lastButtonState;
- }
|