summaryrefslogtreecommitdiff
path: root/tools/idm-ringbuffer/ringbuffer.c
blob: ea912b302e71f969e6c354e3a085989a4cc4867b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <termios.h>

#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;
}