diff --git a/Makefile b/Makefile index 1a09ae0..0766b42 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ AVRMCU ?= atmega8 F_CPU ?= 16000000 ISPPORT ?= /dev/kaboard -VERSION = 0.1 +VERSION = 0.2 HEADERS = include/spi.h include/st7735.h include/st7735initcmds.h HEADERS += images/logo_bw.h images/logo.h include/st7735_gfx.h @@ -19,7 +19,7 @@ SIZE = avr-size SRC_TMP = $(subst ../,,$(SRC)) OBJ = $(SRC_TMP:%.c=$(OBJDIR)/$(AVRMCU)/%.o) -CFLAGS = -I include -I images -I fonts -Os -Wall -Wstrict-prototypes +CFLAGS = -I include -I images -I fonts -Os -Wall -Wstrict-prototypes --std=c99 CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -fshort-enums -fpack-struct -funsigned-char -funsigned-bitfields CFLAGS += -mmcu=$(AVRMCU) -DF_CPU=$(F_CPU)UL -DVERSION=$(VERSION) diff --git a/README.md b/README.md index 5d9220e..aa406db 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,50 @@ and parts of https://github.com/adafruit/Adafruit-GFX-Library/ Tweaks ------ +By LongHairedHacker: + * Uses my own bitmap format for fullcolor and monochrome bitmaps * Faster line drawing based on: https://github.com/adafruit/Adafruit-GFX-Library/pull/36 * Faster font rendering based on https://github.com/adafruit/Adafruit-GFX-Library/issues/69 * Refactored draw_char function that does not load glyph struct twice + +By Massey101: + +* Added support for bitmap clipping +* Added JAYCAR screen for XC4629 support + + +Usage +----- + +Connect the display to the following PINS: + +JAYCAR screen and ATMEGA328: ++--------+--------+ +| Screen | AVR | ++--------+--------+ +| VCC | 5V | ++--------+--------+ +| GND | GND | ++--------+--------+ +| CS | PB2 | ++--------+--------+ +| RESET | PD7 | ++--------+--------+ +| A0 | PD6 | ++--------+--------+ +| SDA | PB3 | ++--------+--------+ +| SCK | PB5 | ++--------+--------+ +| LED | 3.3V | ++--------+--------+ + + +1. Set the environment variables for: + - `AVRMCU` - Your avr chip + - `F_CPU` - Your clock speed + - `ISPPORT` - programming device +2. Modify `include/st7735.h:st7735_type` to your screen. (I have no idea how to + figure out which is which) +3. Run `make flash` diff --git a/include/st7735.h b/include/st7735.h index 275db83..d94cafd 100644 --- a/include/st7735.h +++ b/include/st7735.h @@ -16,10 +16,11 @@ enum ST7735_DISPLAY_TYPE { ST7735_RED_18_GREENTAB, ST7735_RED_18_REDTAB, ST7735_RED_18_BLACKTAB, - ST7735_RED144_GREENTAB + ST7735_RED144_GREENTAB, + ST7735_RED144_JAYCAR }; -static const enum ST7735_DISPLAY_TYPE st7735_type = ST7735_RED_18_BLACKTAB; +static const enum ST7735_DISPLAY_TYPE st7735_type = ST7735_RED144_JAYCAR; // ST7735 commands diff --git a/main.c b/main.c index 57f1ee4..938d4e3 100644 --- a/main.c +++ b/main.c @@ -6,10 +6,7 @@ #include "st7735_gfx.h" #include "st7735_font.h" -//#include "logo.h" #include "logo_bw.h" - -//#include "tom_thumb.h" #include "free_sans.h" int main(void) { @@ -19,9 +16,7 @@ int main(void) { st7735_set_orientation(ST7735_LANDSCAPE); st7735_fill_rect(0, 0, 160, 128, ST7735_COLOR_BLACK); - st7735_draw_mono_bitmap(16, 4, &logo_bw, ST7735_COLOR_WHITE, ST7735_COLOR_BLACK); - - //st7735_draw_bitmap(10, 10, &logo); + st7735_draw_mono_bitmap(16, 4, (PGM_P) logo_bw, ST7735_COLOR_WHITE, ST7735_COLOR_BLACK); for(uint8_t x = 8; x <= 160; x += 8) { diff --git a/st7735.c b/st7735.c index 0cd58d8..6dfc37f 100644 --- a/st7735.c +++ b/st7735.c @@ -132,12 +132,22 @@ void st7735_init() { break; case ST7735_RED144_GREENTAB: - st7735_run_command_list(st7735_red_init1); - st7735_run_command_list(st7735_red_init_green1442); - st7735_run_command_list(st7735_red_init3); st7735_height = st7735_default_height_144; st7735_column_start = 2; st7735_row_start = 3; + st7735_run_command_list(st7735_red_init1); + st7735_run_command_list(st7735_red_init_green1442); + st7735_run_command_list(st7735_red_init3); + st7735_width = st7735_default_width; + st7735_height = st7735_default_height_144; + break; + case ST7735_RED144_JAYCAR: + st7735_height = st7735_default_height_144; + st7735_column_start = 32; + st7735_row_start = 0; + st7735_run_command_list(st7735_red_init1); + st7735_run_command_list(st7735_red_init_green1442); + st7735_run_command_list(st7735_red_init3); st7735_width = st7735_default_width; st7735_height = st7735_default_height_144; break; @@ -168,11 +178,19 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) { st7735_width = st7735_default_width; - if(st7735_type == ST7735_RED144_GREENTAB) { - st7735_height = st7735_default_height_144; + if( + st7735_type == ST7735_RED144_GREENTAB || + st7735_type == ST7735_RED144_JAYCAR + ) { + st7735_height = st7735_default_height_144; } else { - st7735_height = st7735_default_height_18; - } + st7735_height = st7735_default_height_18; + } + + if(st7735_type == ST7735_RED144_JAYCAR) { + st7735_column_start = 0; + st7735_row_start = 32; + } break; @@ -183,13 +201,20 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) { st7735_write_data(MADCTL_MY | MADCTL_MV | MADCTL_BGR); } - if(st7735_type == ST7735_RED144_GREENTAB) { + if( + st7735_type == ST7735_RED144_GREENTAB || + st7735_type == ST7735_RED144_JAYCAR + ) { st7735_width = st7735_default_height_144; } else { st7735_width = st7735_default_height_18; } + if(st7735_type == ST7735_RED144_JAYCAR) { + st7735_column_start = 32; + } + st7735_height = st7735_default_width; break; @@ -202,11 +227,19 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) { st7735_width = st7735_default_width; - if(st7735_type == ST7735_RED144_GREENTAB) { + if( + st7735_type == ST7735_RED144_GREENTAB || + st7735_type == ST7735_RED144_JAYCAR + ) { st7735_height = st7735_default_height_144; } else { st7735_height = st7735_default_height_18; } + + if(st7735_type == ST7735_RED144_JAYCAR) { + st7735_column_start = 0; + } + break; case ST7735_LANDSCAPE_INV: @@ -216,12 +249,19 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) { st7735_write_data(MADCTL_MX | MADCTL_MV | MADCTL_BGR); } - if (st7735_type == ST7735_RED144_GREENTAB) { + if( + st7735_type == ST7735_RED144_GREENTAB || + st7735_type == ST7735_RED144_JAYCAR + ) { st7735_width = st7735_default_height_144; } else { st7735_width = st7735_default_height_18; } + if(st7735_type == ST7735_RED144_JAYCAR) { + st7735_column_start = 0; + } + st7735_height = st7735_default_width; break; } @@ -287,29 +327,36 @@ void st7735_fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color void st7735_draw_bitmap(uint8_t x, uint8_t y, PGM_P bitmap) { - if(x >= st7735_width || y >= st7735_height) { - return; - } - uint8_t w = pgm_read_word(bitmap); bitmap += 2; uint8_t h = pgm_read_word(bitmap); bitmap += 2; + uint8_t max_x = x + w - 1; + uint8_t max_y = y + h - 1; - if((x + w - 1) >= st7735_width) { - return; - } - if((y + h - 1) >= st7735_height) { + if(x >= st7735_width || y >= st7735_height) { return; } - st7735_set_addr_win(x, y, x + w - 1, y + h - 1); + if(max_x >= st7735_width) { + max_x = st7735_width - 1; + } + + if(max_y >= st7735_height) { + max_y = st7735_height - 1; + } + + st7735_set_addr_win(x, y, max_x, max_y); st7735_set_rs(); spi_unset_cs(); for(uint8_t i = 0; i < h; i++) { for(uint8_t j = 0; j < w; j++) { + if((x + j) >= st7735_width || (y + i) >= st7735_height) { + bitmap += 2; + continue; + } uint16_t color = pgm_read_word(bitmap); st7735_write_color(color); bitmap += 2; @@ -322,19 +369,22 @@ void st7735_draw_bitmap(uint8_t x, uint8_t y, PGM_P bitmap) { void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_set, uint16_t color_unset) { uint8_t w = pgm_read_byte(bitmap++); uint8_t h = pgm_read_byte(bitmap++); + uint8_t max_x = x + w - 1; + uint8_t max_y = y + h - 1; if(x >= st7735_width || y >= st7735_height) { return; } - if((x + w - 1) >= st7735_width) { - return; - } - if((y + h - 1) >= st7735_height) { - return; + if(max_x >= st7735_width) { + max_x = st7735_width - 1; } - st7735_set_addr_win(x, y, x + w - 1, y + h - 1); + if(max_y >= st7735_height) { + max_y = st7735_height - 1; + } + + st7735_set_addr_win(x, y, max_x, max_y); st7735_set_rs(); spi_unset_cs(); @@ -347,6 +397,10 @@ void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_ byte = pgm_read_byte(bitmap++); } + if((x + j) >= st7735_width || (y + i) >= st7735_height) { + bit_pos++; + continue; + } if(byte & (1 << (bit_pos % 8))) { st7735_write_color(color_set); } @@ -358,5 +412,4 @@ void st7735_draw_mono_bitmap(uint8_t x, uint8_t y, PGM_P bitmap, uint16_t color_ } spi_set_cs(); - }