aboutsummaryrefslogtreecommitdiff
path: root/example-primer2/main.c
blob: c83a81045103a14de802771d6cb908d40e1eb2db (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include "neug.h"
#include "sys.h" /* for set_led */
#include "stm32f103.h"
#include "adc.h"
#include "st7732.h"
#include "primer2-switches.h"
#include "primer2-ts.h"
#include "board.h"

#ifdef TEST_DISPLAY_LOGO
static uint8_t buf[LCD_COLUMN*LCD_ROW*BYTES_PER_PIXEL] = {
#include "chopstx-logo.data"
};

void
lcd_logo (void)
{
  uint8_t args[4];

  /* Set RA and CA.  */
  /* RASET, 0, 0, 0, 159  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_ROW-1;
  lcd_command_writen (RASET, args, 4);
  /* CASET, 0, 0, 0, 127  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_COLUMN-1;
  lcd_command_writen (CASET, args, 4);

  /* Write logo.  */
  lcd_command_writen (RAMWR, buf, LCD_COLUMN*LCD_ROW*BYTES_PER_PIXEL);
}
#endif

/* Table of 32*(cos, sin) for range 0 to pi/2 with step pi/256.  */
static uint8_t ctable[128*2] = {
32, 0, 31, 0, 31, 0, 31, 1, 31, 1, 31, 1, 31, 2, 31, 2,
31, 3, 31, 3, 31, 3, 31, 4, 31, 4, 31, 5, 31, 5, 31, 5,
31, 6, 31, 6, 31, 7, 31, 7, 31, 7, 30, 8, 30, 8, 30, 8,
30, 9, 30, 9, 30, 10, 30, 10, 30, 10, 29, 11, 29, 11, 29, 11,
29, 12, 29, 12, 29, 12, 29, 13, 28, 13, 28, 14, 28, 14, 28, 14,
28, 15, 28, 15, 27, 15, 27, 16, 27, 16, 27, 16, 27, 17, 26, 17,
26, 17, 26, 18, 26, 18, 25, 18, 25, 19, 25, 19, 25, 19, 24, 19,
24, 20, 24, 20, 24, 20, 23, 21, 23, 21, 23, 21, 23, 22, 22, 22,
22, 22, 22, 22, 22, 23, 21, 23, 21, 23, 21, 23, 20, 24, 20, 24,
20, 24, 19, 24, 19, 25, 19, 25, 19, 25, 18, 25, 18, 26, 18, 26,
17, 26, 17, 26, 17, 27, 16, 27, 16, 27, 16, 27, 15, 27, 15, 28,
15, 28, 14, 28, 14, 28, 14, 28, 13, 28, 13, 29, 12, 29, 12, 29,
12, 29, 11, 29, 11, 29, 11, 29, 10, 30, 10, 30, 10, 30, 9, 30,
9, 30, 8, 30, 8, 30, 8, 30, 7, 31, 7, 31, 7, 31, 6, 31,
6, 31, 5, 31, 5, 31, 5, 31, 4, 31, 4, 31, 3, 31, 3, 31,
3, 31, 2, 31, 2, 31, 1, 31, 1, 31, 1, 31, 0, 31, 0, 31,
};

#ifdef TEST_LCD_CIRCLE
void
lcd_circle (void)
{
  int i, j;
  uint8_t *p;
  int x, y;

  /* Clear display.  */
  /* Set RA and CA.  */
  /* RASET, 0, 0, 0, 159  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_ROW-1;
  lcd_command_writen (RASET, args, 4);
  /* CASET, 0, 0, 0, 127  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_COLUMN-1;
  lcd_command_writen (CASET, args, 4);

  lcd_command_writen (RAMWR, 0, LCD_COLUMN*LCD_ROW*BYTES_PER_PIXEL);

  /* Draw a circle.  */
  for (i = 0; i < 128; i++)
    {
      x = 64 + ctable[2*i];
      y = 80 + ctable[2*i+1];
      lcd_draw_point (x, y, 0xfc, 0xfc, 0xfc);
    }
  for (i = 0; i < 128; i++)
    {
      x = 64 - ctable[2*i+1];
      y = 80 + ctable[2*i];
      lcd_draw_point (x, y, 0xfc, 0, 0xfc);
    }
  for (i = 0; i < 128; i++)
    {
      x = 64 - ctable[2*i];
      y = 80 - ctable[2*i+1];
      lcd_draw_point (x, y, 0, 0xfc, 0xfc);
    }
  for (i = 0; i < 128; i++)
    {
      x = 64 + ctable[2*i+1];
      y = 80 - ctable[2*i];
      lcd_draw_point (x, y, 0xfc, 0xfc, 0);
    }
}
#endif

#define RANDOM_BYTES_LENGTH 64
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];

