summaryrefslogtreecommitdiff
path: root/libraries/MultiLCD/MultiLCD.cpp
blob: 4cf3dd51e825c324022bf66b2eaebc0a79adf299 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*************************************************************************
* Arduino Text Display Library for Multiple LCDs
* Distributed under GPL v2.0
* Copyright (c) 2013 Stanley Huang <stanleyhuangyc@live.com>
* All rights reserved.
*************************************************************************/

#include <Arduino.h>
#include <Wire.h>
#include "MultiLCD.h"

void LCD_Common::printInt(unsigned int value, char padding)
{
    unsigned int den = 10000;
    for (byte i = 5; i > 0; i--, den /= 10) {
        byte v = (byte)(value / den);
        value -= v * den;
        if (v == 0 && padding) {
            if (padding >= i) {
                writeDigit(-1);
            }
            continue;
        }
        padding = 0;
        writeDigit(v);
    }
}

void LCD_Common::printLong(unsigned long value, char padding)
{
    unsigned long den = 1000000000;
    for (byte i = 10; i > 0; i--, den /= 10) {
        byte v = (byte)(value / den);
        value -= v * den;
        if (v == 0 && padding) {
            if (padding >= i) {
                writeDigit(-1);
            }
            continue;
        }
        padding = 0;
        writeDigit(v);
    }
}

void LCD_ZTOLED::setCursor(byte column, byte line)
{
    m_column = column;
    m_page = line;
    ScI2cMxSetLocation(OLED_ADDRESS, m_page, m_column);
    delay(1);
}

size_t LCD_ZTOLED::write(uint8_t c)
{
    if (m_font == FONT_SIZE_SMALL) {
        if (c <= 0x20 || c >= 0x7f) {
            ScI2cMxFillArea(OLED_ADDRESS, m_column, m_column + 5, m_page, m_page, 0);
        } else {
            setCursor(m_column, m_page);
            ScI2cMxDisplayDot(OLED_ADDRESS, font5x8[c - 0x21], 5);
        }
        m_column += 6;
    } else {
        char s[2] = {c};
        ScI2cMxDisplay8x16Str(OLED_ADDRESS, m_page, m_column, s);
        m_column += 8;
        if (m_column >= 128) {
            m_column = 0;
            m_page += 2;
        }
    }
    return 1;
}

/*
void LCD_ZTOLED::print(const char* s)
{
    ScI2cMxDisplay8x16Str(OLED_ADDRESS, m_page, m_column, s);
    m_column += (strlen(s) << 3);
    m_page += (m_column >> 7) << 1;
    m_column %= 0x7f;
}
*/

void LCD_ZTOLED::writeDigit(byte n)
{
    if (n > 9) return;

    if (m_font == FONT_SIZE_SMALL) {
        setCursor(m_column, m_page);
        ScI2cMxDisplayDot(OLED_ADDRESS, font5x8[n + ('0' - 0x21)], 5);
        m_column += 6;
    } else if (m_font == FONT_SIZE_MEDIUM) {
        ScI2cMxDisplayDot(OLED_ADDRESS, digits8x8[n], 8);
        m_column += 8;
    } else if (m_font == FONT_SIZE_LARGE) {
        write('0' + n);
    } else {
        unsigned char data[32];
        if (n >= 0 && n <= 9) {
            memcpy_P(data, digits16x16[n], 32);
        } else {
            memset(data, 0, sizeof(data));
        }
        ScI2cMxDisplayDot16x16(OLED_ADDRESS, m_page, m_column, data);
        m_column += 16;
    }
}

void LCD_ZTOLED::clear()
{
    ScI2cMxFillArea(OLED_ADDRESS, 0, 7, 0, 127, 0);
    delay(10);
    setCursor(0, 0);
}

void LCD_ZTOLED::begin()
{
    I2cInit();
    ScI2cMxReset(OLED_ADDRESS);
    delay(10);
}

