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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/displayio/I2CDisplay.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t* self,
|
|
|
|
busio_i2c_obj_t* i2c, uint16_t device_address, const mcu_pin_obj_t* reset) {
|
|
|
|
|
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);
|
2019-11-08 15:35:35 -05:00
|
|
|
common_hal_displayio_i2cdisplay_reset(self);
|
|
|
|
}
|
|
|
|
|
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;
|
2019-07-05 22:01:54 -04:00
|
|
|
mp_raise_ValueError_varg(translate("Unable to find I2C Display at %x"), device_address);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
// Our object is statically allocated off the heap so make sure the bus object lives to the end
|
|
|
|
// of the heap as well.
|
|
|
|
gc_never_free(self->bus);
|
|
|
|
|
|
|
|
self->address = device_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t* self) {
|
|
|
|
if (self->bus == &self->inline_bus) {
|
|
|
|
common_hal_busio_i2c_deinit(self->bus);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-21 14:49:49 -04:00
|
|
|
bool common_hal_displayio_i2cdisplay_reset(mp_obj_t obj) {
|
2019-08-02 19:17:38 -04:00
|
|
|
displayio_i2cdisplay_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
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_displayio_i2cdisplay_bus_free(mp_obj_t obj) {
|
2019-07-05 22:01:54 -04:00
|
|
|
displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:17:38 -04:00
|
|
|
bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t obj) {
|
2019-09-03 17:46:47 -04:00
|
|
|
displayio_i2cdisplay_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
|
|
|
}
|
|
|
|
|
2019-08-23 18:27:21 -04:00
|
|
|
void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, display_byte_type_t data_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length) {
|
2019-07-05 22:01:54 -04:00
|
|
|
displayio_i2cdisplay_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];
|
|
|
|
}
|
|
|
|
common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length, true);
|
|
|
|
} else {
|
|
|
|
uint8_t data_bytes[data_length + 1];
|
|
|
|
data_bytes[0] = 0x40;
|
|
|
|
memcpy(data_bytes + 1, data, data_length);
|
|
|
|
common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t obj) {
|
|
|
|
displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj);
|
|
|
|
common_hal_busio_i2c_unlock(self->bus);
|
|
|
|
}
|