aboutsummaryrefslogtreecommitdiffstats
path: root/source/dogs.c
blob: ab0b20d5df02aec4ed3eb5f98bf92f527ffc165a (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
/**
 * dogs.c - Interface for drawing to the EADOGS102N-6 display.
 * Copyright (C) 2020  Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * See the GNU General Public License for more details.
 */

#include "bit-font2.h"
//#include "ch.h"
#include "dogs.h"
#include "hal.h"

#define SET_DATA palSetPad(GPIOC, 15)
#define SET_CMD  palClearPad(GPIOC, 15)
#define CS_HIGH  palSetPad(GPIOA, 4)
#define CS_LOW   palClearPad(GPIOA, 4)
unsigned char dogs_buffer[DISP_WIDTH * DISP_HEIGHT / 8];

//static volatile bool dogs_spi_done = false;

#define spi_send(data, len) spiSend(&SPID1, len, data)
//static void spi_send(unsigned char *data, unsigned int len)
//{
    //dogs_spi_done = false;
    //spiStartSend(&SPID1, len, data);
    //while (!dogs_spi_done)
    //    asm("wfi");

    //for (; len > 0; --len)
    //    spiPolledExchange(&SPID1, *data++);
//}

static void dogs_init_display();
//static void dogs_spi_callback(SPIDriver *spid)
//{
//    if (spiIsBufferComplete(spid))
//        dogs_spi_done = true;
//}

void dogs_init()
{
    // SPI
    palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATE(1));     // MOSI
    palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(0));     // CLK
    palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);  // nCS
    palSetPadMode(GPIOC, 14, PAL_MODE_OUTPUT_PUSHPULL); // nRST
    palSetPadMode(GPIOC, 15, PAL_MODE_OUTPUT_PUSHPULL); // CD

    palSetPad(GPIOA, 4);
    palSetPad(GPIOC, 14);
    palClearPad(GPIOC, 15);

    static const SPIConfig spicfg = {
        false,
        NULL /*dogs_spi_callback*/,
        0, // cr1
        0
    };
    spiStart(&SPID1, &spicfg);

    dogs_init_display();
    dogs_clear();
    dogs_flush();
}

void dogs_write_cmd(unsigned char byte)
{
    spi_send(&byte, 1);
}

void dogs_set_column(unsigned int col)
{
    dogs_write_cmd(0x10 | ((col >> 4) & 0xF));
    dogs_write_cmd(0x00 | (col & 0xF));
}
void dogs_set_power(unsigned int bits)
{
    dogs_write_cmd(0x28 | (bits & 0x7));
}
void dogs_set_scroll_line(unsigned int startline)
{
    dogs_write_cmd(0x40 | (startline & 0x3F));
}
void dogs_set_page(unsigned int page)
{
    dogs_write_cmd(0xB0 | (page & 0xF));
}
void dogs_set_vlcd_ratio(unsigned int ratio)
{
    dogs_write_cmd(0x20 | (ratio & 0x7));
}
void dogs_set_contrast(unsigned int value)
{
    dogs_write_cmd(0x81);
    dogs_write_cmd(0x00 | (value & 0x3F));
}
void dogs_set_pixelson(bool on)
{
    dogs_write_cmd(on ? 0xA5 : 0xA4);
}
void dogs_set_invert(bool invert)
{
    dogs_write_cmd(invert ? 0xA7 : 0xA6);
}
void dogs_set_sleep(bool sleep)
{
    dogs_write_cmd(sleep ? 0xAE : 0xAF);
}
void dogs_set_segdir(bool normal)
{
    dogs_write_cmd(normal ? 0xA0 : 0xA1);
}
void dogs_set_comdir(bool normal)
{
    dogs_write_cmd(normal ? 0xC0 : 0xC8);
}
void dogs_reset()
{
    dogs_write_cmd(0xE2);
}
void dogs_set_bias(bool higher)
{
    dogs_write_cmd(higher ? 0xA2 : 0xA3);
}
void dogs_set_advanced(unsigned int bits)
{
    dogs_write_cmd(0xFA);
    dogs_write_cmd(0x10 | bits);
}

