Commit f0cc6c89 authored by russhughes's avatar russhughes

Merge branch 'c-logic-patch-1'

parents b3022817 5942b7ae
......@@ -323,6 +323,15 @@ I was not able to run the display with a baud rate over 40MHZ.
Fill a rectangle starting from (`x`, `y`) coordinates
- `circle(x, y, r, color)`
Draws a circle with radius `r` centered at the (`x`, `y`) coordinates in the given
`color`.
- `fill_circle(x, y, r, color)`
Draws a filled circle with radius `r` centered at the (`x`, `y`) coordinates in the given `color`.
- `blit_buffer(buffer, x, y, width, height)`
Copy bytes() or bytearray() content to the screen internal memory.
......@@ -345,7 +354,7 @@ I was not able to run the display with a baud rate over 40MHZ.
arguments `fg` and `bg`, otherwise the foreground color defaults to `WHITE`
and the background color defaults to `BLACK`. See the `README.md` in the
`truetype/fonts` directory for example fonts. Returns the width of the string
as printed in pixels.
as printed in pixels. Accepts UTF8 encoded strings.
The `font2bitmap` utility creates compatible 1 bit per pixel bitmap modules
from Proportional or Monospaced True Type fonts. The character size,
......
"""
circles.py
Draw circles in random colors at random locations on a
LILYGO® TTGO T-Display.
"""
import random
from machine import Pin, SPI
import st7789
def main():
tft = st7789.ST7789(
SPI(1, baudrate=30000000, sck=Pin(18), mosi=Pin(19)),
135,
240,
reset=Pin(23, Pin.OUT),
cs=Pin(5, Pin.OUT),
dc=Pin(16, Pin.OUT),
backlight=Pin(4, Pin.OUT),
rotation=3)
tft.init()
radius_max = 25
while True:
for rotation in range(4):
tft.rotation(rotation)
tft.fill(0)
col_max = tft.width() - radius_max
row_max = tft.height() - radius_max
for _ in range(128):
tft.fill_circle(
random.randint(0, col_max),
random.randint(0, row_max),
random.randint(0, radius_max),
st7789.color565(
random.getrandbits(8),
random.getrandbits(8),
random.getrandbits(8)))
tft.circle(
random.randint(0, col_max),
random.randint(0, row_max),
random.randint(0, radius_max),
st7789.color565(
random.getrandbits(8),
random.getrandbits(8),
random.getrandbits(8)))
main()
......@@ -696,10 +696,9 @@ STATIC mp_obj_t st7789_ST7789_write(size_t n_args, const mp_obj_t *args)
CS_LOW();
write_spi(self->spi_obj, (uint8_t *) self->i2c_buffer, data_size);
CS_HIGH();
print_width += width;
}
print_width += width;
x += width;
break;
}
char_index++;
......@@ -1084,6 +1083,90 @@ STATIC mp_obj_t st7789_ST7789_vline(size_t n_args, const mp_obj_t *args)
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_vline_obj, 5, 5, st7789_ST7789_vline);
// Circle/Fill_Circle by https://github.com/c-logic
// https://github.com/russhughes/st7789_mpy/pull/46
// https://github.com/c-logic/st7789_mpy.git patch-1
STATIC mp_obj_t st7789_ST7789_circle(size_t n_args, const mp_obj_t *args)
{
st7789_ST7789_obj_t *self = MP_OBJ_TO_PTR(args[0]);
mp_int_t xm = mp_obj_get_int(args[1]);
mp_int_t ym = mp_obj_get_int(args[2]);
mp_int_t r = mp_obj_get_int(args[3]);
mp_int_t color = mp_obj_get_int(args[4]);
mp_int_t f = 1 - r;
mp_int_t ddF_x = 1;
mp_int_t ddF_y = -2 * r;
mp_int_t x = 0;
mp_int_t y = r;
draw_pixel(self, xm, ym + r, color);
draw_pixel(self, xm, ym - r, color);
draw_pixel(self, xm + r, ym, color);
draw_pixel(self, xm - r, ym, color);
while(x < y) {
if(f >= 0) {
y -= 1;
ddF_y += 2;
f += ddF_y;
}
x += 1;
ddF_x += 2;
f += ddF_x;
draw_pixel(self, xm + x, ym + y, color);
draw_pixel(self, xm - x, ym + y, color);
draw_pixel(self, xm + x, ym - y, color);
draw_pixel(self, xm - x, ym - y, color);
draw_pixel(self, xm + y, ym + x, color);
draw_pixel(self, xm - y, ym + x, color);
draw_pixel(self, xm + y, ym - x, color);
draw_pixel(self, xm - y, ym - x, color);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_circle_obj, 5, 5, st7789_ST7789_circle);
// Circle/Fill_Circle by https://github.com/c-logic
// https://github.com/russhughes/st7789_mpy/pull/46
// https://github.com/c-logic/st7789_mpy.git patch-1
STATIC mp_obj_t st7789_ST7789_fill_circle(size_t n_args, const mp_obj_t *args)
{
st7789_ST7789_obj_t *self = MP_OBJ_TO_PTR(args[0]);
mp_int_t xm = mp_obj_get_int(args[1]);
mp_int_t ym = mp_obj_get_int(args[2]);
mp_int_t r = mp_obj_get_int(args[3]);
mp_int_t color = mp_obj_get_int(args[4]);
mp_int_t f = 1 - r;
mp_int_t ddF_x = 1;
mp_int_t ddF_y = -2 * r;
mp_int_t x = 0;
mp_int_t y = r;
fast_vline(self, xm, ym - y, 2 * y + 1, color);
while(x < y) {
if(f >= 0) {
y -= 1;
ddF_y += 2;
f += ddF_y;
}
x += 1;
ddF_x += 2;
f += ddF_x;
fast_vline(self, xm + x, ym - y, 2 * y + 1, color);
fast_vline(self, xm + y, ym - x, 2 * x + 1, color);
fast_vline(self, xm - x, ym - y, 2 * y + 1, color);
fast_vline(self, xm - y, ym - x, 2 * x + 1, color);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_fill_circle_obj, 5, 5, st7789_ST7789_fill_circle);
STATIC mp_obj_t st7789_ST7789_rect(size_t n_args, const mp_obj_t *args)
{
st7789_ST7789_obj_t *self = MP_OBJ_TO_PTR(args[0]);
......@@ -1099,6 +1182,7 @@ STATIC mp_obj_t st7789_ST7789_rect(size_t n_args, const mp_obj_t *args)
fast_vline(self, x + w - 1, y, h, color);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(st7789_ST7789_rect_obj, 6, 6, st7789_ST7789_rect);
STATIC mp_obj_t st7789_ST7789_madctl(size_t n_args, const mp_obj_t *args)
......@@ -1714,6 +1798,8 @@ STATIC const mp_rom_map_elem_t st7789_ST7789_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&st7789_ST7789_fill_obj)},
{MP_ROM_QSTR(MP_QSTR_hline), MP_ROM_PTR(&st7789_ST7789_hline_obj)},
{MP_ROM_QSTR(MP_QSTR_vline), MP_ROM_PTR(&st7789_ST7789_vline_obj)},
{MP_ROM_QSTR(MP_QSTR_fill_circle), MP_ROM_PTR(&st7789_ST7789_fill_circle_obj)},
{MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&st7789_ST7789_circle_obj)},
{MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&st7789_ST7789_rect_obj)},
{MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&st7789_ST7789_text_obj)},
{MP_ROM_QSTR(MP_QSTR_rotation), MP_ROM_PTR(&st7789_ST7789_rotation_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