summaryrefslogtreecommitdiff
path: root/shared-bindings/displayio/ParallelBus.c
blob: 1ce1e2aff41d44033beff790a9e700b5efb38d30 (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
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Scott Shawcroft 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 "shared-bindings/displayio/ParallelBus.h"

#include <stdint.h>

#include "lib/utils/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "shared-module/displayio/__init__.h"
#include "supervisor/shared/translate.h"

//| .. currentmodule:: displayio
//|
//| :class:`ParallelBus` -- Manage updating a display over SPI four wire protocol
//| ==============================================================================
//|
//| Manage updating a display over SPI four wire protocol in the background while Python code runs.
//| It doesn't handle display initialization.
//|
//| .. warning:: This will be changed before 4.0.0. Consider it very experimental.
//|
//| .. class:: ParallelBus(*, data0, command, chip_select, write, read, reset)
//|
//|   Create a ParallelBus object associated with the given pins. The bus is inferred from data0
//|   by implying the next 7 additional pins on a given GPIO port.
//|
//|   :param microcontroller.Pin: The first data pin. The rest are implied
//|   :param microcontroller.Pin command: Data or command pin
//|   :param microcontroller.Pin chip_select: Chip select pin
//|   :param microcontroller.Pin write: Write pin
//|   :param microcontroller.Pin read: Read pin
//|   :param microcontroller.Pin reset: Reset pin
//|
STATIC mp_obj_t displayio_parallelbus_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    enum { ARG_data0, ARG_command, ARG_chip_select, ARG_write, ARG_read, ARG_reset };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_data0, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
        { MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
        { MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
        { MP_QSTR_write, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
        { MP_QSTR_read, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
        { MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
    };
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    mp_obj_t data0 = args[ARG_data0].u_obj;
    mp_obj_t command = args[ARG_command].u_obj;
    mp_obj_t chip_select = args[ARG_chip_select].u_obj;
    mp_obj_t write = args[ARG_write].u_obj;
    mp_obj_t read = args[ARG_read].u_obj;
    mp_obj_t reset = args[ARG_reset].u_obj;
    assert_pin_free(data0);
    assert_pin_free(command);
    assert_pin_free(chip_select);
    assert_pin_free(write);
    assert_pin_free(read);
    assert_pin_free(reset);

    displayio_parallelbus_obj_t* self = NULL;
    for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
        if (displays[i].parallel_bus.base.type== NULL ||
            displays[i].parallel_bus.base.type == &mp_type_NoneType) {
            self = &displays[i].parallel_bus;
            self->base.type = &displayio_parallelbus_type;
            break;
        }
    }
    if (self == NULL) {
        mp_raise_RuntimeError(translate("Too many display busses"));
    }

    common_hal_displayio_parallelbus_construct(self, data0, command, chip_select, write, read, reset);
    return self;
}

STATIC const mp_rom_map_elem_t displayio_parallelbus_locals_dict_table[] = {
};
STATIC MP_DEFINE_CONST_DICT(displayio_parallelbus_locals_dict, displayio_parallelbus_locals_dict_table);

const mp_obj_type_t displayio_parallelbus_type = {
    { &mp_type_type },
    .name = MP_QSTR_ParallelBus,
    .make_new = displayio_parallelbus_make_new,
    .locals_dict = (mp_obj_dict_t*)&displayio_parallelbus_locals_dict,
};