aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-module/sharpdisplay/SharpMemoryFramebuffer.c
blob: 285abb1dbe463b1bf75ea92c0a6b679f6e2fcedf (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
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 Jeff Epler for Adafruit Industries
 *
 * 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 <string.h>

#include "py/gc.h"

#include "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"

#include "supervisor/memory.h"

#define SHARPMEM_BIT_WRITECMD_LSB (0x80)
#define SHARPMEM_BIT_VCOM_LSB (0x40)

STATIC uint8_t bitrev(uint8_t n) {
    uint8_t r = 0;
    for (int i = 0; i < 8; i++) {r |= ((n >> i) & 1) << (7 - i);
    }
    return r;
}

int common_hal_sharpdisplay_framebuffer_get_width(sharpdisplay_framebuffer_obj_t *self) {
    return self->width;
}

int common_hal_sharpdisplay_framebuffer_get_height(sharpdisplay_framebuffer_obj_t *self) {
    return self->height;
}

STATIC int common_hal_sharpdisplay_framebuffer_get_row_stride(sharpdisplay_framebuffer_obj_t *self) {
    return (self->width + 7) / 8 + 2;
}

STATIC int common_hal_sharpdisplay_framebuffer_get_first_pixel_offset(sharpdisplay_framebuffer_obj_t *self) {
    return 2;
}

STATIC bool common_hal_sharpdisplay_framebuffer_get_reverse_pixels_in_byte(sharpdisplay_framebuffer_obj_t *self) {
    return true;
}

STATIC bool common_hal_sharpdisplay_framebuffer_get_pixels_in_byte_share_row(sharpdisplay_framebuffer_obj_t *self) {
    return true;
}

void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *self) {
    if (self->bus != &self->inline_bus
        #if CIRCUITPY_BOARD_SPI
        && !common_hal_board_is_spi(self->bus)
        #endif
        ) {
        memcpy(&self->inline_bus, self->bus, sizeof(busio_spi_obj_t));
        self->bus = &self->inline_bus;
    }
}

void common_hal_sharpdisplay_framebuffer_reconstruct(sharpdisplay_framebuffer_obj_t *self) {
    // Look up the allocation by the old pointer and get the new pointer from it.
    supervisor_allocation *alloc = allocation_from_ptr(self->bufinfo.buf);
    self->bufinfo.buf = alloc ? alloc->ptr : NULL;
}

void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_obj_t *self, mp_buffer_info_t *bufinfo) {
    if (!self->bufinfo.buf) {
        int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
        int height = common_hal_sharpdisplay_framebuffer_get_height(self);
        self->bufinfo.len = row_stride * height + 2;
        supervisor_allocation *alloc = allocate_memory(align32_size(self->bufinfo.len), false, true);
        if (alloc == NULL) {
            m_malloc_fail(self->bufinfo.len);
        }
        self->bufinfo.buf = alloc->ptr;
        memset(alloc->ptr, 0, self->bufinfo.len);

        uint8_t *data = self->bufinfo.buf;
        *data++ = SHARPMEM_BIT_WRITECMD_LSB;

        for (int y = 0; y < height; y++) {
            *data = bitrev(y + 1);
            data += row_stride;
        }
        self->full_refresh = true;
    }
    if (bufinfo) {
        *bufinfo = self->bufinfo;
    }
}

void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *self) {
    if (self->base.type != &sharpdisplay_framebuffer_type) {
        return;
    }

    if (self->bus == &self->inline_bus) {
        common_hal_busio_spi_deinit(self->bus);
    }

    common_hal_reset_pin(self->chip_select.pin);

    free_memory(allocation_from_ptr(self->bufinfo.buf));

    memset(self, 0, sizeof(*self));
}

