summaryrefslogtreecommitdiff
path: root/tools/idm-ringbuffer/ringbuffer.c
blob: 0baa2a340eb47079b983ca083a9e93eabf0f948e (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *  mur.sat
 *
 *  Somewhen in the year 2012, mur.at will have a nano satellite launched
 *  into a low earth orbit (310 km above the surface of our planet). The
 *  satellite itself is a TubeSat personal satellite kit, developed and
 *  launched by interorbital systems. mur.sat is a joint venture of mur.at,
 *  ESC im Labor and realraum.
 *
 *  Please visit the project hompage at sat.mur.at for further information.
 *
 *
 *  Copyright (C) 2012 Christian Pointner <equinox@mur.at>
 *                     Markus Grüneis <gimpf@gimpf.org>
 *
 *  This file is part of mur.sat.
 *
 *  mur.sat is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  mur.sat is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with mur.sat. If not, see <http://www.gnu.org/licenses/>.
 */

#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 10u
#define BUFFER_END (BUFFER_START + BUFFER_SIZE)
#define RAM_SIZE (BUFFER_END + 1)

#define BOOL int
#define FALSE 0
#define TRUE 1
char picRam[RAM_SIZE] = { 0 };
char* bufferStart = &(picRam[BUFFER_START]);
char* bufferEnd = &(picRam[BUFFER_END - 1]);

char spiData = -1;
char *writePtr, *readPtr;
BOOL overrun, underrun, cmd_read;


/* ##### Code under Test  ##### */

void buf_init()
{
  writePtr = bufferStart;
  readPtr = bufferStart;
  overrun = FALSE;
  underrun = TRUE;
  cmd_read = FALSE;
}

void wbuf_next()
{
  if (!overrun) {
    writePtr++;
    writePtr = writePtr > bufferEnd ? bufferStart : writePtr;
    underrun = FALSE;
    if(writePtr == readPtr) {
      overrun = TRUE;
    }
  }
}

void rbuf_next()
{
  if(!underrun) {
    readPtr++;
    readPtr = readPtr > bufferEnd ? bufferStart : readPtr;
    overrun = FALSE;
    if(readPtr == writePtr) {
      underrun = TRUE;
    }
  }
}

void timer2Interrupt()
{
  static char value = 0;
  if (!overrun) {
    *writePtr = ++value;
    wbuf_next();
  }
}

void sspInterrupt()
{
  if (cmd_read && spiData == 0) {
    *readPtr = 0;
    rbuf_next();
    spiData = underrun ? -1 : *readPtr;
  } else if (spiData == 'r') {
    cmd_read = TRUE;
    spiData = underrun ? -1 : *readPtr;
  } else {
    cmd_read = FALSE;
    printf("%s\n", "I don't like you!");
  }
}


/* ##### test environment ##### */

void debugPrint(char cmd) {
  printf("cmd('%c'): wPtr=%d, rPtr=%d, ovr=%d, udr=%d, sspbuf=%02hhx\n",
         cmd,
         (int) (writePtr - bufferStart),
         (int) (readPtr - bufferStart),
         (int) overrun,
         (int) underrun,
         (int) spiData);
  int i;
  printf("          ");
  for(i = 0; i < BUFFER_SIZE; ++i) {
    printf("%02hhx ", (unsigned int)bufferStart[i]);
  }
  printf("\n");
}

int main()
{
  struct termios t;
  tcgetattr(STDIN_FILENO, &t);
  t.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &t);

  buf_init();

  int key;
  debugPrint(' '); 
  for(;;) {
    key = getchar();
    switch(key) {
    case EOF:
    case 4:
    case 'q': return 0;
    case '\r':
    case '\n': break;
    case '+': timer2Interrupt(); debugPrint(key); break;
    case '0': spiData = 0; sspInterrupt(); debugPrint(key); break;
    default: spiData = key; sspInterrupt(); debugPrint(key); break;
    }
  }

  return 0;
}