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
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at>                |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 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.                                            |
+----------------------------------------------------------------------------+
*/

#ifndef GATE_APPS_MCU_SSD1306_DRIVER_H_INCLUDED
#define GATE_APPS_MCU_SSD1306_DRIVER_H_INCLUDED

#include "gate/io/gpiodevices.hpp"

/* definitions based on ADAFRUIT ARDUINO SSD1306 library */

#define SSD1306_BLACK 0
#define SSD1306_WHITE 1
#define SSD1306_INVERSE 2

#define SSD1306_MEMORYMODE 0x20
#define SSD1306_COLUMNADDR 0x21
#define SSD1306_PAGEADDR 0x22
#define SSD1306_SETCONTRAST 0x81
#define SSD1306_CHARGEPUMP 0x8D
#define SSD1306_SEGREMAP 0xA0
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_DISPLAYALLON 0xA5
#define SSD1306_NORMALDISPLAY 0xA6
#define SSD1306_INVERTDISPLAY 0xA7
#define SSD1306_SETMULTIPLEX 0xA8
#define SSD1306_DISPLAYOFF 0xAE
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_COMSCANINC 0xC0
#define SSD1306_COMSCANDEC 0xC8
#define SSD1306_SETDISPLAYOFFSET 0xD3
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
#define SSD1306_SETPRECHARGE 0xD9
#define SSD1306_SETCOMPINS 0xDA
#define SSD1306_SETVCOMDETECT 0xDB

#define SSD1306_SETLOWCOLUMN 0x00
#define SSD1306_SETHIGHCOLUMN 0x10
#define SSD1306_SETSTARTLINE 0x40

#define SSD1306_EXTERNALVCC 0x01
#define SSD1306_SWITCHCAPVCC 0x02

#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
#define SSD1306_DEACTIVATE_SCROLL 0x2E
#define SSD1306_ACTIVATE_SCROLL 0x2F
#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3

namespace gate
{
namespace io
{
namespace drivers
{
namespace details
{

    template<unsigned WIDTH, unsigned HEIGHT>
    struct SDD1306Config
    {
        static unsigned const ComPins = 0x02;
        static unsigned getContrast(bool_t externalVcc) { return 0x8f; }
    };

    template<> struct SDD1306Config<128, 32>
    {
        static unsigned const ComPins = 0x02;
        static unsigned getContrast(bool_t externalVcc) { return 0x8f; }
    };

    template<> struct SDD1306Config<128, 64>
    {
        static unsigned const ComPins = 0x12;
        static unsigned getContrast(bool_t externalVcc) { return externalVcc ? 0x9f : 0xcf; }
    };

    template<> struct SDD1306Config<96, 16>
    {
        static unsigned const ComPins = 0x02;
        static unsigned getContrast(bool_t externalVcc) { return externalVcc ? 0x10 : 0xaf; }
    };

    template<> struct SDD1306Config<64, 32>
    {
        static unsigned const ComPins = 0x12;
        static unsigned getContrast(bool_t externalVcc) { return externalVcc ? 0x10 : 0xcf; }
    };

    template<unsigned ADDR, unsigned WIDTH, unsigned HEIGHT>
    class SDD1306DisplayBase
    {
    protected:
        unsigned char       screen[WIDTH * HEIGHT / 8];

    protected:
        static void sendCommand(io::I2CDevice& dev, unsigned char initByte, unsigned char cmd)
        {
            unsigned char buffer[2];
            buffer[0] = initByte;
            buffer[1] = cmd;
            dev.trySend(ADDR, &buffer[0], sizeof(buffer));
        }
        
        static void sendCommandList(io::I2CDevice& dev, unsigned char initByte, unsigned char const* lst, size_t count)
        {
            unsigned char const* blocks[2];
            size_t blockLengths[2];
            blocks[0] = &initByte;
            blockLengths[0] = 1;
            blocks[1] = lst;
            blockLengths[1] = count;

            dev.trySend(ADDR, (void const* const*)&blocks[0], &blockLengths[0], 2);
        }

        template<unsigned BLOCKCOUNT>
        static void sendCommandLists(io::I2CDevice& dev, unsigned char initByte, unsigned char const* const* commandBlocks, size_t const* commandBlocksLength)
        {
            unsigned char const* blocks[BLOCKCOUNT + 1];
            size_t blockLengths[BLOCKCOUNT + 1];
            blocks[0] = &initByte;
            blockLengths[0] = 1;
            for (unsigned n = 0; n != BLOCKCOUNT; ++n)
            {
                blocks[n + 1] = commandBlocks[n];
                blockLengths[n + 1] = commandBlocksLength[n];
            }
            dev.trySend(ADDR, (void const* const*)&blocks[0], &blockLengths[0], BLOCKCOUNT + 1);
        }

