Subversion Repositories Projects

Rev

Rev 379 | Rev 403 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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