int
main (int argc, const char *argv[])
{
  int count;
  int vx, vy;
  int r, g, b;
  uint8_t args[4];

  (void)argc;
  (void)argv;

  set_led (1);
  set_backlight (1);

  adc_init ();
  neug_init (random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));

  lcd_init ();

#ifdef TEST_LCD_LOGO
  lcd_logo ();

  while (! joystick ())
    chopstx_usec_wait (500*1000);
#endif

  /* Set RA and CA.  */
  /* RASET, 0, 0, 0, 159  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_ROW-1;
  lcd_command_writen (RASET, args, 4);
  /* CASET, 0, 0, 0, 127  */
  args[0] = 0x00; args[1] = 0; args[2] = 0x00; args[3] = LCD_COLUMN-1;
  lcd_command_writen (CASET, args, 4);

  /* Fill display.  */
  lcd_command_filln (RAMWR, 0xfc, LCD_COLUMN*LCD_ROW*BYTES_PER_PIXEL);

  vx = (LCD_COLUMN/2) << 5;
  vy = (LCD_ROW/2) << 5;
  r = g = b = 0;
#if 1
  adc3_init ();
  adc3_start ();

  count = 0;
  while (1)
    {
      uint32_t resv[4];

      adc3_conversion (resv);
      if (ts_pushed (resv[2]))
	{
	  int reg[3], point[2];

	  ts_conversion (resv, reg);
#if 0
	  lcd_printhex (reg[0], 5,  8, 0x00, 0x00, 0xfc, 0xfc);
	  lcd_printhex (reg[1], 5,  18, 0x00, 0x00, 0xfc, 0xfc);
	  lcd_printhex (reg[2], 5,  28, 0x00, 0x00, 0xfc, 0xfc);
#endif
	  if (!ts_adjust (reg, point))
	    {
	      chopstx_usec_wait (50*1000);
	      continue;
	    }

	  lcd_draw_point (point[0], point[1], r, g, b);
	}
      else
	ts_adjust (NULL, NULL);

      chopstx_usec_wait (50*1000);
      count++;
      if ((count/10) & 1)
	set_led (0);
      else
	set_led (1);
      if (pbutton())
	break;
    }

  adc3_stop ();
#endif

  count = 0;
  while (1)
    {
      int jo;
      uint32_t th = neug_get (NEUG_KICK_FILLING) & 0x1ff;

      /* Get random point on a circle with the radius of 32 and walk
	 towards it.  */
      if (th < 128)
	{
	  vx += ctable[2*th];
	  vy += ctable[2*th+1];
	}
      else if (th < 256)
	{
	  vx -= ctable[2*(th & 0x7f)+1];
	  vy += ctable[2*(th & 0x7f)];
	}
      else if (th < 384)
	{
	  vx -= ctable[2*(th & 0x7f)];
	  vy -= ctable[2*(th & 0x7f)+1];
	}
      else
	{
	  vx += ctable[2*(th & 0x7f)+1];
	  vy -= ctable[2*(th & 0x7f)];
	}

      if (vx < 0)
	vx += (LCD_COLUMN << 5);
      if (vy < 0)
	vy += (LCD_ROW << 5);

      /* Change draw color with joystick.  */
      jo = joystick ();
      if (JOYSTICK_L (jo))
	r = 0xfc;
      if (JOYSTICK_R (jo))
	g = 0xfc;
      if (JOYSTICK_U (jo))
	b = 0xfc;
      if (JOYSTICK_D (jo))
	r = g = b = 0;

      /* 2-dim random walk on torus.  */
      lcd_draw_point ((vx>>5)%LCD_COLUMN, (vy>>5)%LCD_ROW, r, g, b);
      chopstx_usec_wait (10*1000);

      if (pbutton ())
	count++;
      /* Shutdown when p-button is held down for 5 sec.  */
      if (count > 500)
	{
	  set_led (0);
	  shutdown ();
	}
      else
	{
	  /* Disable backlight when p-button is held down for 3 sec.  */
	  if (count > 300)
	    set_backlight (0);

	  /* Blink led when p-button is held.  */
	  if ((count/50) & 1)
	    set_led (0);
	  else
	    set_led (1);
	}
#if 1
      lcd_printhex (count, 5,  8, 0x00, 0x00, 0xfc, 0xfc);
#endif

    }

  return 0;
}