Subversion Repositories Projects

Rev

Rev 489 | Rev 519 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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