void dogs_init_display()
{
    SET_CMD;
    CS_LOW;
    dogs_reset();
    CS_HIGH;

    unsigned long int reset_sleep = (STM32_SYSCLK / 1000) * 100;
    while (reset_sleep != 0) {
        asm("nop; nop; nop; nop; nop");
        reset_sleep -= 8;
    }

    CS_LOW;
    dogs_set_scroll_line(0);
    dogs_set_segdir(true);
    dogs_set_comdir(false);
    dogs_set_pixelson(false);
    dogs_set_invert(false);
    dogs_set_bias(true);
    dogs_set_power(0x07);
    dogs_set_vlcd_ratio(7);
    dogs_set_contrast(12);
    dogs_set_advanced(0x83);
    dogs_set_sleep(false);
    CS_HIGH;
}

void dogs_clear()
{
    uint32_t *ptr = (uint32_t *)dogs_buffer;
    unsigned int count = sizeof(dogs_buffer) / sizeof(uint32_t) / 12;

    for (; count; --count) {
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
        *ptr++ = 0;
    }
}

void dogs_flush()
{
    unsigned char *ptr = dogs_buffer;
    CS_LOW;
    for (int page = 0; page < 8; ++page) {
        SET_CMD;
        dogs_set_page(page);
        dogs_set_column(30);

        SET_DATA;
        spi_send(ptr, 102);
        ptr += 102;
    }
    CS_HIGH;
}

void draw_pixel(int x, int y, bool state)
{
    if (x < 0 || y < 0 || x >= DISP_WIDTH || y >= DISP_HEIGHT)
        return;
    y = DISP_HEIGHT - 1 - y;
    if (state)
        dogs_buffer[y / 8 * DISP_WIDTH + x] |= (1 << (y % 8));
    else
        dogs_buffer[y / 8 * DISP_WIDTH + x] &= ~(1 << (y % 8));
}

void draw_rect(int x, int y, int w, int h)
{
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++)
            draw_pixel(x + i, y + j, true);
    }
}

void draw_bitmap_wh(int x, int y, int w, int h, const unsigned char *data)
{
    int sbit = 0;

    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            draw_pixel(x + i, y + j, *data & (1 << sbit));
            if (++sbit == 8) {
                sbit = 0;
                ++data;
            }
        }
    }
}

void draw_bitmap(int x, int y, const unsigned char *buffer)
{
    draw_bitmap_wh(x, y, buffer[0], buffer[1], buffer + 2);
}

void draw_glyph(int ix, int iy, unsigned int glyph)
{
    const unsigned char *ptr;
    int x, y, sbit, tbits;

    x = ix + 5;
    y = iy;
    ptr = bitfontGetBitmapLo(glyph);
    sbit = 0;
    tbits = 18;

draw_glyph_loop:
    for (int i = 0; i < tbits; ++i) {
        draw_pixel(x, y, *ptr & (1 << sbit));

        if (x == ix) {
            x += 5;
            ++y;
        } else {
            --x;
        }

        if (sbit == 7) {
            sbit = 0;
            ++ptr;
        } else {
            ++sbit;
        }
    }

    if (tbits == 18) {
        ptr = bitfontGetBitmapHi(glyph);
        sbit = 0;
        tbits = 24;
        goto draw_glyph_loop;
    }
}

void draw_number(int x, int y, int number)
{
    // TODO Handle negatives better?
    if (number < 0)
        number = -number;

    // Count digits
    int tmp = number;
    int count;
    for (count = 0; tmp; count++)
        tmp /= 10;
    if (count == 0)
        count = 1;

    // Draw digits
    x += count * 7;
    do {
        x -= 7;
        draw_glyph(x, y, bitfontGetGlyph('0' + (number % 10)));
    } while (number /= 10);
}