123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
-
- #include <stdlib.h>
- #include <stdint.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "uart.h"
- #include "cube.h"
- #include "time.h"
- #include "audio.h"
- #include "memLayer.h"
-
- #ifndef F_CPU
- #define F_CPU 16000000L
- #endif
-
- #define OK 0x42
- #define ERROR 0x23
-
- void serialHandler(char c);
- void recieveAnimations(void);
- void transmitAnimations(void);
- uint8_t audioModeSelected(void);
- inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
- inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
- void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
- void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
-
- uint8_t refreshAnimationCount = 1;
-
- int main(void) {
- unsigned int character;
- uint8_t *audioData;
- uint8_t **imageData;
- uint8_t i;
- uint64_t lastTimeChecked;
- uint8_t audioMode;
- uint16_t count;
-
- DDRD = 0xFF;
- DDRB = 0xFE;
- DDRC = 0xFF;
- DDRA = 0xFF;
-
- imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
- for (i = 0; i < 8; i++) {
- imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
- }
-
- init();
- uart_init(UART_BAUD_SELECT(19200, 16000000L));
- initSystemTimer();
-
- sei();
-
- audioMode = audioModeSelected();
- lastTimeChecked = getSystemTime();
-
- while (1) {
- if(audioMode) {
-
- audioData = getAudioData();
- visualizeAudioData(audioData, imageData);
- setImage(imageData);
- free(audioData);
- while(!isFinished());
- } else {
-
-
-
-
-
-
- character = uart_getc();
- if (!(character & 0xFF00)) {
- serialHandler((char)(character & 0x00FF));
- }
-
- if (refreshAnimationCount) {
- count = getAnimationCount();
- refreshAnimationCount = 0;
- }
- }
-
- if ((getSystemTime() - lastTimeChecked) > 1000) {
-
- audioMode = audioModeSelected();
- lastTimeChecked = getSystemTime();
- }
- }
-
- close();
- return 0;
- }
-
- void serialHandler(char c) {
-
-
-
-
-
- switch(c) {
- case OK:
-
- uart_putc(OK);
- break;
-
- case 'd': case 'D':
- clearMem();
- uart_putc(OK);
- break;
-
- case 'g': case 'G':
- transmitAnimations();
- break;
-
- case 's': case 'S':
- recieveAnimations();
- break;
-
- default:
- uart_putc(ERROR);
- break;
- }
- }
-
- void recieveAnimations() {
- uint8_t animCount, a, frameCount, f, i;
- uint16_t completeCount = 0, character;
- uint8_t frame[65];
-
- uart_putc(OK);
-
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
-
- animCount = (uint8_t)(character & 0x00FF);
- uart_putc(OK);
-
- for (a = 0; a < animCount; a++) {
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
-
- frameCount = (uint8_t)(character & 0x00FF);
- uart_putc(OK);
-
- for (f = 0; f < frameCount; f++) {
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
-
- frame[64] = (uint8_t)(character & 0x00FF);
- uart_putc(OK);
-
- for (i = 0; i < 64; i++) {
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
-
- frame[i] = (uint8_t)(character & 0x00FF);
- }
- uart_putc(OK);
-
- setFrame(completeCount++, frame);
- }
- }
-
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
- character = uart_getc();
- while (character & 0xFF00) {
- character = uart_getc();
- }
- uart_putc(OK);
- setAnimationCount(completeCount);
- refreshAnimationCount = 1;
- }
-
- #define CAF(x) (x % 256)
- #define CAS(x) (x / 256)
- #define CALCANIMATIONS(x) ((CAF(x) == 0) ? (CAS(x)) : (CAS(x) + 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;
- }
-
- uart_putc(OK);
- uart_putc(animationsToGo);
- while ((character = uart_getc()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- return;
- }
-
- for (a = 0; a < animationsToGo; a++) {
- if (framesToGo > 255) {
- fMax = 255;
- } else {
- fMax = framesToGo;
- }
-
- uart_putc(fMax);
- while ((character = uart_getc()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- return;
- }
-
- for (f = 0; f < fMax; f++) {
- frame = getFrame(f + (255 * a));
-
- uart_putc(frame[64]);
- while ((character = uart_getc()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- free(frame);
- return;
- }
-
- for (i = 0; i < 64; i++) {
- uart_putc(frame[i]);
- }
- while ((character = uart_getc()) & 0xFF00);
- if ((character & 0x00FF) != OK) {
- free(frame);
- return;
- }
-
- free(frame);
- }
- framesToGo -= fMax;
- }
-
- uart_putc(OK);
- uart_putc(OK);
- uart_putc(OK);
- uart_putc(OK);
-
- while ((character = uart_getc()) & 0xFF00);
-
- }
-
-
- uint8_t audioModeSelected(void) {
-
- uint64_t startTime = getSystemTime();
- uint8_t startState = PINB & (1 << PB0);
-
- while((getSystemTime() - startTime) < 10);
-
- if ((PINB & (1 << PB0)) != startState) {
- return audioModeSelected();
- } else {
- return startState;
- }
- }
-
- inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
- buf[z][y] |= (1 << x);
- }
-
- inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
- buf[z][y] &= ~(1 << x);
- }
-
- void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
- uint8_t i;
- for (i = 0; i < length; i++) {
- buf[i] = d;
- }
- }
-
- void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
- uint8_t i = 0;
- for (; i < height; i++) {
- setPixelBuffer(x, i, z, buf);
- }
- }
-
- void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
- uint8_t i;
- for (i = 0; i < 8; i++) {
- setBuffer(0, imageData[i], 8);
- }
-
-
-
-
-
-
- #define FACTOR 31
-
-
-
- setRow(0, 0, (audioData[0] / FACTOR), imageData);
- setRow(0, 1, (audioData[0] / FACTOR), imageData);
- setRow(1, 0, (audioData[0] / FACTOR), imageData);
-
- setRow(0, 2, (audioData[1] / FACTOR), imageData);
- setRow(0, 3, (audioData[1] / FACTOR), imageData);
- setRow(1, 1, (audioData[1] / FACTOR), imageData);
- setRow(1, 2, (audioData[1] / FACTOR), imageData);
- setRow(2, 0, (audioData[1] / FACTOR), imageData);
- setRow(2, 1, (audioData[1] / FACTOR), imageData);
-
- setRow(0, 4, (audioData[2] / FACTOR), imageData);
- setRow(0, 5, (audioData[2] / FACTOR), imageData);
- setRow(1, 3, (audioData[2] / FACTOR), imageData);
- setRow(1, 4, (audioData[2] / FACTOR), imageData);
- setRow(2, 2, (audioData[2] / FACTOR), imageData);
- setRow(2, 3, (audioData[2] / FACTOR), imageData);
- setRow(3, 0, (audioData[2] / FACTOR), imageData);
- setRow(3, 1, (audioData[2] / FACTOR), imageData);
- setRow(3, 2, (audioData[2] / FACTOR), imageData);
- setRow(4, 0, (audioData[2] / FACTOR), imageData);
- setRow(4, 1, (audioData[2] / FACTOR), imageData);
-
- setRow(0, 6, (audioData[3] / FACTOR), imageData);
- setRow(0, 7, (audioData[3] / FACTOR), imageData);
- setRow(1, 5, (audioData[3] / FACTOR), imageData);
- setRow(1, 6, (audioData[3] / FACTOR), imageData);
- setRow(2, 4, (audioData[3] / FACTOR), imageData);
- setRow(2, 5, (audioData[3] / FACTOR), imageData);
- setRow(3, 3, (audioData[3] / FACTOR), imageData);
- setRow(3, 4, (audioData[3] / FACTOR), imageData);
- setRow(4, 2, (audioData[3] / FACTOR), imageData);
- setRow(4, 3, (audioData[3] / FACTOR), imageData);
- setRow(5, 0, (audioData[3] / FACTOR), imageData);
- setRow(5, 1, (audioData[3] / FACTOR), imageData);
- setRow(5, 2, (audioData[3] / FACTOR), imageData);
- setRow(6, 0, (audioData[3] / FACTOR), imageData);
- setRow(6, 1, (audioData[3] / FACTOR), imageData);
-
- setRow(1, 7, (audioData[4] / FACTOR), imageData);
- setRow(2, 6, (audioData[4] / FACTOR), imageData);
- setRow(2, 7, (audioData[4] / FACTOR), imageData);
- setRow(3, 5, (audioData[4] / FACTOR), imageData);
- setRow(3, 6, (audioData[4] / FACTOR), imageData);
- setRow(4, 4, (audioData[4] / FACTOR), imageData);
- setRow(4, 5, (audioData[4] / FACTOR), imageData);
- setRow(5, 3, (audioData[4] / FACTOR), imageData);
- setRow(5, 4, (audioData[4] / FACTOR), imageData);
- setRow(6, 2, (audioData[4] / FACTOR), imageData);
- setRow(6, 3, (audioData[4] / FACTOR), imageData);
- setRow(7, 0, (audioData[4] / FACTOR), imageData);
- setRow(7, 1, (audioData[4] / FACTOR), imageData);
-
- setRow(3, 7, (audioData[5] / FACTOR), imageData);
- setRow(4, 6, (audioData[5] / FACTOR), imageData);
- setRow(4, 7, (audioData[5] / FACTOR), imageData);
- setRow(5, 5, (audioData[5] / FACTOR), imageData);
- setRow(5, 6, (audioData[5] / FACTOR), imageData);
- setRow(6, 4, (audioData[5] / FACTOR), imageData);
- setRow(6, 5, (audioData[5] / FACTOR), imageData);
- setRow(7, 2, (audioData[5] / FACTOR), imageData);
- setRow(7, 3, (audioData[5] / FACTOR), imageData);
- setRow(7, 4, (audioData[5] / FACTOR), imageData);
-
- setRow(5, 7, (audioData[6] / FACTOR), imageData);
- setRow(6, 6, (audioData[6] / FACTOR), imageData);
- setRow(6, 7, (audioData[6] / FACTOR), imageData);
- setRow(7, 5, (audioData[6] / FACTOR), imageData);
- setRow(7, 6, (audioData[6] / FACTOR), imageData);
- setRow(7, 7, (audioData[6] / FACTOR), imageData);
- }
|