Source

vectorscope / scopeclock.c

Trammell Hudson b0f2075 












Trammell Hudson ad2f608 
Trammell Hudson b0f2075 
Trammell Hudson 2d1324d 






















































Trammell Hudson b0f2075 


































Trammell Hudson 31d7197 
































Trammell Hudson f4369a9 


































Trammell Hudson ad2f608 

Trammell Hudson f4369a9 



















Trammell Hudson ad2f608 






Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 
Trammell Hudson ad2f608 


Trammell Hudson a962fd0 

Trammell Hudson ad2f608 
Trammell Hudson 63d62f7 




Trammell Hudson ad2f608 
Trammell Hudson 63d62f7 








Trammell Hudson a962fd0 

Trammell Hudson 63d62f7 
Trammell Hudson ad2f608 
Trammell Hudson 63d62f7 


Trammell Hudson ad2f608 


Trammell Hudson a962fd0 


























Trammell Hudson ad2f608 

Trammell Hudson b0f2075 


































Trammell Hudson d7b8d2c 

Trammell Hudson 2d1324d 
























Trammell Hudson d7b8d2c 
Trammell Hudson b0f2075 


Trammell Hudson 2d1324d 







Trammell Hudson b0f2075 
Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 

Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 



Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 



Trammell Hudson b0f2075 
Trammell Hudson a962fd0 









Trammell Hudson 2d1324d 
Trammell Hudson a962fd0 
Trammell Hudson d7b8d2c 


Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 


Trammell Hudson 2d1324d 
Trammell Hudson d7b8d2c 


Trammell Hudson a962fd0 

Trammell Hudson 2d1324d 



Trammell Hudson d7b8d2c 
Trammell Hudson 2d1324d 



Trammell Hudson d7b8d2c 


Trammell Hudson a962fd0 



Trammell Hudson b0f2075 














  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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
 * \file Dual DAC outputs for driving a vector scope.
 *
 */

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>
#include "usb_serial.h"
#include "bits.h"
#include "hershey.h"


/** Track the number of miliseconds, sec, min and hour since midnight */
static volatile uint16_t now_ms;
static volatile uint8_t now_sec;
static volatile uint8_t now_min;
static volatile uint8_t now_hour;


// Define CONFIG_HZ_IRQ to enable a timer interrupt rather than
// polling the interrupt flag.
#define CONFIG_HZ_IRQ

/** 1000-hz overflow */
#ifdef CONFIG_HZ_IRQ
ISR(TIMER0_COMPA_vect)
#else
static void
now_update(void)
#endif
{
	if (now_ms < 999)
	{
		now_ms += 1;
		return;
	}

	now_ms = 0;

	if (now_sec < 59)
	{
		now_sec++;
		return;
	}

	now_sec = 0;

	if (now_min < 59)
	{
		now_min++;
		return;
	}

	now_min = 0;

	if (now_hour < 23)
	{
		now_hour++;
		return;
	}

	now_hour = 0;
}



void send_str(const char *s);
uint8_t recv_str(char *buf, uint8_t size);
void parse_and_execute_command(const char *buf, uint8_t num);

static uint8_t
hexdigit(
	uint8_t x
)
{
	x &= 0xF;
	if (x < 0xA)
		return x + '0' - 0x0;
	else
		return x + 'A' - 0xA;
}


static uint8_t
printable(
	uint8_t x
)
{
	if ('A' <= x && x <= 'Z')
		return 1;
	if ('a' <= x && x <= 'z')
		return 1;
	if ('0' <= x && x <= '9')
		return 1;
	if (x == ' ')
		return 1;

	return 0;
}


static void
line_vert(
	uint8_t x0,
	uint8_t y0,
	uint8_t w
)
{
	PORTB = x0;
	PORTD = y0;

	for (uint8_t i = 0 ; i < w ; i++)
	{
		PORTD++;
	}
}

static void
line_horiz(
	uint8_t x0,
	uint8_t y0,
	uint8_t h
)
{
	PORTB = x0;
	PORTD = y0;

	for (uint8_t i = 0 ; i < h ; i++)
	{
		PORTB++;
	}
}


static void
line(
	uint8_t x0,
	uint8_t y0,
	uint8_t x1,
	uint8_t y1
)
{
	int dx;
	int dy;
	int sx;
	int sy;

	if (x0 < x1)
	{
		dx = x1 - x0;
		sx = 1;
	} else {
		dx = x0 - x1;
		sx = -1;
	}

	if (y0 < y1)
	{
		dy = y1 - y0;
		sy = 1;
	} else {
		dy = y0 - y1;
		sy = -1;
	}

	int err = dx - dy;

	while (1)
	{
		PORTB = x0;
		PORTD = y0;

		if (x0 == x1 && y0 == y1)
			break;

		int e2 = 2 * err;

		if (e2 > -dy)
		{
			err = err - dy;
			x0 += sx;
		}
		if (e2 < dx)
		{
			err = err + dx;
			y0 += sy;
		}
	}
}


