Subversion Repositories Projects

Rev

Rev 346 | Go to most recent revision | Details | 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
 
21
#include "max7456_software_spi.h"
22
 
23
/* ##########################################################################
24
 * MAX7456 SPI & Display stuff
25
 * ##########################################################################*/
26
 
27
/**
28
 * Send a byte through SPI
29
 */
30
void spi_send(uint8_t byte) {
31
    for (int8_t i = 7; i >= 0; i--) {
32
        if (((byte >> i) & 1)) {
33
            MAX_SDIN_HIGH
34
        } else {
35
            MAX_SDIN_LOW
36
        }
37
        MAX_SCLK_HIGH
38
        MAX_SCLK_LOW
39
    }
40
}
41
 
42
/**
43
 *  Send <byte> to <address> of MAX7456
44
 */
45
void spi_send_byte(uint8_t address, uint8_t byte) {
46
    // start sending
47
    MAX_CS_LOW
48
 
49
    spi_send(address);
50
    spi_send(byte);
51
 
52
    // end sending
53
    MAX_CS_HIGH
54
}
55
 
56
/**
57
 *  write a <character> to <address> of MAX7456 display memory
58
 */
59
void write_char(uint16_t address, char character) {
60
    spi_send_byte(0x05, (address & 0xFF00) >> 8); // DMAH
61
    spi_send_byte(0x06, (address & 0x00FF)); // DMAL
62
    spi_send_byte(0x07, character); // DMDI
63
}
64
 
65
/**
66
 *  write a character <attribute> to <address> of MAX7456 display memory
67
 */
68
void write_char_att(uint16_t address, char attribute) {
69
    // the only important part is that the DMAH[1] is set
70
    // so we add 2 which binary is the 2nd lowest byte
71
    spi_send_byte(0x05, ((address & 0xFF00) >> 8) | 2); // DMAH
72
    spi_send_byte(0x06, (address & 0x00FF)); // DMAL
73
    spi_send_byte(0x07, attribute); // DMDI
74
}
75
 
76
/**
77
 *  write a <character> at <x>/<y> to MAX7456 display memory
78
 */
79
void write_char_xy(uint8_t x, uint8_t y, char character) {
80
    uint16_t address = y * 30 + x;
81
    write_char(address, character);
82
}
83
 
84
/**
85
 *  write a  character <attribute> at <x>/<y> to MAX7456 display memory
86
 */
87
void write_char_att_xy(uint8_t x, uint8_t y, char attribute) {
88
    uint16_t address = y * 30 + x;
89
    write_char_att(address, attribute);
90
}
91
 
92
/**
93
 *  clear display by writing blank characters all over it
94
 */
95
void clear(void) {
96
    uint16_t memory_address = 0;
97
    for (unsigned int a = 0; a < 480; a++) {
98
        write_char(memory_address++, 0);
99
    }
100
}
101
 
102
/**
103
 *  write an ascii <character> to <address> of MAX7456 display memory
104
 */
105
void write_ascii_char(uint16_t address, char c) {
106
    if (c == 32) c = 0; // remap space
107
    else if (c > 48 && c <= 57) c -= 48; // remap numbers
108
    else if (c == '0') c = 10; // remap zero
109
    else if (c >= 65 && c <= 90) c -= 54; // remap big letters
110
    else if (c >= 97 && c <= 122) c -= 60; // remap small letters
111
    else if (c == '(') c = 63; // remap
112
    else if (c == ')') c = 64; // remap
113
    else if (c == '.') c = 65; // remap
114
    else if (c == '?') c = 66; // remap
115
    else if (c == ';') c = 67; // remap
116
    else if (c == ':') c = 68; // remap
117
    else if (c == ',') c = 69; // remap
118
    else if (c == '\'') c = 70; // remap
119
    else if (c == '/') c = 71; // remap
120
    else if (c == '"') c = 72; // remap
121
    else if (c == '-') c = 73; // remap minus
122
    else if (c == '<') c = 74; // remap
123
    else if (c == '>') c = 75; // remap
124
    else if (c == '@') c = 76; // remap
125
    write_char(address, c);
126
}
127
 
128
/**
129
 *  write an ascii <string> at <x>/<y> to MAX7456 display memory
130
 */
131
void write_ascii_string(uint8_t x, uint8_t y, char *string) {
132
    while (*string) {
133
        write_ascii_char(((x++)+(y * 30)), *string);
134
        string++;
135
    }
136
}
137
 
138
/**
139
 *  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
141
 *  like compass only taking three characters (values <= 999)
142
 */
143
void write_3digit_number_u(uint8_t x, uint8_t y, uint16_t number) {
144
    uint16_t num = 100;
145
    uint8_t started = 0;
146
 
147
    while (num > 0) {
148
        uint8_t b = number / num;
149
        if (b > 0 || started || num == 1) {
150
            write_ascii_char((x++)+(y * 30), '0' + b);
151
            started = 1;
152
        } else {
153
            write_ascii_char((x++)+(y * 30), 0);
154
        }
155
        number -= b * num;
156
 
157
        num /= 10;
158
    }
159
}
160
 