void LCD_PCD8544::writeDigit(byte n)
{
    if (m_font == FONT_SIZE_SMALL) {
        write(n >= 0 && n <= 9 ? '0' + n : ' ');
    } else if (m_font == FONT_SIZE_MEDIUM) {
        unsigned char data[8];
        if (n >= 0 && n <= 9) {
            memcpy_P(data, digits8x8[n], 8);
        } else {
            memset(data, 0, sizeof(data));
        }
        draw8x8(data);
    } else {
        unsigned char data[32];
        if (n >= 0 && n <= 9) {
            memcpy_P(data, digits16x16[n], 32);
        } else {
            memset(data, 0, sizeof(data));
        }
        draw16x16(data);
        //column += 16;
    }
}


void LCD_PCD8544::draw(const unsigned char *data, unsigned char x, unsigned char y, unsigned char width, unsigned char height)
{
    height >>= 3;
    for (unsigned char y = 0; y < height; y++) {
        setCursor(x, y);
        for (unsigned char x = 0; x < width; x++) {
            send(PCD8544_DATA, data[y * width + x]);
        }
    }
}

void LCD_SSD1306::setCursor(byte column, byte line)
{
    m_col = column;
    m_row = line;
    ssd1306_command(0xB0 + m_row);//set page address
    ssd1306_command(m_col & 0xf);//set lower column address
    ssd1306_command(0x10 | (m_col >> 4));//set higher column address
}

size_t LCD_SSD1306::write(uint8_t c)
{
    if (m_font == FONT_SIZE_SMALL) {
        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        if (c > 0x20 && c < 0x7f) {
            c -= 0x21;
            for (byte i = 0; i < 5; i++) {
                Wire.write(pgm_read_byte_near(&font5x8[c][i]));
            }
            Wire.write(0);
        } else {
            for (byte i = 0; i < 6; i++) {
                Wire.write(0);
            }
        }
        Wire.endTransmission();
        m_col += 8;
    } else {
        if (c > 0x20 && c < 0x7f) {
            c -= 0x21;

            ssd1306_command(0xB0 + m_row);//set page address
            ssd1306_command(m_col & 0xf);//set lower column address
            ssd1306_command(0x10 | (m_col >> 4));//set higher column address

            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte i = 0; i <= 14; i += 2) {
                Wire.write(pgm_read_byte_near(&font8x16_terminal[c][i]));
            }
            Wire.endTransmission();

            ssd1306_command(0xB0 + m_row + 1);//set page address
            ssd1306_command(m_col & 0xf);//set lower column address
            ssd1306_command(0x10 | (m_col >> 4));//set higher column address

            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte i = 1; i <= 15; i += 2) {
                Wire.write(pgm_read_byte_near(&font8x16_terminal[c][i]));
            }
            Wire.endTransmission();
        } else {
            ssd1306_command(0xB0 + m_row);//set page address
            ssd1306_command(m_col & 0xf);//set lower column address
            ssd1306_command(0x10 | (m_col >> 4));//set higher column address

            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte i = 0; i < 8; i ++) {
                Wire.write(0);
            }
            Wire.endTransmission();

            ssd1306_command(0xB0 + m_row + 1);//set page address
            ssd1306_command(m_col & 0xf);//set lower column address
            ssd1306_command(0x10 | (m_col >> 4));//set higher column address

            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte i = 0; i < 8; i ++) {
                Wire.write(0);
            }
            Wire.endTransmission();
        }
        m_col += 9;
    }
    return 1;
}