static void
draw_digit(
	uint8_t x,
	uint8_t y,
	uint8_t val
)
{
	const path_t * p = digits[val];
	const int8_t scale = 2;

	while (1)
	{
		uint8_t ox = x + p->x / scale;
		uint8_t oy = y + p->y / scale;

		while (1)
		{
			p++;
			int8_t px = p->x;
			int8_t py = p->y;

			if (px == 0 && py == 0)
				return;

			if (px == -1 && py == -1)
			{
				p++;
				break;
			}

			uint8_t nx = x + px / scale;
			uint8_t ny = y + py / scale;

			line(ox, oy, nx, ny);
			ox = nx;
			oy = ny;
		}
	}
}

static const uint8_t hour_pos[][2] = {
{ 128,228 },
{ 153,224 },
{ 177,214 },
{ 198,198 },
{ 214,178 },
{ 224,153 },
{ 227,128 },
{ 224,102 },
{ 214,78 },
{ 198,57 },
{ 178,41 },
{ 154,31 },
{ 128,28 },
{ 102,31 },
{ 78,41 },
{ 57,57 },
{ 41,77 },
{ 31,101 },
{ 28,127 },
{ 31,153 },
{ 41,177 },
{ 57,198 },
{ 77,214 },
{ 101,224 },
};



int main(void)
{
	// set for 16 MHz clock
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
	CPU_PRESCALE(0);

	// Disable the ADC
	ADMUX = 0;

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);

	// wait for the user to run their terminal emulator program
	// which sets DTR to indicate it is ready to receive.
	while (!(usb_serial_get_control() & USB_SERIAL_DTR))
		continue;

	// discard anything that was received prior.  Sometimes the
	// operating system or other software will send a modem
	// "AT command", which can still be buffered.
	usb_serial_flush_input();

	DDRB = 0xFF;
	DDRD = 0xFF;
	PORTB = 128;
	PORTD = 0;

	uint8_t off = 0;
	char buf[32];

	uint8_t px = 0;
	uint8_t py = 0;
	uint8_t count = 0;

	// Configure timer0 to overflow every 1 ms
	// CTC mode (clear counter at OCR0A, signal interrupt)
	TCCR0A = 0
		| (1 << WGM01)
		| (0 << WGM00)
		;

	TCCR0B = 0
		| (0 << WGM02)
		| (0 << CS02)
		| (1 << CS01)
		| (1 << CS00)
		;


	// Clk/256 @ 16 MHz => 250 ticks == 10 ms
	OCR0A = 250;

#ifdef CONFIG_HZ_IRQ
	sbi(TIMSK0, OCIE0A);
	sei();
#endif

	

	while (1)
	{
#ifndef CONFIG_HZ_IRQ
		// If the interrupt is not in use, check the overflow bit
		if (bit_is_set(TIFR0, OCF0A))
		{
			sbi(TIFR0, OCF0A);
			now_update();
		}
#endif

		if (count++ == 100)
		{
			count = 0;
			if (px < 30)
				px++;
			else
				px = 0;
	
			if (py < 120)
				py += 3;
			else
				py = 0;
		}


		for (uint8_t h = 0 ; h < 24 ; h++)
		{
			uint8_t x = hour_pos[h][0];
			uint8_t y = hour_pos[h][1];
			draw_digit(x-8, y-4, h / 10);
			draw_digit(x+2, y-4, h % 10);
		}

		// Draw the hour hand
		uint8_t h = now_hour;

		draw_digit( 0+px, 64+py, h / 10);
		draw_digit(32+px, 64+py, h % 10);

		uint8_t m = now_min;
		draw_digit(80+px, 64+py, m / 10);
		draw_digit(80+32+px, 64+py, m % 10);

		uint8_t s = now_sec;
		draw_digit(160+px, 64+py, s / 10);
		draw_digit(160+32+px, 64+py, s % 10);

		line(128, 128, hour_pos[s/3][0], hour_pos[s/3][1]);

/*
		draw_digit(0*32+px, py, (now_ms / 100) % 10);
		draw_digit(1*32+px, py, (now_ms / 10) % 10);
		draw_digit(2*32+px, py, (now_ms / 1) % 10);

		draw_digit(4*32+px, py, (TCNT0 / 100) % 10);
		draw_digit(5*32+px, py, (TCNT0 / 10) % 10);
		draw_digit(6*32+px, py, (TCNT0 / 1) % 10);
*/

		//line(128, 128, (x & 255), x >> 8);

		line_horiz(0,0,255);
		line_horiz(0,255,255);
		line_vert(0,0,255);
		line_vert(255,0,255);
	}
}


// Send a string to the USB serial port.  The string must be in
// flash memory, using PSTR
//
void send_str(const char *s)
{
	char c;
	while (1) {
		c = pgm_read_byte(s++);
		if (!c) break;
		usb_serial_putchar(c);
	}
}