summaryrefslogtreecommitdiff
path: root/src/display.cc
blob: 18e88beeb552b5d21160f1d8baabc8a6a9ac3b9b (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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>

#include "display.h"
#include "font.h"

Display display;

Display::Display()
{
	char_pos = -1;
}

void Display::disable()
{
	TIMSK0 &= ~_BV(TOIE0);
	PORTB = 0;
	PORTD = 0;
}

void Display::enable()
{
	// Ports B and D drive the dot matrix display -> set all as output
	DDRB = 0xff;
	DDRD = 0xff;

	// Enable 8bit counter with prescaler=8 (-> timer frequency = 1MHz)
	TCCR0A = _BV(CS01);
	// raise timer interrupt on counter overflow (-> interrupt frequency = ~4kHz)
	TIMSK0 = _BV(TOIE0);
}

void Display::multiplex()
{
	static uint8_t scroll;

	/*
	 * To avoid flickering, do not put any code (or expensive index
	 * calculations) between the following three lines.
	 */
	PORTB = 0;
	PORTD = disp_buf[active_col];
	PORTB = _BV(active_col);

	if (++active_col == 8) {
		active_col = 0;
		if (++scroll == current_anim->speed) {
			scroll = 0;
			need_update = 1;
		}
	}
}

void Display::update() {
	uint8_t i, glyph_len;
	uint8_t *glyph_addr;
	if (need_update) {
		need_update = 0;

		if (status == RUNNING) {
			if (current_anim->type == AnimationType::TEXT) {
				for (i = 0; i < 7; i++) {
					disp_buf[i] = disp_buf[i+1];
				}

				glyph_addr = (uint8_t *)pgm_read_ptr(&font[current_anim->data[str_pos]]);
				glyph_len = pgm_read_byte(&glyph_addr[0]);
				char_pos++;

				if (char_pos > glyph_len) {
					char_pos = 0;
					str_pos++;
				}

				if (char_pos == 0) {
					disp_buf[7] = 0xff; // whitespace
				} else {
					disp_buf[7] = ~pgm_read_byte(&glyph_addr[char_pos]);
				}
			} else if (current_anim->type == AnimationType::FRAMES) {
				for (i = 0; i < 8; i++) {
					disp_buf[i] = ~current_anim->data[str_pos+i];
				}
				str_pos += 8;
			}
			if (str_pos >= current_anim->length) {
				str_pos = 0;
				if (current_anim->delay > 0) {
					status = PAUSED;
				}
			}
		} else if (status == PAUSED) {
			str_pos++;
			if (str_pos >= current_anim->delay) {
				str_pos = 0;
				status = RUNNING;
			}
		}
	}
}

void Display::reset()
{
	for (uint8_t i = 0; i < 8; i++)
		disp_buf[i] = 0xff;
	str_pos = 0;
	char_pos = -1;
	status = RUNNING;
}

void Display::show(animation_t *anim)
{
	current_anim = anim;
	reset();
}

/*
 * Draws a single display column. This function should be called at least once
 * per millisecond.
 *
 * Current configuration:
 * Called every 256 microseconds. The whole display is refreshed every 2048us,
 * giving a refresh rate of ~500Hz
 */
ISR(TIMER0_OVF_vect)
{
	display.multiplex();
}