void LCD_SSD1306::writeDigit(byte n)
{
    if (n > 9) return;

    uint8_t twbrbackup = TWBR;
    TWBR = 18; // upgrade to 400KHz!
    if (m_font == FONT_SIZE_SMALL) {
        n += '0' - 0x21;
        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (byte i = 0; i < 5; i++) {
            Wire.write(pgm_read_byte_near(&font5x8[n][i]));
        }
        Wire.write(0);
        Wire.endTransmission();
        m_col += 6;
    } else if (m_font == FONT_SIZE_MEDIUM) {
        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (byte i = 0; i < 8; i++) {
            Wire.write(pgm_read_byte_near(&digits8x8[n][i]));
        }
        Wire.endTransmission();
        m_col += 8;
    } else if (m_font == FONT_SIZE_LARGE) {
        n += '0' - 0x21;
        ssd1306_command(0xB0 + m_row);//set page address
        ssd1306_command(m_col & 0xf);//set lower column address
        ssd1306_command(0x10 | (m_col >> 4));//set higher column address

        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (byte i = 0; i <= 14; i += 2) {
            Wire.write(pgm_read_byte_near(&font8x16_terminal[n][i]));
        }
        Wire.endTransmission();

        ssd1306_command(0xB0 + m_row + 1);//set page address
        ssd1306_command(m_col & 0xf);//set lower column address
        ssd1306_command(0x10 | (m_col >> 4));//set higher column address

        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (byte i = 1; i <= 15; i += 2) {
            Wire.write(pgm_read_byte_near(&font8x16_terminal[n][i]));
        }
        Wire.endTransmission();
        m_col += 9;
    } else if (m_font == FONT_SIZE_XLARGE) {
        byte i;
        ssd1306_command(0xB0 + m_row);//set page address
        ssd1306_command(m_col & 0xf);//set lower column address
        ssd1306_command(0x10 | (m_col >> 4));//set higher column address

        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (i = 0; i < 16; i ++) {
            Wire.write(pgm_read_byte_near(&digits16x16[n][i]));
        }
        Wire.endTransmission();

        ssd1306_command(0xB0 + m_row + 1);//set page address
        ssd1306_command(m_col & 0xf);//set lower column address
        ssd1306_command(0x10 | (m_col >> 4));//set higher column address

        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (; i < 32; i ++) {
            Wire.write(pgm_read_byte_near(&digits16x16[n][i]));
        }
        Wire.endTransmission();
        m_col += 16;
    }
    TWBR = twbrbackup;
}

void LCD_SSD1306::draw(const PROGMEM byte* buffer, byte x, byte y, byte width, byte height)
{
    ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
    ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
    ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0

    // save I2C bitrate
    uint8_t twbrbackup = TWBR;
    TWBR = 18; // upgrade to 400KHz!

    const PROGMEM byte *p = buffer;
    height >>= 3;
    width >>= 3;
    y >>= 3;
    for (byte i = 0; i < height; i++) {
      // send a bunch of data in one xmission
        ssd1306_command(0xB0 + i + y);//set page address
        ssd1306_command(x & 0xf);//set lower column address
        ssd1306_command(0x10 | (x >> 4));//set higher column address

        for(byte j = 0; j < 8; j++){
            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte k = 0; k < width; k++, p++) {
                Wire.write(pgm_read_byte_near(p));
            }
            Wire.endTransmission();
        }
    }
    TWBR = twbrbackup;
}

void LCD_SSD1306::clearLine(byte line)
{
    ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
    ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
    ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0

    // save I2C bitrate
    uint8_t twbrbackup = TWBR;
    TWBR = 18; // upgrade to 400KHz!

    // send a bunch of data in one xmission
    ssd1306_command(0xB0 + line);//set page address
    ssd1306_command(0);//set lower column address
    ssd1306_command(0x10);//set higher column address

    for(byte j = 0; j < 8; j++){
        Wire.beginTransmission(_i2caddr);
        Wire.write(0x40);
        for (byte k = 0; k < 16; k++) {
            Wire.write(0);
        }
        Wire.endTransmission();
    }

    TWBR = twbrbackup;
}

void LCD_SSD1306::clear(byte x, byte y, byte width, byte height)
{
    ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
    ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
    ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0

    // save I2C bitrate
    uint8_t twbrbackup = TWBR;
    TWBR = 18; // upgrade to 400KHz!

    height >>= 3;
    width >>= 3;
    y >>= 3;
    for (byte i = 0; i < height; i++) {
      // send a bunch of data in one xmission
        ssd1306_command(0xB0 + i + y);//set page address
        ssd1306_command(x & 0xf);//set lower column address
        ssd1306_command(0x10 | (x >> 4));//set higher column address

        for(byte j = 0; j < 8; j++){
            Wire.beginTransmission(_i2caddr);
            Wire.write(0x40);
            for (byte k = 0; k < width; k++) {
                Wire.write(0);
            }
            Wire.endTransmission();
        }
    }
    TWBR = twbrbackup;
}