123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
-
-
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
- #include <util/atomic.h>
-
- #ifdef DEBUG
- #include "serial.h"
- #include "strings.h"
- #endif
- #include "cube.h"
-
- #ifndef F_CPU
- #define F_CPU 16000000L
- #endif
-
-
-
- #define COUNT 41666
- #define COUNT2 0
-
- volatile uint8_t imgBuffer[8][8];
- volatile uint8_t changedFlag = 0;
- volatile uint8_t imgFlag = 0;
- volatile uint8_t layer = 0;
- volatile uint8_t toggleFlag = 0;
-
- inline void isrCall(void);
-
- volatile uint32_t timesTriggered = 0;
- volatile uint8_t warningDisplayed = 0;
-
- ISR(TIMER1_COMPA_vect) {
- if (toggleFlag >= COUNT2) {
- isrCall();
- toggleFlag = 0;
- timesTriggered++;
- } else {
- toggleFlag++;
- }
- }
-
- uint32_t getTriggerCount(void) {
- return timesTriggered;
- }
-
- void setImage(uint8_t *img) {
- uint8_t i, j;
- ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
- changedFlag = 1;
- imgFlag = 0;
- toggleFlag = 0;
- for (i = 0; i < 8; i++) {
- for (j = 0; j < 8; j++) {
- imgBuffer[j][i] = ~(img[j + (i * 8)]);
- }
- }
- }
- }
-
- void fillBuffer(uint8_t val) {
- uint8_t i, j;
- ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
- changedFlag = 1;
- imgFlag = 0;
- toggleFlag = 0;
- for (i = 0; i < 8; i++) {
- for (j = 0; j < 8; j++) {
- imgBuffer[i][j] = ~(val);
- }
- }
- }
- }
-
- uint8_t isFinished(void) {
- return (imgFlag / 2);
- }
-
- void initCube(void) {
- uint8_t x, y;
-
- TCCR1B |= (1 << CS10) | (1 << WGM12);
- OCR1A = COUNT;
- TIMSK = (1 << OCIE1A);
-
- fillBuffer(0);
- sei();
-
-
- for (x = 0; x < 8; x++) {
- for (y = 0; y < 8; y++) {
- ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
- imgBuffer[x][y] &= ~(0xFF);
- changedFlag = 1;
- imgFlag = 0;
- toggleFlag = COUNT2;
- }
- while(imgFlag < 1);
-
-
- }
- }
-
- cli();
-
- timesTriggered = 0;
- imgFlag = 0;
- toggleFlag = 0;
- }
-
- void close(void) {
- TIMSK &= ~(1 << OCIE1A);
- }
-
-
- inline void setFet(uint8_t data) {
- PORTD = (data & ~(3));
- PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
- }
-
-
- inline void selectLatch(uint8_t latchNr) {
-
- PORTC &= ~(0xFC);
- PORTB &= ~(6);
-
-
- if (latchNr > 1) {
- PORTC |= (1 << latchNr);
- } else {
- PORTB |= (1 << (latchNr + 1));
- }
- }
-
- inline void setLatch(uint8_t latchNr, uint8_t data) {
- selectLatch(latchNr);
- PORTA = data;
- asm volatile("nop"::);
- }
-
- inline void isrCall(void) {
- uint8_t latchCtr = 0;
-
- if (changedFlag != 0) {
-
- layer = 0;
- changedFlag = 0;
- }
- setFet(0);
- for (; latchCtr < 8; latchCtr++) {
- setLatch(latchCtr, imgBuffer[layer][latchCtr]);
- }
- setFet(1 << layer);
-
-
- if (layer < 7) {
- layer++;
- } else {
- layer = 0;
- imgFlag++;
- }
- }
|