161
/**
162
 *  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
164
 *  like seconds only taking two characters (values <= 99)
165
 *  Since this is used for seconds only and it looks better, there
166
 *  is a trading 0 attached
167
 */
168
void write_2digit_number_u(uint8_t x, uint8_t y, uint16_t number) {
169
    uint16_t num = 10;
170
    uint8_t started = 0;
171
 
172
    while (num > 0) {
173
        uint8_t b = number / num;
174
        if (b > 0 || started || num == 1) {
175
            write_ascii_char((x++)+(y * 30), '0' + b);
176
            started = 1;
177
        } else {
178
            write_ascii_char((x++)+(y * 30), '0');
179
        }
180
        number -= b * num;
181
 
182
        num /= 10;
183
    }
184
}
185
 
186
/**
187
 *  write a unsigned number as /10th at <x>/<y> to MAX7456 display memory
188
 */
189
void write_number_u_10th(uint8_t x, uint8_t y, uint16_t number) {
190
    uint16_t num = 10000;
191
    uint8_t started = 0;
192
 
193
    while (num > 0) {
194
        uint8_t b = number / num;
195
 
196
        if (b > 0 || started || num == 1) {
197
            if ((num / 10) == 0) write_char((x++)+(y * 30), 65);
198
            write_ascii_char((x++)+(y * 30), '0' + b);
199
            started = 1;
200
        } else {
201
            write_ascii_char((x++)+(y * 30), 0);
202
        }
203
        number -= b * num;
204
 
205
        num /= 10;
206
    }
207
}
208
 
209
/**
210
 *  write a unsigned number at <x>/<y> to MAX7456 display memory
211
 */
212
void write_number_u(uint8_t x, uint8_t y, uint16_t number) {
213
    uint16_t num = 10000;
214
    uint8_t started = 0;
215
 
216
    while (num > 0) {
217
        uint8_t b = number / num;
218
        if (b > 0 || started || num == 1) {
219
            write_ascii_char((x++)+(y * 30), '0' + b);
220
            started = 1;
221
        } else {
222
            write_ascii_char((x++)+(y * 30), 0);
223
        }
224
        number -= b * num;
225
 
226
        num /= 10;
227
    }
228
}
229
 
230
/**
231
 *  write a signed number at <x>/<y> to MAX7456 display memory
232
 */
233
void write_number_s(uint8_t x, uint8_t y, int16_t w) {
234
    if (((uint16_t) w) > 32767) {
235
        w = w - 65536;
236
 
237
        int16_t num = -10000;
238
        uint8_t started = 0;
239
 
240
        while (num < 0) {
241
            uint8_t b = w / num;
242
            if (b > 0 || started || num == 1) {
243
                if (!started) write_char((x - 1)+(y * 30), 0x49);
244
                write_ascii_char((x++)+(y * 30), '0' + b);
245
                started = 1;
246
            } else {
247
                write_ascii_char((x++)+(y * 30), 0);
248
            }
249
            w -= b * num;
250
 
251
            num /= 10;
252
        }
253
    } else {
254
        write_char((x)+(y * 30), 0);
255
        write_number_u(x, y, w);
256
    }
257
}
258
 
259
/**
260
 *  write <seconds> as human readable time at <x>/<y> to MAX7456 display mem
261
 */
262
void write_time(uint8_t x, uint8_t y, uint16_t seconds) {
263
    uint16_t min = seconds / 60;
264
    seconds -= min * 60;
265
    write_3digit_number_u(x, y, min);
266
    write_char_xy(x + 3, y, 68);
267
    write_2digit_number_u(x + 4, y, seconds);
268
}
269
 
270
/**
271
 * for testing write all chars to screen
272
 */
273
void write_all_chars() {
274
    uint16_t x = 3, y = 2, t = 0;
275
    while (t < 256) {
276
        write_char_xy(x++, y, t++);
277
        if (x > 25) {
278
            y++;
279
            x = 3;
280
        }
281
    }
282
}
283
 
284
/**
285
 * let the MAX7456 learn a new character at <number>
286
 * with <data>.
287
 */
288
void learn_char(uint8_t number, unsigned char* data) {
289
    // select character to write (CMAH)
290
    spi_send_byte(0x09, number);
291
 
292
    for (uint8_t i = 0; i < 54; i++) {
293
        // select 4pixel byte of char (CMAL)
294
        spi_send_byte(0x0A, i);
295
 
296
        // write 4pixel byte of char (CMDI)
297
        spi_send_byte(0x0B, data[i]);
298
    }
299
 
300
    // write to the NVM array from the shadow RAM (CMM)
301
    spi_send_byte(0x08, 0b10100000);
302
 
303
    // according to maxim writing to nvram takes about 12ms, lets wait longer
304
    _delay_ms(120);
305
}