Rev 331 | Rev 349 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 331 | Rev 346 | ||
---|---|---|---|
1 | /**************************************************************************** |
1 | /**************************************************************************** |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
3 | * admiralcascade@gmail.com * |
3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
5 | * * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License. * |
8 | * the Free Software Foundation; either version 2 of the License. * |
9 | * * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
13 | * GNU General Public License for more details. * |
14 | * * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ****************************************************************************/ |
19 | ****************************************************************************/ |
- | 20 | ||
- | 21 | #include <avr/io.h> |
|
- | 22 | #include <util/delay.h> |
|
20 | 23 | #include <avr/pgmspace.h> |
|
21 | #include "max7456_software_spi.h" |
24 | #include "max7456_software_spi.h" |
22 | 25 | ||
23 | /* ########################################################################## |
26 | /* ########################################################################## |
24 | * MAX7456 SPI & Display stuff |
27 | * MAX7456 SPI & Display stuff |
25 | * ##########################################################################*/ |
28 | * ##########################################################################*/ |
26 | 29 | ||
27 | /** |
30 | /** |
28 | * Send a byte through SPI |
31 | * Send a byte through SPI |
29 | */ |
32 | */ |
30 | void spi_send(uint8_t byte) { |
33 | void spi_send(uint8_t byte) { |
31 | for (int8_t i = 7; i >= 0; i--) { |
34 | for (int8_t i = 7; i >= 0; i--) { |
32 | if (((byte >> i) & 1)) { |
35 | if (((byte >> i) & 1)) { |
33 | MAX_SDIN_HIGH |
36 | MAX_SDIN_HIGH |
34 | } else { |
37 | } else { |
35 | MAX_SDIN_LOW |
38 | MAX_SDIN_LOW |
36 | } |
39 | } |
37 | MAX_SCLK_HIGH |
40 | MAX_SCLK_HIGH |
38 | MAX_SCLK_LOW |
41 | MAX_SCLK_LOW |
39 | } |
42 | } |
40 | } |
43 | } |
41 | 44 | ||
42 | /** |
45 | /** |
43 | * Send <byte> to <address> of MAX7456 |
46 | * Send <byte> to <address> of MAX7456 |
44 | */ |
47 | */ |
45 | void spi_send_byte(uint8_t address, uint8_t byte) { |
48 | void spi_send_byte(uint8_t address, uint8_t byte) { |
46 | // start sending |
49 | // start sending |
47 | MAX_CS_LOW |
50 | MAX_CS_LOW |
48 | 51 | ||
49 | spi_send(address); |
52 | spi_send(address); |
50 | spi_send(byte); |
53 | spi_send(byte); |
51 | 54 | ||
52 | // end sending |
55 | // end sending |
53 | MAX_CS_HIGH |
56 | MAX_CS_HIGH |
54 | } |
57 | } |
55 | 58 | ||
56 | /** |
59 | /** |
57 | * write a <character> to <address> of MAX7456 display memory |
60 | * write a <character> to <address> of MAX7456 display memory |
58 | */ |
61 | */ |
59 | void write_char(uint16_t address, char character) { |
62 | void write_char(uint16_t address, char character) { |
60 | spi_send_byte(0x05, (address & 0xFF00) >> 8); // DMAH |
63 | spi_send_byte(0x05, (address & 0xFF00) >> 8); // DMAH |
61 | spi_send_byte(0x06, (address & 0x00FF)); // DMAL |
64 | spi_send_byte(0x06, (address & 0x00FF)); // DMAL |
62 | spi_send_byte(0x07, character); // DMDI |
65 | spi_send_byte(0x07, character); // DMDI |
63 | } |
66 | } |
64 | 67 | ||
65 | /** |
68 | /** |
66 | * write a character <attribute> to <address> of MAX7456 display memory |
69 | * write a character <attribute> to <address> of MAX7456 display memory |
67 | */ |
70 | */ |
68 | void write_char_att(uint16_t address, char attribute) { |
71 | void write_char_att(uint16_t address, char attribute) { |
69 | // the only important part is that the DMAH[1] is set |
72 | // the only important part is that the DMAH[1] is set |
70 | // so we add 2 which binary is the 2nd lowest byte |
73 | // so we add 2 which binary is the 2nd lowest byte |
71 | spi_send_byte(0x05, ((address & 0xFF00) >> 8) | 2); // DMAH |
74 | spi_send_byte(0x05, ((address & 0xFF00) >> 8) | 2); // DMAH |
72 | spi_send_byte(0x06, (address & 0x00FF)); // DMAL |
75 | spi_send_byte(0x06, (address & 0x00FF)); // DMAL |
73 | spi_send_byte(0x07, attribute); // DMDI |
76 | spi_send_byte(0x07, attribute); // DMDI |
74 | } |
77 | } |
75 | 78 | ||
76 | /** |
79 | /** |
77 | * write a <character> at <x>/<y> to MAX7456 display memory |
80 | * write a <character> at <x>/<y> to MAX7456 display memory |
78 | */ |
81 | */ |
79 | void write_char_xy(uint8_t x, uint8_t y, char character) { |
82 | void write_char_xy(uint8_t x, uint8_t y, char character) { |
80 | uint16_t address = y * 30 + x; |
83 | uint16_t address = y * 30 + x; |
81 | write_char(address, character); |
84 | write_char(address, character); |
82 | } |
85 | } |
83 | 86 | ||
84 | /** |
87 | /** |
85 | * write a character <attribute> at <x>/<y> to MAX7456 display memory |
88 | * write a character <attribute> at <x>/<y> to MAX7456 display memory |
86 | */ |
89 | */ |
87 | void write_char_att_xy(uint8_t x, uint8_t y, char attribute) { |
90 | void write_char_att_xy(uint8_t x, uint8_t y, char attribute) { |
88 | uint16_t address = y * 30 + x; |
91 | uint16_t address = y * 30 + x; |
89 | write_char_att(address, attribute); |
92 | write_char_att(address, attribute); |
90 | } |
93 | } |
91 | 94 | ||
92 | /** |
95 | /** |
93 | * clear display by writing blank characters all over it |
96 | * clear display by writing blank characters all over it |
94 | */ |
97 | */ |
95 | void clear(void) { |
98 | void clear(void) { |
96 | uint16_t memory_address = 0; |
99 | uint16_t memory_address = 0; |
97 | for (unsigned int a = 0; a < 480; a++) { |
100 | for (unsigned int a = 0; a < 480; a++) { |
98 | write_char(memory_address++, 0); |
101 | write_char(memory_address++, 0); |
99 | } |
102 | } |
100 | } |
103 | } |
101 | 104 | ||
102 | /** |
105 | /** |
103 | * write an ascii <character> to <address> of MAX7456 display memory |
106 | * write an ascii <character> to <address> of MAX7456 display memory |
104 | */ |
107 | */ |
105 | void write_ascii_char(uint16_t address, char c) { |
108 | void write_ascii_char(uint16_t address, char c) { |
106 | if (c == 32) c = 0; // remap space |
109 | if (c == 32) c = 0; // remap space |
107 | else if (c > 48 && c <= 57) c -= 48; // remap numbers |
110 | else if (c > 48 && c <= 57) c -= 48; // remap numbers |
108 | else if (c == '0') c = 10; // remap zero |
111 | else if (c == '0') c = 10; // remap zero |
109 | else if (c >= 65 && c <= 90) c -= 54; // remap big letters |
112 | else if (c >= 65 && c <= 90) c -= 54; // remap big letters |
110 | else if (c >= 97 && c <= 122) c -= 60; // remap small letters |
113 | else if (c >= 97 && c <= 122) c -= 60; // remap small letters |
111 | else if (c == '(') c = 63; // remap |
114 | else if (c == '(') c = 63; // remap |
112 | else if (c == ')') c = 64; // remap |
115 | else if (c == ')') c = 64; // remap |
113 | else if (c == '.') c = 65; // remap |
116 | else if (c == '.') c = 65; // remap |
114 | else if (c == '?') c = 66; // remap |
117 | else if (c == '?') c = 66; // remap |
115 | else if (c == ';') c = 67; // remap |
118 | else if (c == ';') c = 67; // remap |
116 | else if (c == ':') c = 68; // remap |
119 | else if (c == ':') c = 68; // remap |
117 | else if (c == ',') c = 69; // remap |
120 | else if (c == ',') c = 69; // remap |
118 | else if (c == '\'') c = 70; // remap |
121 | else if (c == '\'') c = 70; // remap |
119 | else if (c == '/') c = 71; // remap |
122 | else if (c == '/') c = 71; // remap |
120 | else if (c == '"') c = 72; // remap |
123 | else if (c == '"') c = 72; // remap |
121 | else if (c == '-') c = 73; // remap minus |
124 | else if (c == '-') c = 73; // remap minus |
122 | else if (c == '<') c = 74; // remap |
125 | else if (c == '<') c = 74; // remap |
123 | else if (c == '>') c = 75; // remap |
126 | else if (c == '>') c = 75; // remap |
124 | else if (c == '@') c = 76; // remap |
127 | else if (c == '@') c = 76; // remap |
125 | write_char(address, c); |
128 | write_char(address, c); |
126 | } |
129 | } |
127 | 130 | ||
128 | /** |
131 | /** |
129 | * write an ascii <string> at <x>/<y> to MAX7456 display memory |
132 | * write an ascii <string> at <x>/<y> to MAX7456 display memory |
130 | */ |
133 | */ |
131 | void write_ascii_string(uint8_t x, uint8_t y, char *string) { |
134 | void write_ascii_string(uint8_t x, uint8_t y, char *string) { |
132 | while (*string) { |
135 | while (*string) { |
133 | write_ascii_char(((x++)+(y * 30)), *string); |
136 | write_ascii_char(((x++)+(y * 30)), *string); |
134 | string++; |
137 | string++; |
135 | } |
138 | } |
136 | } |
139 | } |
- | 140 | ||
- | 141 | /** |
|
- | 142 | * write an ascii <string> from progmen at <x>/<y> to MAX7456 display memory |
|
- | 143 | */ |
|
- | 144 | void write_ascii_string_pgm(uint8_t x, uint8_t y, char *string) { |
|
- | 145 | while (pgm_read_byte(string) != 0x00) |
|
- | 146 | write_ascii_char(((x++)+(y * 30)), pgm_read_byte(string++)); |
|
- | 147 | } |
|
137 | 148 | ||
138 | /** |
149 | /** |
139 | * Write only the last three digits of a <number> at <x>/<y> to MAX7456 |
150 | * Write only the last three digits of a <number> at <x>/<y> to MAX7456 |
140 | * display memory. takes full 16bit numbers as well for stuff |
151 | * display memory. takes full 16bit numbers as well for stuff |
141 | * like compass only taking three characters (values <= 999) |
152 | * like compass only taking three characters (values <= 999) |
142 | */ |
153 | */ |
143 | void write_3digit_number_u(uint8_t x, uint8_t y, uint16_t number) { |
154 | void write_3digit_number_u(uint8_t x, uint8_t y, uint16_t number) { |
144 | uint16_t num = 100; |
155 | uint16_t num = 100; |
145 | uint8_t started = 0; |
156 | uint8_t started = 0; |
146 | 157 | ||
147 | while (num > 0) { |
158 | while (num > 0) { |
148 | uint8_t b = number / num; |
159 | uint8_t b = number / num; |
149 | if (b > 0 || started || num == 1) { |
160 | if (b > 0 || started || num == 1) { |
150 | write_ascii_char((x++)+(y * 30), '0' + b); |
161 | write_ascii_char((x++)+(y * 30), '0' + b); |
151 | started = 1; |
162 | started = 1; |
152 | } else { |
163 | } else { |
153 | write_ascii_char((x++)+(y * 30), 0); |
164 | write_ascii_char((x++)+(y * 30), 0); |
154 | } |
165 | } |
155 | number -= b * num; |
166 | number -= b * num; |
156 | 167 | ||
157 | num /= 10; |
168 | num /= 10; |
158 | } |
169 | } |
159 | } |
170 | } |
160 | 171 | ||
161 | /** |
172 | /** |
162 | * Write only the last two digits of a number at <x>/<y> to MAX7456 |
173 | * Write only the last two digits of a number at <x>/<y> to MAX7456 |
163 | * display memory. takes full 16bit numbers as well for stuff |
174 | * display memory. takes full 16bit numbers as well for stuff |
164 | * like seconds only taking two characters (values <= 99) |
175 | * like seconds only taking two characters (values <= 99) |
165 | * Since this is used for seconds only and it looks better, there |
176 | * Since this is used for seconds only and it looks better, there |
166 | * is a trading 0 attached |
177 | * is a trading 0 attached |
167 | */ |
178 | */ |
168 | void write_2digit_number_u(uint8_t x, uint8_t y, uint16_t number) { |
179 | void write_2digit_number_u(uint8_t x, uint8_t y, uint16_t number) { |
169 | uint16_t num = 10; |
180 | uint16_t num = 10; |
170 | uint8_t started = 0; |
181 | uint8_t started = 0; |
171 | 182 | ||
172 | while (num > 0) { |
183 | while (num > 0) { |
173 | uint8_t b = number / num; |
184 | uint8_t b = number / num; |
174 | if (b > 0 || started || num == 1) { |
185 | if (b > 0 || started || num == 1) { |
175 | write_ascii_char((x++)+(y * 30), '0' + b); |
186 | write_ascii_char((x++)+(y * 30), '0' + b); |
176 | started = 1; |
187 | started = 1; |
177 | } else { |
188 | } else { |
178 | write_ascii_char((x++)+(y * 30), '0'); |
189 | write_ascii_char((x++)+(y * 30), '0'); |
179 | } |
190 | } |
180 | number -= b * num; |
191 | number -= b * num; |
181 | 192 | ||
182 | num /= 10; |
193 | num /= 10; |
183 | } |
194 | } |
184 | } |
195 | } |
185 | 196 | ||
186 | /** |
197 | /** |
187 | * write a unsigned number as /10th at <x>/<y> to MAX7456 display memory |
198 | * write a unsigned number as /10th at <x>/<y> to MAX7456 display memory |
188 | */ |
199 | */ |
189 | void write_number_u_10th(uint8_t x, uint8_t y, uint16_t number) { |
200 | void write_number_u_10th(uint8_t x, uint8_t y, uint16_t number) { |
190 | uint16_t num = 10000; |
201 | uint16_t num = 10000; |
191 | uint8_t started = 0; |
202 | uint8_t started = 0; |
192 | 203 | ||
193 | while (num > 0) { |
204 | while (num > 0) { |
194 | uint8_t b = number / num; |
205 | uint8_t b = number / num; |
195 | 206 | ||
196 | if (b > 0 || started || num == 1) { |
207 | if (b > 0 || started || num == 1) { |
197 | if ((num / 10) == 0) write_char((x++)+(y * 30), 65); |
208 | if ((num / 10) == 0) write_char((x++)+(y * 30), 65); |
198 | write_ascii_char((x++)+(y * 30), '0' + b); |
209 | write_ascii_char((x++)+(y * 30), '0' + b); |
199 | started = 1; |
210 | started = 1; |
200 | } else { |
211 | } else { |
201 | write_ascii_char((x++)+(y * 30), 0); |
212 | write_ascii_char((x++)+(y * 30), 0); |
202 | } |
213 | } |
203 | number -= b * num; |
214 | number -= b * num; |
204 | 215 | ||
205 | num /= 10; |
216 | num /= 10; |
206 | } |
217 | } |
207 | } |
218 | } |
208 | 219 | ||
209 | /** |
220 | /** |
210 | * write a unsigned number at <x>/<y> to MAX7456 display memory |
221 | * write a unsigned number at <x>/<y> to MAX7456 display memory |
211 | */ |
222 | */ |
212 | void write_number_u(uint8_t x, uint8_t y, uint16_t number) { |
223 | void write_number_u(uint8_t x, uint8_t y, uint16_t number) { |
213 | uint16_t num = 10000; |
224 | uint16_t num = 10000; |
214 | uint8_t started = 0; |
225 | uint8_t started = 0; |
215 | 226 | ||
216 | while (num > 0) { |
227 | while (num > 0) { |
217 | uint8_t b = number / num; |
228 | uint8_t b = number / num; |
218 | if (b > 0 || started || num == 1) { |
229 | if (b > 0 || started || num == 1) { |
219 | write_ascii_char((x++)+(y * 30), '0' + b); |
230 | write_ascii_char((x++)+(y * 30), '0' + b); |
220 | started = 1; |
231 | started = 1; |
221 | } else { |
232 | } else { |
222 | write_ascii_char((x++)+(y * 30), 0); |
233 | write_ascii_char((x++)+(y * 30), 0); |
223 | } |
234 | } |
224 | number -= b * num; |
235 | number -= b * num; |
225 | 236 | ||
226 | num /= 10; |
237 | num /= 10; |
227 | } |
238 | } |
228 | } |
239 | } |
229 | 240 | ||
230 | /** |
241 | /** |
231 | * write a signed number at <x>/<y> to MAX7456 display memory |
242 | * write a signed number at <x>/<y> to MAX7456 display memory |
232 | */ |
243 | */ |
233 | void write_number_s(uint8_t x, uint8_t y, int16_t w) { |
244 | void write_number_s(uint8_t x, uint8_t y, int16_t w) { |
234 | if (((uint16_t) w) > 32767) { |
245 | if (((uint16_t) w) > 32767) { |
235 | w = w - 65536; |
246 | w = w - 65536; |
236 | 247 | ||
237 | int16_t num = -10000; |
248 | int16_t num = -10000; |
238 | uint8_t started = 0; |
249 | uint8_t started = 0; |
239 | 250 | ||
240 | while (num < 0) { |
251 | while (num < 0) { |
241 | uint8_t b = w / num; |
252 | uint8_t b = w / num; |
242 | if (b > 0 || started || num == 1) { |
253 | if (b > 0 || started || num == 1) { |
243 | if (!started) write_char((x - 1)+(y * 30), 0x49); |
254 | if (!started) write_char((x - 1)+(y * 30), 0x49); |
244 | write_ascii_char((x++)+(y * 30), '0' + b); |
255 | write_ascii_char((x++)+(y * 30), '0' + b); |
245 | started = 1; |
256 | started = 1; |
246 | } else { |
257 | } else { |
247 | write_ascii_char((x++)+(y * 30), 0); |
258 | write_ascii_char((x++)+(y * 30), 0); |
248 | } |
259 | } |
249 | w -= b * num; |
260 | w -= b * num; |
250 | 261 | ||
251 | num /= 10; |
262 | num /= 10; |
252 | } |
263 | } |
253 | } else { |
264 | } else { |
254 | write_char((x)+(y * 30), 0); |
265 | write_char((x)+(y * 30), 0); |
255 | write_number_u(x, y, w); |
266 | write_number_u(x, y, w); |
256 | } |
267 | } |
257 | } |
268 | } |
258 | 269 | ||
259 | /** |
270 | /** |
260 | * write <seconds> as human readable time at <x>/<y> to MAX7456 display mem |
271 | * write <seconds> as human readable time at <x>/<y> to MAX7456 display mem |
261 | */ |
272 | */ |
262 | void write_time(uint8_t x, uint8_t y, uint16_t seconds) { |
273 | void write_time(uint8_t x, uint8_t y, uint16_t seconds) { |
263 | uint16_t min = seconds / 60; |
274 | uint16_t min = seconds / 60; |
264 | seconds -= min * 60; |
275 | seconds -= min * 60; |
265 | write_3digit_number_u(x, y, min); |
276 | write_3digit_number_u(x, y, min); |
266 | write_char_xy(x + 3, y, 68); |
277 | write_char_xy(x + 3, y, 68); |
267 | write_2digit_number_u(x + 4, y, seconds); |
278 | write_2digit_number_u(x + 4, y, seconds); |
268 | } |
279 | } |
269 | 280 | ||
270 | /** |
281 | /** |
271 | * for testing write all chars to screen |
282 | * for testing write all chars to screen |
272 | */ |
283 | */ |
273 | void write_all_chars() { |
284 | void write_all_chars() { |
274 | uint16_t x = 3, y = 2, t = 0; |
285 | uint16_t x = 3, y = 2, t = 0; |
275 | while (t < 256) { |
286 | while (t < 256) { |
276 | write_char_xy(x++, y, t++); |
287 | write_char_xy(x++, y, t++); |
277 | if (x > 25) { |
288 | if (x > 25) { |
278 | y++; |
289 | y++; |
279 | x = 3; |
290 | x = 3; |
280 | } |
291 | } |
281 | } |
292 | } |
282 | } |
293 | } |
283 | 294 | ||
284 | /** |
295 | /** |
285 | * let the MAX7456 learn a new character at <number> |
296 | * let the MAX7456 learn a new character at <number> |
286 | * with <data>. |
297 | * with <data>. |
287 | */ |
298 | */ |
288 | void learn_char(uint8_t number, unsigned char* data) { |
299 | void learn_char(uint8_t number, unsigned char* data) { |
289 | // select character to write (CMAH) |
300 | // select character to write (CMAH) |
290 | spi_send_byte(0x09, number); |
301 | spi_send_byte(0x09, number); |
291 | 302 | ||
292 | for (uint8_t i = 0; i < 54; i++) { |
303 | for (uint8_t i = 0; i < 54; i++) { |
293 | // select 4pixel byte of char (CMAL) |
304 | // select 4pixel byte of char (CMAL) |
294 | spi_send_byte(0x0A, i); |
305 | spi_send_byte(0x0A, i); |
295 | 306 | ||
296 | // write 4pixel byte of char (CMDI) |
307 | // write 4pixel byte of char (CMDI) |
297 | spi_send_byte(0x0B, data[i]); |
308 | spi_send_byte(0x0B, data[i]); |
298 | } |
309 | } |
299 | 310 | ||
300 | // write to the NVM array from the shadow RAM (CMM) |
311 | // write to the NVM array from the shadow RAM (CMM) |
301 | spi_send_byte(0x08, 0b10100000); |
312 | spi_send_byte(0x08, 0b10100000); |
302 | 313 | ||
303 | // according to maxim writing to nvram takes about 12ms, lets wait longer |
314 | // according to maxim writing to nvram takes about 12ms, lets wait longer |
304 | _delay_ms(120); |
315 | _delay_ms(120); |
305 | } |
316 | } |
306 | 317 |