Subversion Repositories Projects

Rev

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