2019-07-05 22:01:54 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 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.
|
|
|
|
*/
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
#include "shared-bindings/i2cdisplaybus/I2CDisplayBus.h"
|
2019-07-05 22:01:54 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/busio/I2C.h"
|
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2019-11-15 12:47:00 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2019-07-05 22:01:54 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/time/__init__.h"
|
2019-08-23 18:27:21 -04:00
|
|
|
#include "shared-module/displayio/display_core.h"
|
2019-07-05 22:01:54 -04:00
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_i2cdisplaybus_i2cdisplaybus_construct(i2cdisplaybus_i2cdisplaybus_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
busio_i2c_obj_t *i2c, uint16_t device_address, const mcu_pin_obj_t *reset) {
|
2019-07-05 22:01:54 -04:00
|
|
|
|
2019-11-08 15:35:35 -05:00
|
|
|
// Reset the display before probing
|
|
|
|
self->reset.base.type = &mp_type_NoneType;
|
|
|
|
if (reset != NULL) {
|
|
|
|
self->reset.base.type = &digitalio_digitalinout_type;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&self->reset, reset);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL);
|
2019-11-15 12:47:00 -05:00
|
|
|
common_hal_never_reset_pin(reset);
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_i2cdisplaybus_i2cdisplaybus_reset(self);
|
2019-11-08 15:35:35 -05:00
|
|
|
}
|
|
|
|
|
2019-07-05 22:01:54 -04:00
|
|
|
// Probe the bus to see if a device acknowledges the given address.
|
|
|
|
if (!common_hal_busio_i2c_probe(i2c, device_address)) {
|
2020-09-10 15:37:43 -04:00
|
|
|
self->base.type = &mp_type_NoneType;
|
2023-10-17 19:09:47 -04:00
|
|
|
common_hal_i2cdisplaybus_i2cdisplaybus_deinit(self);
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError_varg(MP_ERROR_TEXT("Unable to find I2C Display at %x"), device_address);
|
2019-07-05 22:01:54 -04:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:35:35 -05:00
|
|
|
// Write to the device and return 0 on success or an appropriate error code from mperrno.h
|
2019-07-05 22:01:54 -04:00
|
|
|
self->bus = i2c;
|
|
|
|
common_hal_busio_i2c_never_reset(self->bus);
|
|
|
|
|
|
|
|
self->address = device_address;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_i2cdisplaybus_i2cdisplaybus_deinit(i2cdisplaybus_i2cdisplaybus_obj_t *self) {
|
2019-07-05 22:01:54 -04:00
|
|
|
if (self->bus == &self->inline_bus) {
|
|
|
|
common_hal_busio_i2c_deinit(self->bus);
|
|
|
|
}
|
2023-10-02 19:49:31 -04:00
|
|
|
// TODO figure out how to undo never_reset. maybe only mark never_reset when
|
|
|
|
// we subsume objects off the mp heap.
|
|
|
|
|
|
|
|
self->bus = NULL;
|
2019-07-05 22:01:54 -04:00
|
|
|
|
2020-09-14 11:58:13 -04:00
|
|
|
if (self->reset.base.type == &digitalio_digitalinout_type) {
|
|
|
|
common_hal_digitalio_digitalinout_deinit(&self->reset);
|
2020-09-10 15:37:43 -04:00
|
|
|
}
|
2019-07-05 22:01:54 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_i2cdisplaybus_i2cdisplaybus_reset(mp_obj_t obj) {
|
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-08-21 14:49:49 -04:00
|
|
|
if (self->reset.base.type == &mp_type_NoneType) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-02 19:17:38 -04:00
|
|
|
|
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->reset, false);
|
2019-08-23 18:27:21 -04:00
|
|
|
common_hal_mcu_delay_us(4);
|
2019-08-02 19:17:38 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(&self->reset, true);
|
2019-08-21 14:49:49 -04:00
|
|
|
return true;
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_i2cdisplaybus_i2cdisplaybus_bus_free(mp_obj_t obj) {
|
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-07-05 22:01:54 -04:00
|
|
|
if (!common_hal_busio_i2c_try_lock(self->bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-03 17:46:47 -04:00
|
|
|
common_hal_busio_i2c_unlock(self->bus);
|
2019-07-05 22:01:54 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
bool common_hal_i2cdisplaybus_i2cdisplaybus_begin_transaction(mp_obj_t obj) {
|
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-08-27 19:03:05 -04:00
|
|
|
return common_hal_busio_i2c_try_lock(self->bus);
|
2019-08-02 19:17:38 -04:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_i2cdisplaybus_i2cdisplaybus_send(mp_obj_t obj, display_byte_type_t data_type,
|
2021-03-15 09:57:36 -04:00
|
|
|
display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) {
|
2023-10-17 19:09:47 -04:00
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-08-23 18:27:21 -04:00
|
|
|
if (data_type == DISPLAY_COMMAND) {
|
2019-07-05 22:01:54 -04:00
|
|
|
uint8_t command_bytes[2 * data_length];
|
|
|
|
for (uint32_t i = 0; i < data_length; i++) {
|
|
|
|
command_bytes[2 * i] = 0x80;
|
|
|
|
command_bytes[2 * i + 1] = data[i];
|
|
|
|
}
|
2022-01-29 22:44:27 -05:00
|
|
|
common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length);
|
2019-07-05 22:01:54 -04:00
|
|
|
} else {
|
|
|
|
uint8_t data_bytes[data_length + 1];
|
|
|
|
data_bytes[0] = 0x40;
|
|
|
|
memcpy(data_bytes + 1, data, data_length);
|
2022-01-29 22:44:27 -05:00
|
|
|
common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1);
|
2019-07-05 22:01:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:09:47 -04:00
|
|
|
void common_hal_i2cdisplaybus_i2cdisplaybus_end_transaction(mp_obj_t obj) {
|
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
2019-07-05 22:01:54 -04:00
|
|
|
common_hal_busio_i2c_unlock(self->bus);
|
|
|
|
}
|
2023-10-02 19:49:31 -04:00
|
|
|
|
|
|
|
void common_hal_i2cdisplaybus_i2cdisplaybus_collect_ptrs(mp_obj_t obj) {
|
|
|
|
i2cdisplaybus_i2cdisplaybus_obj_t *self = MP_OBJ_TO_PTR(obj);
|
|
|
|
gc_collect_ptr((void *)self->bus);
|
|
|
|
}
|