summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/ssd1351/ssd1351.cxx
blob: 2103603b85d9785050453d8ba1b21c259a4bba3d (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
/*
 * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Based on Adafruit SSD1351 library.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <string>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>

#include "ssd1351.h"

using namespace upm;
using namespace std;

SSD1351::SSD1351 (uint8_t oc, uint8_t dc, uint8_t rst) :
        GFX(SSD1351WIDTH, SSD1351HEIGHT),
        m_spi(0), m_oc(oc), m_dc(dc), m_rst(rst) {

    m_name = "SSD1351";
    m_usemap = true;

    // Setup SPI bus
    m_spi.frequency(8 * 1000000);
    m_spi.mode(mraa::SPI_MODE3);
    m_spi.writeByte(0x00); // Need to bring clk high before init

    // Init pins
    if (m_oc.dir(mraa::DIR_OUT) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Could not initialize CS pin");
        return;
    }
    m_oc.useMmap(true);
    if (m_dc.dir(mraa::DIR_OUT) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Could not initialize data/cmd pin");
        return;
    }
    m_dc.useMmap(true);
    if (m_rst.dir(mraa::DIR_OUT) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Could not initialize reset pin");
        return;
    }

    // Toggle reset pin
    ocLow();
    m_rst.write(1);
    usleep(500000);
    m_rst.write(0);
    usleep(500000);
    m_rst.write(1);
    usleep(500000);

    // Configure and init display
    writeCommand(SSD1351_CMD_COMMANDLOCK);
    writeData(0x12);

    writeCommand(SSD1351_CMD_COMMANDLOCK);
    writeData(0xB1);

    writeCommand(SSD1351_CMD_DISPLAYOFF);

    writeCommand(SSD1351_CMD_CLOCKDIV);
    writeCommand(0xF1);

    writeCommand(SSD1351_CMD_MUXRATIO);
    writeData(127);

    writeCommand(SSD1351_CMD_SETREMAP);
    writeData(0x74);

    writeCommand(SSD1351_CMD_SETCOLUMN);
    writeData(0x00);
    writeData(0x7F);

    writeCommand(SSD1351_CMD_SETROW);
    writeData(0x00);
    writeData(0x7F);

    writeCommand(SSD1351_CMD_STARTLINE);
    if (SSD1351HEIGHT == 96) {
        writeData(96);
    } else {
        writeData(0);
    }

    writeCommand(SSD1351_CMD_DISPLAYOFFSET);
    writeData(0x0);

    writeCommand(SSD1351_CMD_SETGPIO);
    writeData(0x00);

    writeCommand(SSD1351_CMD_FUNCTIONSELECT);
    writeData(0x01);

    writeCommand(SSD1351_CMD_PRECHARGE);
    writeCommand(0x32);

    writeCommand(SSD1351_CMD_VCOMH);
    writeCommand(0x05);

    writeCommand(SSD1351_CMD_NORMALDISPLAY);

    writeCommand(SSD1351_CMD_CONTRASTABC);
    writeData(0xC8);
    writeData(0x80);
    writeData(0xC8);

    writeCommand(SSD1351_CMD_CONTRASTMASTER);
    writeData(0x0F);

    writeCommand(SSD1351_CMD_SETVSL );
    writeData(0xA0);
    writeData(0xB5);
    writeData(0x55);

    writeCommand(SSD1351_CMD_PRECHARGE2);
    writeData(0x01);

    writeCommand(SSD1351_CMD_DISPLAYON);
}

SSD1351::~SSD1351() {
}

void
SSD1351::writeCommand (uint8_t value) {
    dcLow();
    m_spi.writeByte(value);
}

void
SSD1351::writeData (uint8_t value) {
    dcHigh ();
    m_spi.writeByte(value);
}

void
SSD1351::drawPixel(int16_t x, int16_t y, uint16_t color) {
      if ((x < 0) || (y < 0) || (x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
          return;

      if(m_usemap) {
          int index = (y * SSD1351WIDTH + x) * 2;
          m_map[index] = color >> 8;
          m_map[index + 1] = color;
      } else {
          writeCommand(SSD1351_CMD_SETCOLUMN);
          writeData(x);
          writeData(SSD1351WIDTH-1);

          writeCommand(SSD1351_CMD_SETROW);
          writeData(y);
          writeData(SSD1351HEIGHT-1);

          writeCommand(SSD1351_CMD_WRITERAM);
          writeData(color >> 8);
          writeData(color);
      }
}
void
SSD1351::refresh () {
    writeCommand(SSD1351_CMD_WRITERAM);
    int blockSize = SSD1351HEIGHT * SSD1351WIDTH * 2 / BLOCKS;
    dcHigh();
    for (int block = 0; block < BLOCKS; block++) {
        m_spi.write(&m_map[block * blockSize], blockSize);
    }
}
void
SSD1351::ocLow() {
    if (m_oc.write(LOW) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Failed to write CS pin");
    }
}
void
SSD1351::ocHigh() {
    if (m_oc.write(HIGH) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Failed to write CS pin");
    }
}
void
SSD1351::dcLow() {
    if (m_dc.write(LOW) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Failed to write data/cmd pin");
    }
}
void
SSD1351::dcHigh() {
    if (m_dc.write(HIGH) != mraa::SUCCESS) {
        throw std::runtime_error(string(__FUNCTION__) +
                               ": Failed to write data/cmd pin");
    }
}
void
upm::SSD1351::useMemoryMap(bool var) {
    m_usemap = var;
}