summaryrefslogtreecommitdiff
path: root/components/slcd/s7leklcd/s7leklcd.c
blob: aa3c8943cc4e937be08bb0f7805a01040da9062e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "s7leklcd.h"
#include <board.h>
#include <utility/assert.h>

//------------------------------------------------------------------------------
//         Local variables
//------------------------------------------------------------------------------

/// List of the 26 SLCD upper chars ( A ~ Z )
static const unsigned short pLcdUpperChars[26] = {
    S7LEKLCD_A, S7LEKLCD_B, S7LEKLCD_C, S7LEKLCD_D,
    S7LEKLCD_E, S7LEKLCD_F, S7LEKLCD_G, S7LEKLCD_H,
    S7LEKLCD_I, S7LEKLCD_J, S7LEKLCD_K, S7LEKLCD_L,
    S7LEKLCD_M, S7LEKLCD_N, S7LEKLCD_O, S7LEKLCD_P,
    S7LEKLCD_Q, S7LEKLCD_R, S7LEKLCD_S, S7LEKLCD_T,
    S7LEKLCD_U, S7LEKLCD_V, S7LEKLCD_W, S7LEKLCD_X,
    S7LEKLCD_Y, S7LEKLCD_Z 
};

/// List of the 26 SLCD lower chars ( a ~ z )
static const unsigned short pLcdLowerChars[26] = {
    S7LEKLCD_a, S7LEKLCD_b, S7LEKLCD_c, S7LEKLCD_d,
    S7LEKLCD_e, S7LEKLCD_f, S7LEKLCD_g, S7LEKLCD_h,
    S7LEKLCD_i, S7LEKLCD_j, S7LEKLCD_k, S7LEKLCD_l,
    S7LEKLCD_m, S7LEKLCD_n, S7LEKLCD_o, S7LEKLCD_p,
    S7LEKLCD_q, S7LEKLCD_r, S7LEKLCD_s, S7LEKLCD_t,
    S7LEKLCD_u, S7LEKLCD_v, S7LEKLCD_w, S7LEKLCD_x,
    S7LEKLCD_y, S7LEKLCD_z 
};

/// List of the 10 SLCD numbers ( 0 ~ 9 )
static const unsigned short pLcdNumbers[10] = {
    S7LEKLCD_0, S7LEKLCD_1, S7LEKLCD_2, S7LEKLCD_3,
    S7LEKLCD_4, S7LEKLCD_5, S7LEKLCD_6, S7LEKLCD_7,
    S7LEKLCD_8, S7LEKLCD_9
};

//------------------------------------------------------------------------------
//         Local functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Returns the LCD character code to display given an input character.
/// \param c  Character to display.
/// \param dot  Indicates if the character should be dotted or not.
//------------------------------------------------------------------------------
static unsigned short GetSLcdChar(unsigned char c, unsigned char dot)
{
    unsigned short symbol = 0;

    // Add dot if needed
    if (dot) {

        symbol = S7LEKLCD_DOT;
    }

    // Find corresponding symbol for character
    // Space
    if (c == ' ') {
    
        symbol |= S7LEKLCD_NONE;
    }
    // Single dot
    else if (c == '.') {
    
        symbol |= S7LEKLCD_NONE | S7LEKLCD_DOT;
    }
    // Number
    else if ((c >= '0') && (c <= '9')) {
    
        symbol |= pLcdNumbers[c - '0'];
    }
    // Lower-case letter
    else if ((c >= 'a') && (c <= 'z')) {
    
        symbol |= pLcdLowerChars[c - 'a'];
    }
    // Upper case letter
    else if ((c >= 'A') && (c <= 'Z')) {
    
        symbol |= pLcdUpperChars[c - 'A'];
    }
    // Special characters
    else {
        switch (c) {
            case '+': symbol |= S7LEKLCD_PLUS; break;
            case '-': symbol |= S7LEKLCD_MINUS; break;
            case '=': symbol |= S7LEKLCD_EQUAL; break;
            case '>': symbol |= S7LEKLCD_LARGE; break;
            case '<': symbol |= S7LEKLCD_LESS; break;
            case '\\': symbol |= S7LEKLCD_SLASH; break;
            case '/': symbol |= S7LEKLCD_BACKSLASH; break;
            case '$': symbol |= S7LEKLCD_DOLLAR; break;
            case '|': symbol |= S7LEKLCD_OR; break;
            case ',': symbol |= S7LEKLCD_COMMA; break;
            case '\'': symbol |= S7LEKLCD_INVCOMMA1; break;
            case '"': symbol |= S7LEKLCD_INVCOMMA2; break;
            case '_': symbol |= D; break;
            default: symbol = S7LEKLCD_NONE;
        }
    }

    return symbol;
}

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Switches a symbol of the LCD on or off.
/// \param symbol  Symbol Index in the LCD buffer matrix.
/// \param set  If 1, the symbol is turned on; otherwise it is turned off.
//------------------------------------------------------------------------------
void S7LEKLCD_Symbol(unsigned short symbol, unsigned char set)
{
    unsigned int common = symbol / 40;
    unsigned int reg = (symbol % 40) / 32;
    unsigned int bit = (symbol % 40) % 32;
    
    if (set) {

        AT91C_BASE_SLCDC->SLCDC_MEM[common * 2 + reg] |= 1 << bit;
    }
    else {

        AT91C_BASE_SLCDC->SLCDC_MEM[common * 2 + reg] &= ~(1 << bit);
    }
}

