Commit 19794e84 authored by Russ Hughes's avatar Russ Hughes

fixed map_bitarray_to_rgb565, new t-watch firmware

parent 4d0191c3
......@@ -32,9 +32,7 @@ Includes frozen axp202c driver from https://github.com/lewisxhe/AXP202X_Librarie
File | Details
------------- | ----------------------------------------------------------
firmware.bin | MicroPython v1.12-464-gcae77daf0 compiled with ESP IDF v3
firmware2.bin | MicroPython v1.12-662-g8da40baa4 compiled with ESP IDF v4 with Bluetooth
firmware3.bin | MicroPython v1.12-662-g8da40baa4 compiled with ESP IDF v4 with Bluetooth, bma432, focaltouch, ir, st7789, axp202c and frozen vga fonts.
firmware.bin | MicroPython v1.12-665-g60f5b941e compiled with ESP IDF v4 with Bluetooth, focaltouch, ir, st7789, axp202c, frozen vga fonts and updated map_bitarray_to_rgb565.
## Modules
......@@ -80,6 +78,8 @@ ttgo_hello.py | https://youtu.be/z41Du4GDMSY
ttgo_scroll.py | https://youtu.be/GQa-RzHLBak
watch_draw.py | https://www.youtube.com/watch?v=O_lDBnvH1Sw
watch_hello.py | https://youtu.be/Bwq39tuMoY4
watch_bitmap.py |
This is a work in progress.
......
'''
watch_bitarray.py
An example using map_bitarray_to_rgb565 to draw sprites for
the TTGO T-Watch-2020
'''
import time
import random
import machine
import axp202c
import st7789
SPRITES = 100
SPRITE_WIDTH = 16
SPRITE_HEIGHT = 16
SPRITE_STEPS = 3
SPRITE_BITMAPS = [
bytearray([
0b00000000, 0b00000000,
0b00000001, 0b11110000,
0b00000111, 0b11110000,
0b00001111, 0b11100000,
0b00001111, 0b11000000,
0b00011111, 0b10000000,
0b00011111, 0b00000000,
0b00011110, 0b00000000,
0b00011111, 0b00000000,
0b00011111, 0b10000000,
0b00001111, 0b11000000,
0b00001111, 0b11100000,
0b00000111, 0b11110000,
0b00000001, 0b11110000,
0b00000000, 0b00000000,
0b00000000, 0b00000000]),
bytearray([
0b00000000, 0b00000000,
0b00000011, 0b11100000,
0b00001111, 0b11111000,
0b00011111, 0b11111100,
0b00011111, 0b11111100,
0b00111111, 0b11110000,
0b00111111, 0b10000000,
0b00111100, 0b00000000,
0b00111111, 0b10000000,
0b00111111, 0b11110000,
0b00011111, 0b11111100,
0b00011111, 0b11111100,
0b00001111, 0b11111000,
0b00000011, 0b11100000,
0b00000000, 0b00000000,
0b00000000, 0b00000000]),
bytearray([
0b00000000, 0b00000000,
0b00000111, 0b11000000,
0b00011111, 0b11110000,
0b00111111, 0b11111000,
0b00111111, 0b11111000,
0b01111111, 0b11111100,
0b01111111, 0b11111100,
0b01111111, 0b11111100,
0b01111111, 0b11111100,
0b01111111, 0b11111100,
0b00111111, 0b11111000,
0b00111111, 0b11111000,
0b00011111, 0b11110000,
0b00000111, 0b11000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000]),
bytearray([
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000])]
class pacman():
'''
pacman class to keep track of a sprites locaton and step
'''
def __init__(self, x, y, step):
self.x = x
self.y = y
self.step = step
def move(self):
self.step += 1
self.step %= SPRITE_STEPS
self.x += 1
if self.x == 223:
self.step = SPRITE_STEPS
self.x %= 224
def main():
'''
Draw on screen using map_bitarray_to_rgb565
'''
try:
# Turn on display backlight
axp = axp202c.PMU()
axp.enablePower(axp202c.AXP202_LDO2)
# initialize display spi port
spi = machine.SPI(
2,
baudrate=32000000,
polarity=1,
phase=0,
bits=8,
firstbit=0,
sck=machine.Pin(18, machine.Pin.OUT),
mosi=machine.Pin(19, machine.Pin.OUT))
# configure display
tft = st7789.ST7789(
spi,
240,
240,
cs=machine.Pin(5, machine.Pin.OUT),
dc=machine.Pin(27, machine.Pin.OUT),
backlight=machine.Pin(12, machine.Pin.OUT),
rotation=2)
# enable display and clear screen
tft.init()
tft.fill(st7789.BLACK)
sprite = bytearray(512)
# create pacman spites in random positions
sprites = []
for man in range(SPRITES):
sprites.append(
pacman(
random.randint(0, tft.width()-SPRITE_WIDTH),
random.randint(0, tft.height()-SPRITE_HEIGHT),
random.randint(0, SPRITE_STEPS-1)
)
)
# move and draw sprites
while True:
for man in sprites:
# move the sprite
man.move()
# convert bitmap into rgb565 blitable buffer
tft.map_bitarray_to_rgb565(
SPRITE_BITMAPS[man.step],
sprite,
SPRITE_WIDTH,
st7789.YELLOW,
st7789.BLACK)
# blit the buffer to the display
tft.blit_buffer(
sprite,
man.x,
man.y,
SPRITE_WIDTH,
SPRITE_HEIGHT)
time.sleep(0.1)
finally:
# shutdown spi
if 'spi' in locals():
spi.deinit()
# turn off display backlight
axp.disablePower(axp202c.AXP202_LDO2)
main()
......@@ -3,4 +3,4 @@ SRC_USERMOD += $(addprefix $(ST7789_MOD_DIR)/, \
st7789.c \
)
CFLAGS_USERMOD += -I$(ST7789_MOD_DIR) -DMODULE_ST7789_ENABLED=1
# CFLAGS_USERMOD += -DEXPOSE_EXTRA_METHODS=1
CFLAGS_USERMOD += -DEXPOSE_EXTRA_METHODS=1
......@@ -298,6 +298,7 @@ STATIC mp_obj_t st7789_ST7789_pixel(size_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_pixel_obj, 4, 4, st7789_ST7789_pixel);
STATIC mp_obj_t st7789_ST7789_line(size_t n_args, const mp_obj_t *args) {
st7789_ST7789_obj_t *self = MP_OBJ_TO_PTR(args[0]);
mp_int_t x0 = mp_obj_get_int(args[1]);
......@@ -401,7 +402,6 @@ STATIC mp_obj_t st7789_ST7789_text(size_t n_args, const mp_obj_t *args) {
} else
str = mp_obj_str_get_str(args[2]);
// const char *str = mp_obj_str_get_str(args[2]);
mp_int_t x0 = mp_obj_get_int(args[3]);
mp_int_t y0 = mp_obj_get_int(args[4]);
......@@ -691,6 +691,62 @@ STATIC mp_obj_t st7789_ST7789_offset(size_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_offset_obj, 3, 3, st7789_ST7789_offset);
STATIC uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3);
}
STATIC mp_obj_t st7789_color565(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
return MP_OBJ_NEW_SMALL_INT(color565(
(uint8_t)mp_obj_get_int(r),
(uint8_t)mp_obj_get_int(g),
(uint8_t)mp_obj_get_int(b)
));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(st7789_color565_obj, st7789_color565);
STATIC void map_bitarray_to_rgb565(uint8_t const *bitarray, uint8_t *buffer, int length, int width,
uint16_t color, uint16_t bg_color) {
int row_pos = 0;
for (int i = 0; i < length; i++) {
uint8_t byte = bitarray[i];
for (int bi = 7; bi >= 0; bi--) {
uint8_t b = byte & (1 << bi);
uint16_t cur_color = b ? color : bg_color;
*buffer = (cur_color & 0xff00) >> 8;
buffer++;
*buffer = cur_color & 0xff;
buffer++;
row_pos++;
if (row_pos >= width) {
row_pos = 0;
break;
}
}
}
}
// bitarray buffer width color bg_color
STATIC mp_obj_t st7789_map_bitarray_to_rgb565(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bitarray_info;
mp_buffer_info_t buffer_info;
mp_get_buffer_raise(args[1], &bitarray_info, MP_BUFFER_READ);
mp_get_buffer_raise(args[2], &buffer_info, MP_BUFFER_WRITE);
mp_int_t width = mp_obj_get_int(args[3]);
mp_int_t color = mp_obj_get_int(args[4]);
mp_int_t bg_color = mp_obj_get_int(args[5]);
map_bitarray_to_rgb565(bitarray_info.buf, buffer_info.buf, bitarray_info.len, width, color, bg_color);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_map_bitarray_to_rgb565_obj, 3, 6, st7789_map_bitarray_to_rgb565);
STATIC const mp_rom_map_elem_t st7789_ST7789_locals_dict_table[] = {
// Do not expose internal functions to fit iram_0 section
#ifdef EXPOSE_EXTRA_METHODS
......@@ -700,6 +756,7 @@ STATIC const mp_rom_map_elem_t st7789_ST7789_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_sleep_mode), MP_ROM_PTR(&st7789_ST7789_sleep_mode_obj) },
{ MP_ROM_QSTR(MP_QSTR_inversion_mode), MP_ROM_PTR(&st7789_ST7789_inversion_mode_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_window), MP_ROM_PTR(&st7789_ST7789_set_window_obj) },
{ MP_ROM_QSTR(MP_QSTR_map_bitarray_to_rgb565), MP_ROM_PTR(&st7789_map_bitarray_to_rgb565_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&st7789_ST7789_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&st7789_ST7789_pixel_obj) },
......@@ -793,71 +850,6 @@ mp_obj_t st7789_ST7789_make_new(const mp_obj_type_t *type,
}
STATIC uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3);
}
STATIC mp_obj_t st7789_color565(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
return MP_OBJ_NEW_SMALL_INT(color565(
(uint8_t)mp_obj_get_int(r),
(uint8_t)mp_obj_get_int(g),
(uint8_t)mp_obj_get_int(b)
));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(st7789_color565_obj, st7789_color565);
STATIC void map_bitarray_to_rgb565(uint8_t const *bitarray, uint8_t *buffer, int length, int width,
uint16_t color, uint16_t bg_color) {
int row_pos = 0;
for (int i = 0; i < length; i++) {
uint8_t byte = bitarray[i];
for (int bi = 7; bi >= 0; bi--) {
uint8_t b = byte & (1 << bi);
uint16_t cur_color = b ? color : bg_color;
*buffer = (cur_color & 0xff00) >> 8;
buffer ++;
*buffer = cur_color & 0xff;
buffer ++;
row_pos ++;
if (row_pos >= width) {
row_pos = 0;
break;
}
}
}
}
STATIC mp_obj_t st7789_map_bitarray_to_rgb565(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_bitarray, ARG_buffer, ARG_width, ARG_color, ARG_bg_color };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bitarray, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = -1} },
{ MP_QSTR_color, MP_ARG_INT, {.u_int = WHITE} },
{ MP_QSTR_bg_color, MP_ARG_INT, {.u_int = BLACK } },
};
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_buffer_info_t bitarray_info;
mp_buffer_info_t buffer_info;
mp_get_buffer_raise(args[ARG_bitarray].u_obj, &bitarray_info, MP_BUFFER_READ);
mp_get_buffer_raise(args[ARG_buffer].u_obj, &buffer_info, MP_BUFFER_WRITE);
mp_int_t width = args[ARG_width].u_int;
mp_int_t color = args[ARG_color].u_int;
mp_int_t bg_color = args[ARG_bg_color].u_int;
map_bitarray_to_rgb565(bitarray_info.buf, buffer_info.buf, bitarray_info.len, width, color, bg_color);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(st7789_map_bitarray_to_rgb565_obj, 3, st7789_map_bitarray_to_rgb565);
STATIC const mp_map_elem_t st7789_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_st7789) },
{ MP_ROM_QSTR(MP_QSTR_color565), (mp_obj_t)&st7789_color565_obj },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment