#include #include #include #include #include #define BUFFER_START 0x2000u //#define BUFFER_SIZE 960u #define BUFFER_SIZE 10 #define BUFFER_END (BUFFER_START + BUFFER_SIZE) #define RAM_SIZE (BUFFER_END + 1) #define BOOL int #define FALSE 0 #define TRUE 1 BOOL overrun = FALSE; BOOL overrunForReader = FALSE; BOOL underrun = TRUE; char picRam[RAM_SIZE] = { 0 }; char* bufferStart = &(picRam[BUFFER_START]); char* bufferEnd = &(picRam[BUFFER_END - 1]); char* writePtr = 0; char* readPtr = 0; BOOL currentByteLoaded = FALSE; char spiData = 0; void debugPrint() { printf("wPtr: %d, rPtr: %d, cbl: %d, ovr: %d, udr: %d\n", (int) (writePtr - bufferStart), (int) (readPtr - bufferStart), (int) currentByteLoaded, (int) overrun, (int) underrun); int i; for(i = 0; i < BUFFER_SIZE; ++i) { printf("%02x ", bufferStart[i]); } printf("\n"); } int getch(void) { int ch; struct termios oldt; struct termios newt; tcgetattr(STDIN_FILENO, &oldt); /*store old settings */ newt = oldt; /* copy old settings to new settings */ newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */ tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */ ch = getchar(); /* standard getchar call */ tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */ return ch; /*return received char */ } void incrementWriteIdx() { if (!underrun && (writePtr == readPtr)) { overrun = TRUE; overrunForReader = TRUE; } else { if (!overrun) { writePtr++; writePtr = writePtr > bufferEnd ? bufferStart : writePtr; } overrun = FALSE; overrunForReader = FALSE; underrun = FALSE; } } void incrementReadIdx() { if (!overrunForReader && (readPtr == writePtr)) { underrun = TRUE; } else { readPtr++; readPtr = readPtr > bufferEnd ? bufferStart : readPtr; underrun = FALSE; overrunForReader = FALSE; // overrun = FALSE; } } void beginRead() { if (!currentByteLoaded) { incrementReadIdx(); } if (!underrun) { spiData = *readPtr; printf("SPI out: %02x\n", (int)spiData); } currentByteLoaded = TRUE; } void endRead() { currentByteLoaded = FALSE; } void storeSample() { static char value = 0; value++; incrementWriteIdx(); if (!overrun) { *writePtr = value; } } void triggerByteTransferred() { if (spiData == 0) { endRead(); beginRead(); } else if (spiData == 'r') { beginRead(); } else { printf("%s", "I don't like you!"); } } int main() { writePtr = bufferStart; readPtr = bufferStart; char key; for(;;) { /*ssize_t len = read(0, &key, 1); if (len < 0 && (errno == EINTR || errno == EAGAIN)) { continue; } else if (len == 0) { return 0; } else if (len != 1) { abort(); }*/ debugPrint(); key = getch(); switch(key) { case '+': storeSample(); break; case '0': spiData = 0; triggerByteTransferred(); break; case 'q': return 0; default: spiData = key; triggerByteTransferred(); break; } } return 0; }