//------------------------------------------------------------------------------
/// Switches a pixel of the matrix on or off.
/// \param x  Horizontal pixel coordinate.
/// \param y  Vertical pixel coordinate.
/// \param set  If true, the pixel is switched on; otherwise it is switched off.
//------------------------------------------------------------------------------
void S7LEKLCD_Pixel(unsigned char x, unsigned char y, unsigned char set)
{
    SANITY_CHECK(x < 12);
    SANITY_CHECK(y < 10);

    S7LEKLCD_Symbol((x + 1) + (40 * (9 - y)), set);
}

//------------------------------------------------------------------------------
/// Displays a character at the given position on the LCD.
/// \param c  Character definition.
/// \param index  Position of the character on the LCD ([0...11]).
//------------------------------------------------------------------------------
void S7LEKLCD_Char(unsigned short c, unsigned char index)
{
    SANITY_CHECK(index < 12);

    S7LEKLCD_Symbol((40 * 2) + 37 - index * 2 , (c & A) != 0);
    S7LEKLCD_Symbol((40 * 4) + 37 - index * 2 , (c & B) != 0);
    S7LEKLCD_Symbol((40 * 6) + 37 - index * 2 , (c & C) != 0);
    S7LEKLCD_Symbol((40 * 8) + 38 - index * 2 , (c & D) != 0);
    S7LEKLCD_Symbol((40 * 6) + 38 - index * 2 , (c & E) != 0);
    S7LEKLCD_Symbol((40 * 4) + 38 - index * 2 , (c & F) != 0);
    S7LEKLCD_Symbol((40 * 5) + 38 - index * 2 , (c & G) != 0);
    S7LEKLCD_Symbol((40 * 5) + 37 - index * 2 , (c & H) != 0);
    S7LEKLCD_Symbol((40 * 3) + 38 - index * 2 , (c & I) != 0);
    S7LEKLCD_Symbol((40 * 2) + 38 - index * 2 , (c & J) != 0);
    S7LEKLCD_Symbol((40 * 3) + 37 - index * 2 , (c & K) != 0);
    S7LEKLCD_Symbol((40 * 7) + 38 - index * 2 , (c & L) != 0);
    S7LEKLCD_Symbol((40 * 8) + 37 - index * 2 , (c & M) != 0);
    S7LEKLCD_Symbol((40 * 7) + 37 - index * 2 , (c & N) != 0);
    S7LEKLCD_Symbol((40 * 9) + 37 - index * 2 , (c & P) != 0);
}

//------------------------------------------------------------------------------
/// Displays a clock number at the given position.
/// \param c  Number definition.
/// \param index  Position of the character to be displayed ([0..3]).
//------------------------------------------------------------------------------
void S7LEKLCD_ClockNumber(unsigned short c, unsigned char index)
{
    SANITY_CHECK(index < 4);

    S7LEKLCD_Symbol((40 * 0) + 30 - index * 4 , (c & A) != 0);
    S7LEKLCD_Symbol((40 * 0) + 29 - index * 4 , (c & B) != 0);
    S7LEKLCD_Symbol((40 * 0) + 27 - index * 4 , (c & C) != 0);
    S7LEKLCD_Symbol((40 * 1) + 27 - index * 4 , (c & D) != 0);
    S7LEKLCD_Symbol((40 * 1) + 28 - index * 4 , (c & E) != 0);
    S7LEKLCD_Symbol((40 * 1) + 29 - index * 4 , (c & F) != 0);
    S7LEKLCD_Symbol((40 * 0) + 28 - index * 4 , (c & G) != 0);
}

//------------------------------------------------------------------------------
/// Displays a string on the SLCD starting at the specified character index.
/// \param pString  String to display.
/// \param index  Starting index of string.
//------------------------------------------------------------------------------
void S7LEKLCD_PutString(const char *pString, unsigned char index)
{
    unsigned char i;
    unsigned char j;
    unsigned char dot;

    // Clear characters before index
    for (i=0; i < index; i++) {

        S7LEKLCD_Char(S7LEKLCD_NONE, i);
    }

    // Display string
    j = 0;
    dot = 0;
    while ((i < S7LEKLCD_MAX_CHARS) && (pString[j] != 0)) {

        // Check if next character is a dot
        if (pString[j+1] == '.') {

            dot = 1;
        }

        // Skip dots
        if (pString[j] == '.') {

            j++;
        }
        // Display character
        else {

            S7LEKLCD_Char(GetSLcdChar(pString[j], dot), i);
            dot = 0;
            i++;
            j++;
        }
    }

    // Clear characters at end of string
    for (; i < S7LEKLCD_MAX_CHARS; i++) {

        S7LEKLCD_Char(S7LEKLCD_NONE, i);
    }
}

personal git repositories of Harald Welte. Your mileage may vary