Root/
Source at commit 87cc67d5f7bdc1e5c67de41fb05a9efc643734aa created 11 years 9 months ago. By Werner Almesberger, fw/: add image data upload (completely untested) | |
---|---|
1 | /* |
2 | * fw/image.c - Image sweep |
3 | * |
4 | * Written 2012 by Werner Almesberger |
5 | * Copyright 2012 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdbool.h> |
15 | #include <stdint.h> |
16 | |
17 | #include <avr/io.h> |
18 | #include <avr/interrupt.h> |
19 | |
20 | #include "image.h" |
21 | #include "sweep.h" |
22 | |
23 | |
24 | static uint32_t t_sw; /* cumulative number of timer ticks in sweep */ |
25 | |
26 | static uint16_t wait_periods; /* number of periods to wait before image */ |
27 | static uint16_t wait_period; /* ticks in wait period */ |
28 | static uint16_t wait_short; /* ticks to wait after periods */ |
29 | |
30 | static uint16_t pixel_ticks; /* number of ticks per pixel */ |
31 | static const struct line *curr_line; |
32 | static const struct line *end_line; |
33 | static bool forward; |
34 | |
35 | |
36 | ISR(TIMER1_OVF_vect) |
37 | { |
38 | /* update the sweep time */ |
39 | |
40 | t_sw += OCR1A; |
41 | |
42 | /* if at the end of the image, only update the time */ |
43 | |
44 | if (curr_line == end_line) |
45 | return; |
46 | |
47 | /* wait before starting the image */ |
48 | |
49 | if (wait_periods) { |
50 | if (!--wait_periods) |
51 | OCR1A = wait_short; |
52 | return; |
53 | } |
54 | |
55 | /* output the next line */ |
56 | |
57 | PORTB = (PORTB & 0x3f) | (curr_line->cb & 0xc0); |
58 | PORTC = curr_line->cb; |
59 | PORTD = curr_line->d; |
60 | |
61 | /* move to the next line */ |
62 | |
63 | if (forward) |
64 | curr_line++; |
65 | else |
66 | curr_line--; |
67 | |
68 | /* wait until the next pixel (or slow down if we're done) */ |
69 | |
70 | if (curr_line == end_line) |
71 | OCR1A = 0xffff; |
72 | else |
73 | OCR1A = pixel_ticks; |
74 | } |
75 | |
76 | |
77 | void image_sweep(const struct sweep *sweep) |
78 | { |
79 | TCCR1B = 0; /* stop the timer */ |
80 | |
81 | cli(); |
82 | |
83 | /* shut down all the LEDs, in case we're still in a sweep */ |
84 | |
85 | PORTB &= 0x3f; |
86 | PORTC = 0; |
87 | PORTD = 0; |
88 | |
89 | /* image pointers and sweep direction */ |
90 | |
91 | forward = sweep->forward; |
92 | if (forward) { |
93 | curr_line = image+sweep->left; |
94 | end_line = image+sweep->right+1; |
95 | } else { |
96 | curr_line = image+sweep->right; |
97 | end_line = image+sweep->left-1; |
98 | } |
99 | |
100 | /* timing parameters */ |
101 | |
102 | wait_periods = sweep->wait_ticks >> 16; |
103 | if (wait_periods) { |
104 | wait_short = sweep->wait_ticks; |
105 | wait_period = 0xffff; |
106 | |
107 | /* |
108 | * Make sure we have plenty of time to update OCR1A after an |
109 | * interrupt. |
110 | */ |
111 | if (wait_short < 256) { |
112 | wait_period -= 128; |
113 | wait_short += wait_periods << 7; |
114 | } |
115 | OCR1A = wait_period; |
116 | } else { |
117 | OCR1A = sweep->wait_ticks; |
118 | } |
119 | |
120 | /* prepare the hardware timer */ |
121 | |
122 | t_sw = 0; /* reset the time */ |
123 | TCNT1 = 0; |
124 | TIFR1 = 1 << TOV1; /* clear interrupt */ |
125 | |
126 | /* start the timer */ |
127 | |
128 | TCCR1B = |
129 | 1 << WGM13 | /* WG Mode 15, continued */ |
130 | 1 << WGM12 | |
131 | 1 << CS11; /* clkIO/8 */ |
132 | } |
133 | |
134 | |
135 | void sweep_init(void) |
136 | { |
137 | TCCR1A = |
138 | 1 << WGM11 | /* WG Mode 15 (Fast PWM to OCR1A) */ |
139 | 1 << WGM10; |
140 | |
141 | TCNT1 = 0; |
142 | |
143 | TIMSK1 = 1 << TOIE1; /* enable timer interrupts */ |
144 | } |
145 |
Branches:
master
tornado-v1