void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_t *self, busio_spi_obj_t *spi, const mcu_pin_obj_t *chip_select, int baudrate, int width, int height) {
    common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
    common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
    common_hal_never_reset_pin(chip_select);

    self->bus = spi;
    common_hal_busio_spi_never_reset(self->bus);

    self->width = width;
    self->height = height;
    self->baudrate = baudrate;

    common_hal_sharpdisplay_framebuffer_get_bufinfo(self, NULL);
}

STATIC void common_hal_sharpdisplay_framebuffer_swapbuffers(sharpdisplay_framebuffer_obj_t *self, uint8_t *dirty_row_bitmask) {
    // claim SPI bus
    if (!common_hal_busio_spi_try_lock(self->bus)) {
        return;
    }
    common_hal_busio_spi_configure(self->bus, self->baudrate, 0, 0, 8);

    // set chip select high
    common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);

    // output the toggling signal
    uint8_t *data = self->bufinfo.buf;
    data[0] ^= SHARPMEM_BIT_VCOM_LSB;

    common_hal_busio_spi_write(self->bus, data++, 1);

    // output each changed row
    size_t row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
    for (int y = 0; y < self->height; y++) {
        if (self->full_refresh || (dirty_row_bitmask[y / 8] & (1 << (y & 7)))) {
            common_hal_busio_spi_write(self->bus, data, row_stride);
        }
        data += row_stride;
    }

    // output a trailing zero
    common_hal_busio_spi_write(self->bus, data, 1);

    // set chip select low
    common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);

    // release SPI bus
    common_hal_busio_spi_unlock(self->bus);

    self->full_refresh = false;
}

STATIC void sharpdisplay_framebuffer_deinit(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    common_hal_sharpdisplay_framebuffer_deinit(self);
}

STATIC void sharpdisplay_framebuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    common_hal_sharpdisplay_framebuffer_get_bufinfo(self, bufinfo);
}

STATIC int sharpdisplay_framebuffer_get_color_depth(mp_obj_t self_in) {
    return 1;
}

STATIC int sharpdisplay_framebuffer_get_height(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_height(self);
}

STATIC int sharpdisplay_framebuffer_get_width(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_width(self);
}

STATIC int sharpdisplay_framebuffer_get_first_pixel_offset(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_first_pixel_offset(self);
}

STATIC bool sharpdisplay_framebuffer_get_pixels_in_byte_share_row(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_pixels_in_byte_share_row(self);
}

STATIC bool sharpdisplay_framebuffer_get_reverse_pixels_in_byte(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_reverse_pixels_in_byte(self);
}

STATIC int sharpdisplay_framebuffer_get_row_stride(mp_obj_t self_in) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    return common_hal_sharpdisplay_framebuffer_get_row_stride(self);
}

STATIC void sharpdisplay_framebuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmask) {
    sharpdisplay_framebuffer_obj_t *self = self_in;
    common_hal_sharpdisplay_framebuffer_swapbuffers(self, dirty_row_bitmask);
}

const framebuffer_p_t sharpdisplay_framebuffer_proto = {
    MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer)
    .deinit = sharpdisplay_framebuffer_deinit,
    .get_bufinfo = sharpdisplay_framebuffer_get_bufinfo,
    .get_color_depth = sharpdisplay_framebuffer_get_color_depth,
    .get_height = sharpdisplay_framebuffer_get_height,
    .get_width = sharpdisplay_framebuffer_get_width,
    .swapbuffers = sharpdisplay_framebuffer_swapbuffers,

    .get_first_pixel_offset = sharpdisplay_framebuffer_get_first_pixel_offset,
    .get_pixels_in_byte_share_row = sharpdisplay_framebuffer_get_pixels_in_byte_share_row,
    .get_reverse_pixels_in_byte = sharpdisplay_framebuffer_get_reverse_pixels_in_byte,
    .get_row_stride = sharpdisplay_framebuffer_get_row_stride,
};

void common_hal_sharpdisplay_framebuffer_collect_ptrs(sharpdisplay_framebuffer_obj_t *self) {
    gc_collect_ptr(self->bus);
}