        void initDev(io::I2CDevice& dev, bool_t externalVcc)
        {
            {
                // init sequence 1
                static unsigned char const init1[] = {
                    SSD1306_DISPLAYOFF,
                    SSD1306_SETDISPLAYCLOCKDIV,
                    0x80,
                    SSD1306_SETMULTIPLEX,
                    HEIGHT - 1
                };
                sendCommandList(dev, 0x00, init1, sizeof(init1));
            }

            {
                // init sequence 2
                static unsigned char const init2[] = {
                    SSD1306_SETDISPLAYOFFSET,
                    0x0,
                    SSD1306_SETSTARTLINE | 0x0,
                    SSD1306_CHARGEPUMP,
                    0x14
                };
                unsigned char init2Param = externalVcc ? 0x10 : 0x14;
                static unsigned char const* const init2Blocks[] = {
                    &init2[0],
                    &init2Param
                };
                static size_t const init2BlocksLength[] = {
                    sizeof(init2),
                    1
                };
                sendCommandLists<2>(dev, 0x00, init2Blocks, init2BlocksLength);
            }

            {
                // init sequence 3
                static unsigned char const init3[] = {
                    SSD1306_MEMORYMODE,
                    0x00,
                    SSD1306_SEGREMAP | 0x1,
                    SSD1306_COMSCANDEC
                };
                sendCommandList(dev, 0x00, init3, sizeof(init3));
            }

            {
                // init sequence 4
                unsigned char const comPins = SDD1306Config<WIDTH, HEIGHT>::ComPins;
                unsigned char const contrast = SDD1306Config<WIDTH, HEIGHT>::getContrast(externalVcc);
                unsigned char const init4[] = {
                    SSD1306_SETCOMPINS,
                    comPins,
                    SSD1306_SETCONTRAST,
                    contrast,
                    SSD1306_SETPRECHARGE,
                    0xF1
                };
                sendCommandList(dev, 0x00, init4, sizeof(init4));
            }

            {
                // init sequence 5
                static unsigned char const init5[] = {
                    SSD1306_SETVCOMDETECT,
                    0x40,
                    SSD1306_DISPLAYALLON_RESUME,
                    SSD1306_NORMALDISPLAY,
                    SSD1306_DEACTIVATE_SCROLL,
                    SSD1306_DISPLAYON
                };
                sendCommandList(dev, 0x00, init5, sizeof(init5));
            }
        }

        void renderScreen(io::I2CDevice& dev)
        {
            static unsigned char const message[] = {
                SSD1306_PAGEADDR,
                0,
                0xff,
                SSD1306_COLUMNADDR,
                (WIDTH == 64) ? 0x20 : 0x00,
                (WIDTH == 64) ? 0x20 + WIDTH - 1 : WIDTH - 1
            };
            sendCommandList(dev, 0x00, message, sizeof(message));

            unsigned char const* ptr = this->screen;
            size_t len = sizeof(this->screen);
            size_t chunkLen = 16;<--- Variable 'chunkLen' is assigned a value that is never used.
            while (len > 16)
            {
                sendCommandList(dev, 0x40, ptr, 16);
                ptr += 16;
                len -= 16;
            }
            if (len > 0)
            {
                sendCommandList(dev, 0x40, ptr, len);
            }
        }


    public:
        unsigned getWidth() const
        {
            return WIDTH;
        }

        unsigned getHeight() const
        {
            return HEIGHT;
        }

        unsigned getAddress() const
        {
            return ADDR;
        }

        void clear()
        {
            Mem::clear(this->screen, sizeof(this->screen));
        }

        SDD1306DisplayBase()
        {
            this->clear();
        }

        static unsigned getByteOffset(unsigned x, unsigned y)
        {
            return (y / 8) * WIDTH + x;
        }

        static unsigned char getBitOffset(unsigned x, unsigned y)
        {
            return (unsigned char)(y & 0x07);
        }

        void setPixel(unsigned x, unsigned y)
        {
            unsigned const bytePos = getByteOffset(x, y);
            unsigned char const bitPos = getBitOffset(x, y);
            unsigned char* ptrPos = &this->screen[bytePos];
            *ptrPos |= (unsigned char)((unsigned char)1 << bitPos);
        }

        void clearPixel(unsigned x, unsigned y)
        {
            unsigned const bytePos = getByteOffset(x, y);
            unsigned char const bitPos = getBitOffset(x, y);
            unsigned char* ptrPos = &this->screen[bytePos];
            *ptrPos &= ~(unsigned char)((unsigned char)1 << bitPos);
        }
    };

}   // end of namespace details


    template<unsigned ADDR, unsigned WIDTH, unsigned HEIGHT>
    class GATE_API_LOCAL SDD1306Display : public details::SDD1306DisplayBase<ADDR, WIDTH, HEIGHT>
    {
    public:
        typedef details::SDD1306DisplayBase<ADDR, WIDTH, HEIGHT> base_t;

        SDD1306Display(String const& devicePath)
        :   base_t(),
            dev(devicePath, 0)
        {
        }

        void init()
        {
            base_t::initDev(this->dev, false);
        }

        void render()
        {
            base_t::renderScreen(this->dev);
        }

    private:
        io::I2CDevice   dev;
    };


    typedef SDD1306Display<0x3c, 128, 32>   SDD1306Display128x32;
    typedef SDD1306Display<0x3c, 128, 64>   SDD1306Display128x64;
    typedef SDD1306Display<0x3c, 96, 16>    SDD1306Display96x16;

}   // end of namespace drivers
}   // end of namespace io
}   // end of namespace gate

#endif