+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
c12dcba
master
1/*************************************************** 2This is a library for the Adafruit 1.8" SPI display. 3This library works with the Adafruit 1.8" TFT Breakout w/SD card 4----> http://www.adafruit.com/products/358 5as well as Adafruit raw 1.8" TFT display 6----> http://www.adafruit.com/products/618 7 8Check out the links above for our tutorials and wiring diagrams 9These displays use SPI to communicate, 4 or 5 pins are required to 10interface (RST is optional) 11Adafruit invests time and resources providing this open source code, 12please support Adafruit and open-source hardware by purchasing 13products from Adafruit! 14 15Written by Limor Fried/Ladyada for Adafruit Industries. 16MIT license, all text above must be included in any redistribution 17****************************************************/ 18 19// ST7735.c 20// Runs on LM4F120/TM4C123 21// Low level drivers for the ST7735 160x128 LCD based off of 22// the file described above. 23// 16-bit color, 128 wide by 160 high LCD 24// Daniel Valvano 25// March 28, 2016 26// Augmented 7/17/2014 to have a simple graphics facility 27// Tested with LaunchPadDLL.dll simulator 9/2/2014 28// Modified for use of CS (simultaneous with other SPI slaves) 3/28/2016 29 30/* This example accompanies the book 31"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", 32ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2016 33 34Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu 35You may use, edit, run or distribute this file 36as long as the above copyright notice remains 37THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 38OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 39MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 40VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 41OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 42For more information about my classes, my research, and my books, see 43http://users.ece.utexas.edu/~valvano/ 44*/ 45 46// hardware connections 47// **********ST7735 TFT and SDC******************* 48// ST7735 49// Backlight (pin 10) connected to +3.3 V 50// MISO (pin 9) unconnected 51// SCK (pin 8) connected to PA2 (SSI0Clk) 52// MOSI (pin 7) connected to PA5 (SSI0Tx) 53// TFT_CS (pin 6) connected to PA3 (SSI0Fss) 54// CARD_CS (pin 5) unconnected 55// Data/Command (pin 4) connected to PA6 (GPIO), high for data, low for command 56// RESET (pin 3) connected to PA7 (GPIO) 57// VCC (pin 2) connected to +3.3 V 58// Gnd (pin 1) connected to ground 59 60// **********wide.hk ST7735R with ADXL345 accelerometer ******************* 61// Silkscreen Label (SDC side up; LCD side down) - Connection 62// VCC - +3.3 V 63// GND - Ground 64// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 65// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 66// DC - PA6 TFT data/command 67// RES - PA7 TFT reset 68// CS - PA3 TFT_CS, active low to enable TFT 69// *CS - (NC) SDC_CS, active low to enable SDC 70// MISO - (NC) MISO SPI data from SDC to microcontroller 71// SDA � (NC) I2C data for ADXL345 accelerometer 72// SCL � (NC) I2C clock for ADXL345 accelerometer 73// SDO � (NC) I2C alternate address for ADXL345 accelerometer 74// Backlight + - Light, backlight connected to +3.3 V 75 76// **********wide.hk ST7735R with ADXL335 accelerometer ******************* 77// Silkscreen Label (SDC side up; LCD side down) - Connection 78// VCC - +3.3 V 79// GND - Ground 80// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 81// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 82// DC - PA6 TFT data/command 83// RES - PA7 TFT reset 84// CS - PA3 TFT_CS, active low to enable TFT 85// *CS - (NC) SDC_CS, active low to enable SDC 86// MISO - (NC) MISO SPI data from SDC to microcontroller 87// X� (NC) analog input X-axis from ADXL335 accelerometer 88// Y� (NC) analog input Y-axis from ADXL335 accelerometer 89// Z� (NC) analog input Z-axis from ADXL335 accelerometer 90// Backlight + - Light, backlight connected to +3.3 V 91 92// **********HiLetgo ST7735 TFT and SDC (SDC not tested)******************* 93// ST7735 94// LED- (pin 16) TFT, connected to ground 95// LED+ (pin 15) TFT, connected to +3.3 V 96// SD_CS (pin 14) SDC, chip select 97// MOSI (pin 13) SDC, MOSI 98// MISO (pin 12) SDC, MISO 99// SCK (pin 11) SDC, serial clock 100// CS (pin 10) TFT, PA3 (SSI0Fss) 101// SCL (pin 9) TFT, SCK PA2 (SSI0Clk) 102// SDA (pin 8) TFT, MOSI PA5 (SSI0Tx) 103// A0 (pin 7) TFT, Data/Command PA6 (GPIO), high for data, low for command 104// RESET (pin 6) TFT, to PA7 (GPIO) 105// NC (pins 3,4,5) not connected 106// VCC (pin 2) connected to +3.3 V 107// GND (pin 1) connected to ground 108 109#include <stdio.h> 110#include <stdint.h> 111#include "ST7735.h" 112#include "../inc/tm4c123gh6pm.h" 113 114// 16 rows (0 to 15) and 21 characters (0 to 20) 115// Requires (11 + size*size*6*8) bytes of transmission for each character 116uint32_t StX = 0 ;// position along the horizonal axis 0 to 20 117uint32_t StY = 0 ;// position along the vertical axis 0 to 15 118uint16_t StTextColor = ST7735_YELLOW ; 119 120#define ST7735_NOP 0x00 121#define ST7735_SWRESET 0x01 122#define ST7735_RDDID 0x04 123#define ST7735_RDDST 0x09 124 125#define ST7735_SLPIN 0x10 126#define ST7735_SLPOUT 0x11 127#define ST7735_PTLON 0x12 128#define ST7735_NORON 0x13 129 130#define ST7735_INVOFF 0x20 131#define ST7735_INVON 0x21 132#define ST7735_DISPOFF 0x28 133#define ST7735_DISPON 0x29 134#define ST7735_CASET 0x2A 135#define ST7735_RASET 0x2B 136#define ST7735_RAMWR 0x2C 137#define ST7735_RAMRD 0x2E 138 139#define ST7735_PTLAR 0x30 140#define ST7735_COLMOD 0x3A 141#define ST7735_MADCTL 0x36 142 143#define ST7735_FRMCTR1 0xB1 144#define ST7735_FRMCTR2 0xB2 145#define ST7735_FRMCTR3 0xB3 146#define ST7735_INVCTR 0xB4 147#define ST7735_DISSET5 0xB6 148 149#define ST7735_PWCTR1 0xC0 150#define ST7735_PWCTR2 0xC1 151#define ST7735_PWCTR3 0xC2 152#define ST7735_PWCTR4 0xC3 153#define ST7735_PWCTR5 0xC4 154#define ST7735_VMCTR1 0xC5 155 156#define ST7735_RDID1 0xDA 157#define ST7735_RDID2 0xDB 158#define ST7735_RDID3 0xDC 159#define ST7735_RDID4 0xDD 160 161#define ST7735_PWCTR6 0xFC 162 163#define ST7735_GMCTRP1 0xE0 164#define ST7735_GMCTRN1 0xE1 165 166#define TFT_CS (*((volatile uint32_t *)0x40004020)) 167#define TFT_CS_LOW 0 // CS normally controlled by hardware 168#define TFT_CS_HIGH 0x08 169#define DC (*((volatile uint32_t *)0x40004100)) 170#define DC_COMMAND 0 171#define DC_DATA 0x40 172#define RESET (*((volatile uint32_t *)0x40004200)) 173#define RESET_LOW 0 174#define RESET_HIGH 0x80 175 176#define SSI_CR0_SCR_M 0x0000FF00 // SSI Serial Clock Rate 177#define SSI_CR0_SPH 0x00000080 // SSI Serial Clock Phase 178#define SSI_CR0_SPO 0x00000040 // SSI Serial Clock Polarity 179#define SSI_CR0_FRF_M 0x00000030 // SSI Frame Format Select 180#define SSI_CR0_FRF_MOTO 0x00000000 // Freescale SPI Frame Format 181#define SSI_CR0_DSS_M 0x0000000F // SSI Data Size Select 182#define SSI_CR0_DSS_8 0x00000007 // 8-bit data 183#define SSI_CR1_MS 0x00000004 // SSI Master/Slave Select 184#define SSI_CR1_SSE 0x00000002 // SSI Synchronous Serial Port 185// Enable 186#define SSI_SR_BSY 0x00000010 // SSI Busy Bit 187#define SSI_SR_TNF 0x00000002 // SSI Transmit FIFO Not Full 188#define SSI_CPSR_CPSDVSR_M 0x000000FF // SSI Clock Prescale Divisor 189#define SSI_CC_CS_M 0x0000000F // SSI Baud Clock Source 190#define SSI_CC_CS_SYSPLL 0x00000000 // Either the system clock (if the 191// PLL bypass is in effect) or the 192// PLL output (default) 193#define SYSCTL_RCGC1_SSI0 0x00000010 // SSI0 Clock Gating Control 194#define SYSCTL_RCGC2_GPIOA 0x00000001 // port A Clock Gating Control 195#define ST7735_TFTWIDTH 128 196#define ST7735_TFTHEIGHT 160 197 198#define ST7735_NOP 0x00 199#define ST7735_SWRESET 0x01 200#define ST7735_RDDID 0x04 201#define ST7735_RDDST 0x09 202 203#define ST7735_SLPIN 0x10 204#define ST7735_SLPOUT 0x11 205#define ST7735_PTLON 0x12 206#define ST7735_NORON 0x13 207 208#define ST7735_INVOFF 0x20 209#define ST7735_INVON 0x21 210#define ST7735_DISPOFF 0x28 211#define ST7735_DISPON 0x29 212#define ST7735_CASET 0x2A 213#define ST7735_RASET 0x2B 214#define ST7735_RAMWR 0x2C 215#define ST7735_RAMRD 0x2E 216 217#define ST7735_PTLAR 0x30 218#define ST7735_COLMOD 0x3A 219#define ST7735_MADCTL 0x36 220 221#define ST7735_FRMCTR1 0xB1 222#define ST7735_FRMCTR2 0xB2 223#define ST7735_FRMCTR3 0xB3 224#define ST7735_INVCTR 0xB4 225#define ST7735_DISSET5 0xB6 226 227#define ST7735_PWCTR1 0xC0 228#define ST7735_PWCTR2 0xC1 229#define ST7735_PWCTR3 0xC2 230#define ST7735_PWCTR4 0xC3 231#define ST7735_PWCTR5 0xC4 232#define ST7735_VMCTR1 0xC5 233 234#define ST7735_RDID1 0xDA 235#define ST7735_RDID2 0xDB 236#define ST7735_RDID3 0xDC 237#define ST7735_RDID4 0xDD 238 239#define ST7735_PWCTR6 0xFC 240 241#define ST7735_GMCTRP1 0xE0 242#define ST7735_GMCTRN1 0xE1 243 244// standard ascii 5x7 font 245// originally from glcdfont.c from Adafruit project 246static const uint8_t Font []= { 2470x00 ,0x00 ,0x00 ,0x00 ,0x00 , 2480x3E ,0x5B ,0x4F ,0x5B ,0x3E , 2490x3E ,0x6B ,0x4F ,0x6B ,0x3E , 2500x1C ,0x3E ,0x7C ,0x3E ,0x1C , 2510x18 ,0x3C ,0x7E ,0x3C ,0x18 , 2520x1C ,0x57 ,0x7D ,0x57 ,0x1C , 2530x1C ,0x5E ,0x7F ,0x5E ,0x1C , 2540x00 ,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,0x41 ,0x41 ,0x51 ,0x73 ,// G 3190x7F ,0x08 ,0x08 ,0x08 ,0x7F ,// H 3200x00 ,0x41 ,0x7F ,0x41 ,0x00 ,// I 3210x20 ,0x40 ,0x41 ,0x3F ,0x01 ,// J 3220x7F ,0x08 ,0x14 ,0x22 ,0x41 ,// K 3230x7F ,0x40 ,0x40 ,0x40 ,0x40 ,// L 3240x7F ,0x02 ,0x1C ,0x02 ,0x7F ,// M 3250x7F ,0x04 ,0x08 ,0x10 ,0x7F ,// N 3260x3E ,0x41 ,0x41 ,0x41 ,0x3E ,// O 3270x7F ,0x09 ,0x09 ,0x09 ,0x06 ,// P 3280x3E ,0x41 ,0x51 ,0x21 ,0x5E ,// Q 3290x7F ,0x09 ,0x19 ,0x29 ,0x46 ,// R 3300x26 ,0x49 ,0x49 ,0x49 ,0x32 ,// S 3310x03 ,0x01 ,0x7F ,0x01 ,0x03 ,// T 3320x3F ,0x40 ,0x40 ,0x40 ,0x3F ,// U 3330x1F ,0x20 ,0x40 ,0x20 ,0x1F ,// V 3340x3F ,0x40 ,0x38 ,0x40 ,0x3F ,// W 3350x63 ,0x14 ,0x08 ,0x14 ,0x63 ,// X 3360x03 ,0x04 ,0x78 ,0x04 ,0x03 ,// Y 3370x61 ,0x59 ,0x49 ,0x4D ,0x43 ,// Z 3380x00 ,0x7F ,0x41 ,0x41 ,0x41 , 3390x02 ,0x04 ,0x08 ,0x10 ,0x20 , 3400x00 ,0x41 ,0x41 ,0x41 ,0x7F , 3410x04 ,0x02 ,0x01 ,0x02 ,0x04 , 3420x40 ,0x40 ,0x40 ,0x40 ,0x40 , 3430x00 ,0x03 ,0x07 ,0x08 ,0x00 , 3440x20 ,0x54 ,0x54 ,0x78 ,0x40 ,// a 3450x7F ,0x28 ,0x44 ,0x44 ,0x38 ,// b 3460x38 ,0x44 ,0x44 ,0x44 ,0x28 ,// c 3470x38 ,0x44 ,0x44 ,0x28 ,0x7F ,// d 3480x38 ,0x54 ,0x54 ,0x54 ,0x18 ,// e 3490x00 ,0x08 ,0x7E ,0x09 ,0x02 ,// f 3500x18 ,0xA4 ,0xA4 ,0x9C ,0x78 ,// g 3510x7F ,0x08 ,0x04 ,0x04 ,0x78 ,// h 3520x00 ,0x44 ,0x7D ,0x40 ,0x00 ,// i 3530x20 ,0x40 ,0x40 ,0x3D ,0x00 ,// j 3540x7F ,0x10 ,0x28 ,0x44 ,0x00 ,// k 3550x00 ,0x41 ,0x7F ,0x40 ,0x00 ,// l 3560x7C ,0x04 ,0x78 ,0x04 ,0x78 ,// m 3570x7C ,0x08 ,0x04 ,0x04 ,0x78 ,// n 3580x38 ,0x44 ,0x44 ,0x44 ,0x38 ,// o 3590xFC ,0x18 ,0x24 ,0x24 ,0x18 ,// p 3600x18 ,0x24 ,0x24 ,0x18 ,0xFC ,// q 3610x7C ,0x08 ,0x04 ,0x04 ,0x08 ,// r 3620x48 ,0x54 ,0x54 ,0x54 ,0x24 ,// s 3630x04 ,0x04 ,0x3F ,0x44 ,0x24 ,// t 3640x3C ,0x40 ,0x40 ,0x20 ,0x7C ,// u 3650x1C ,0x20 ,0x40 ,0x20 ,0x1C ,// v 3660x3C ,0x40 ,0x30 ,0x40 ,0x3C ,// w 3670x44 ,0x28 ,0x10 ,0x28 ,0x44 ,// x 3680x4C ,0x90 ,0x90 ,0x90 ,0x7C ,// y 3690x44 ,0x64 ,0x54 ,0x4C ,0x44 ,// z 3700x00 ,0x08 ,0x36 ,0x41 ,0x00 , 3710x00 ,0x00 ,0x77 ,0x00 ,0x00 , 3720x00 ,0x41 ,0x36 ,0x08 ,0x00 , 3730x02 ,0x01 ,0x02 ,0x04 ,0x02 , 3740x3C ,0x26 ,0x23 ,0x26 ,0x3C , 3750x1E ,0xA1 ,0xA1 ,0x61 ,0x12 , 3760x3A ,0x40 ,0x40 ,0x20 ,0x7A , 3770x38 ,0x54 ,0x54 ,0x55 ,0x59 , 3780x21 ,0x55 ,0x55 ,0x79 ,0x41 , 3790x21 ,0x54 ,0x54 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0x02 ,0x02 , 4780x99 ,0xA5 ,0xE7 ,0xA5 ,0x99 , 4790x1C ,0x2A ,0x49 ,0x2A ,0x1C , 4800x4C ,0x72 ,0x01 ,0x72 ,0x4C , 4810x30 ,0x4A ,0x4D ,0x4D ,0x30 , 4820x30 ,0x48 ,0x78 ,0x48 ,0x30 , 4830xBC ,0x62 ,0x5A ,0x46 ,0x3D , 4840x3E ,0x49 ,0x49 ,0x49 ,0x00 , 4850x7E ,0x01 ,0x01 ,0x01 ,0x7E , 4860x2A ,0x2A ,0x2A ,0x2A ,0x2A , 4870x44 ,0x44 ,0x5F ,0x44 ,0x44 , 4880x40 ,0x51 ,0x4A ,0x44 ,0x40 , 4890x40 ,0x44 ,0x4A ,0x51 ,0x40 , 4900x00 ,0x00 ,0xFF ,0x01 ,0x03 , 4910xE0 ,0x80 ,0xFF ,0x00 ,0x00 , 4920x08 ,0x08 ,0x6B ,0x6B ,0x08 , 4930x36 ,0x12 ,0x36 ,0x24 ,0x36 , 4940x06 ,0x0F ,0x09 ,0x0F ,0x06 , 4950x00 ,0x00 ,0x18 ,0x18 ,0x00 , 4960x00 ,0x00 ,0x10 ,0x10 ,0x00 , 4970x30 ,0x40 ,0xFF ,0x01 ,0x01 , 4980x00 ,0x1F ,0x01 ,0x01 ,0x1E , 4990x00 ,0x19 ,0x1D ,0x17 ,0x12 , 5000x00 ,0x3C ,0x3C ,0x3C ,0x3C , 5010x00 ,0x00 ,0x00 ,0x00 ,0x00 , 502}; 503 504 505static uint8_t ColStart ,RowStart ;// some displays need this changed 506static uint8_t Rotation ;// 0 to 3 507static enum initRFlags TabColor ; 508static int16_t _width = ST7735_TFTWIDTH ;// this could probably be a constant, except it is used in Adafruit_GFX and depends on image rotation 509static int16_t _height = ST7735_TFTHEIGHT ; 510 511 512// The Data/Command pin must be valid when the eighth bit is 513// sent. The SSI module has hardware input and output FIFOs 514// that are 8 locations deep. Based on the observation that 515// the LCD interface tends to send a few commands and then a 516// lot of data, the FIFOs are not used when writing 517// commands, and they are used when writing data. This 518// ensures that the Data/Command pin status matches the byte 519// that is actually being transmitted. 520// The write command operation waits until all data has been 521// sent, configures the Data/Command pin for commands, sends 522// the command, and then waits for the transmission to 523// finish. 524// The write data operation waits until there is room in the 525// transmit FIFO, configures the Data/Command pin for data, 526// and then adds the data to the transmit FIFO. 527// NOTE: These functions will crash or stall indefinitely if 528// the SSI0 module is not initialized and enabled. 529void static writecommand (uint8_t c ) { 530// wait until SSI0 not busy/transmit FIFO empty 531while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 532TFT_CS = TFT_CS_LOW ; 533DC = DC_COMMAND ; 534SSI0_DR_R = c ;// data out 535// wait until SSI0 not busy/transmit FIFO empty 536while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 537} 538 539 540void static writedata (uint8_t c ) { 541while ((SSI0_SR_R & SSI_SR_TNF )== 0 ){};// wait until transmit FIFO not full 542DC = DC_DATA ; 543SSI0_DR_R = c ;// data out 544} 545 546void static deselect (void ) { 547// wait until SSI0 not busy/transmit FIFO empty 548while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 549TFT_CS = TFT_CS_HIGH ; 550} 551 552// Subroutine to wait 1 msec 553// Inputs: None 554// Outputs: None 555// Notes: ... 556void Delay1ms (uint32_t n ){uint32_t volatile time ; 557while (n ){ 558time = 72724 * 2 /91 ;// 1msec, tuned at 80 MHz 559while (time ){ 560time -- ; 561 } 562n -- ; 563 } 564} 565 566// Rather than a bazillion writecommand() and writedata() calls, screen 567// initialization commands and arguments are organized in these tables 568// stored in ROM. The table may look bulky, but that's mostly the 569// formatting -- storage-wise this is hundreds of bytes more compact 570// than the equivalent code. Companion function follows. 571#define DELAY 0x80 572static const uint8_t 573Bcmd []= {// Initialization commands for 7735B screens 57418 ,// 18 commands in list: 575ST7735_SWRESET ,DELAY ,// 1: Software reset, no args, w/delay 57650 ,// 50 ms delay 577ST7735_SLPOUT ,DELAY ,// 2: Out of sleep mode, no args, w/delay 578255 ,// 255 = 500 ms delay 579ST7735_COLMOD ,1 + DELAY ,// 3: Set color mode, 1 arg + delay: 5800x05 ,// 16-bit color 58110 ,// 10 ms delay 582ST7735_FRMCTR1 ,3 + DELAY ,// 4: Frame rate control, 3 args + delay: 5830x00 ,// fastest refresh 5840x06 ,// 6 lines front porch 5850x03 ,// 3 lines back porch 58610 ,// 10 ms delay 587ST7735_MADCTL ,1 ,// 5: Memory access ctrl (directions), 1 arg: 5880x08 ,// Row addr/col addr, bottom to top refresh 589ST7735_DISSET5 ,2 ,// 6: Display settings #5, 2 args, no delay: 5900x15 ,// 1 clk cycle nonoverlap, 2 cycle gate 591// rise, 3 cycle osc equalize 5920x02 ,// Fix on VTL 593ST7735_INVCTR ,1 ,// 7: Display inversion control, 1 arg: 5940x0 ,// Line inversion 595ST7735_PWCTR1 ,2 + DELAY ,// 8: Power control, 2 args + delay: 5960x02 ,// GVDD = 4.7V 5970x70 ,// 1.0uA 59810 ,// 10 ms delay 599ST7735_PWCTR2 ,1 ,// 9: Power control, 1 arg, no delay: 6000x05 ,// VGH = 14.7V, VGL = -7.35V 601ST7735_PWCTR3 ,2 ,// 10: Power control, 2 args, no delay: 6020x01 ,// Opamp current small 6030x02 ,// Boost frequency 604ST7735_VMCTR1 ,2 + DELAY ,// 11: Power control, 2 args + delay: 6050x3C ,// VCOMH = 4V 6060x38 ,// VCOML = -1.1V 60710 ,// 10 ms delay 608ST7735_PWCTR6 ,2 ,// 12: Power control, 2 args, no delay: 6090x11 ,0x15 , 610ST7735_GMCTRP1 ,16 ,// 13: Magical unicorn dust, 16 args, no delay: 6110x09 ,0x16 ,0x09 ,0x20 ,// (seriously though, not sure what 6120x21 ,0x1B ,0x13 ,0x19 ,// these config values represent) 6130x17 ,0x15 ,0x1E ,0x2B , 6140x04 ,0x05 ,0x02 ,0x0E , 615ST7735_GMCTRN1 ,16 + DELAY ,// 14: Sparkles and rainbows, 16 args + delay: 6160x0B ,0x14 ,0x08 ,0x1E ,// (ditto) 6170x22 ,0x1D ,0x18 ,0x1E , 6180x1B ,0x1A ,0x24 ,0x2B , 6190x06 ,0x06 ,0x02 ,0x0F , 62010 ,// 10 ms delay 621ST7735_CASET ,4 ,// 15: Column addr set, 4 args, no delay: 6220x00 ,0x02 ,// XSTART = 2 6230x00 ,0x81 ,// XEND = 129 624ST7735_RASET ,4 ,// 16: Row addr set, 4 args, no delay: 6250x00 ,0x02 ,// XSTART = 1 6260x00 ,0x81 ,// XEND = 160 627ST7735_NORON ,DELAY ,// 17: Normal display on, no args, w/delay 62810 ,// 10 ms delay 629ST7735_DISPON ,DELAY ,// 18: Main screen turn on, no args, w/delay 630255 };// 255 = 500 ms delay 631static const uint8_t 632Rcmd1 []= {// Init for 7735R, part 1 (red or green tab) 63315 ,// 15 commands in list: 634ST7735_SWRESET ,DELAY ,// 1: Software reset, 0 args, w/delay 635150 ,// 150 ms delay 636ST7735_SLPOUT ,DELAY ,// 2: Out of sleep mode, 0 args, w/delay 637255 ,// 500 ms delay 638ST7735_FRMCTR1 ,3 ,// 3: Frame rate ctrl - normal mode, 3 args: 6390x01 ,0x2C ,0x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 640ST7735_FRMCTR2 ,3 ,// 4: Frame rate control - idle mode, 3 args: 6410x01 ,0x2C ,0x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 642ST7735_FRMCTR3 ,6 ,// 5: Frame rate ctrl - partial mode, 6 args: 6430x01 ,0x2C ,0x2D ,// Dot inversion mode 6440x01 ,0x2C ,0x2D ,// Line inversion mode 645ST7735_INVCTR ,1 ,// 6: Display inversion ctrl, 1 arg, no delay: 6460x07 ,// No inversion 647ST7735_PWCTR1 ,3 ,// 7: Power control, 3 args, no delay: 6480xA2 , 6490x02 ,// -4.6V 6500x84 ,// AUTO mode 651ST7735_PWCTR2 ,1 ,// 8: Power control, 1 arg, no delay: 6520xC5 ,// VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD 653ST7735_PWCTR3 ,2 ,// 9: Power control, 2 args, no delay: 6540x0A ,// Opamp current small 6550x00 ,// Boost frequency 656ST7735_PWCTR4 ,2 ,// 10: Power control, 2 args, no delay: 6570x8A ,// BCLK/2, Opamp current small & Medium low 6580x2A , 659ST7735_PWCTR5 ,2 ,// 11: Power control, 2 args, no delay: 6600x8A ,0xEE , 661ST7735_VMCTR1 ,1 ,// 12: Power control, 1 arg, no delay: 6620x0E , 663ST7735_INVOFF ,0 ,// 13: Don't invert display, no args, no delay 664ST7735_MADCTL ,1 ,// 14: Memory access control (directions), 1 arg: 6650xC8 ,// row addr/col addr, bottom to top refresh 666ST7735_COLMOD ,1 ,// 15: set color mode, 1 arg, no delay: 6670x05 };// 16-bit color 668static const uint8_t 669Rcmd2green []= {// Init for 7735R, part 2 (green tab only) 6702 ,// 2 commands in list: 671ST7735_CASET ,4 ,// 1: Column addr set, 4 args, no delay: 6720x00 ,0x02 ,// XSTART = 0 6730x00 ,0x7F + 0x02 ,// XEND = 127 674ST7735_RASET ,4 ,// 2: Row addr set, 4 args, no delay: 6750x00 ,0x01 ,// XSTART = 0 6760x00 ,0x9F + 0x01 };// XEND = 159 677static const uint8_t 678Rcmd2red []= {// Init for 7735R, part 2 (red tab only) 6792 ,// 2 commands in list: 680ST7735_CASET ,4 ,// 1: Column addr set, 4 args, no delay: 6810x00 ,0x00 ,// XSTART = 0 6820x00 ,0x7F ,// XEND = 127 683ST7735_RASET ,4 ,// 2: Row addr set, 4 args, no delay: 6840x00 ,0x00 ,// XSTART = 0 6850x00 ,0x9F };// XEND = 159 686static const uint8_t 687Rcmd3 []= {// Init for 7735R, part 3 (red or green tab) 6884 ,// 4 commands in list: 689ST7735_GMCTRP1 ,16 ,// 1: Magical unicorn dust, 16 args, no delay: 6900x02 ,0x1c ,0x07 ,0x12 , 6910x37 ,0x32 ,0x29 ,0x2d , 6920x29 ,0x25 ,0x2B ,0x39 , 6930x00 ,0x01 ,0x03 ,0x10 , 694ST7735_GMCTRN1 ,16 ,// 2: Sparkles and rainbows, 16 args, no delay: 6950x03 ,0x1d ,0x07 ,0x06 , 6960x2E ,0x2C ,0x29 ,0x2D , 6970x2E ,0x2E ,0x37 ,0x3F , 6980x00 ,0x00 ,0x02 ,0x10 , 699ST7735_NORON ,DELAY ,// 3: Normal display on, no args, w/delay 70010 ,// 10 ms delay 701ST7735_DISPON ,DELAY ,// 4: Main screen turn on, no args w/delay 702100 };// 100 ms delay 703 704 705// Companion code to the above tables. Reads and issues 706// a series of LCD commands stored in ROM byte array. 707void static commandList (const uint8_t * addr ) { 708 709uint8_t numCommands ,numArgs ; 710uint16_t ms ; 711 712numCommands = * (addr ++ );// Number of commands to follow 713while (numCommands -- ) {// For each command... 714writecommand (* (addr ++ ));// Read, issue command 715numArgs = * (addr ++ );// Number of args to follow 716ms = numArgs & DELAY ;// If hibit set, delay follows args 717numArgs &= ~DELAY ;// Mask out delay bit 718while (numArgs -- ) {// For each argument... 719writedata (* (addr ++ ));// Read, issue argument 720 } 721 722if (ms ) { 723ms = * (addr ++ );// Read post-command delay time (ms) 724if (ms == 255 )ms = 500 ;// If 255, delay for 500 ms 725Delay1ms (ms ); 726 } 727 } 728} 729 730 731// Initialization code common to both 'B' and 'R' type displays 732void static commonInit (const uint8_t * cmdList ) { 733volatile uint32_t delay ; 734ColStart = RowStart = 0 ;// May be overridden in init func 735 736SYSCTL_RCGCSSI_R |=0x01 ;// activate SSI0 737SYSCTL_RCGCGPIO_R |=0x01 ;// activate port A 738while ((SYSCTL_PRGPIO_R & 0x01 )== 0 ){};// allow time for clock to start 739 740// toggle RST low to reset; CS low so it'll listen to us 741// SSI0Fss is temporarily used as GPIO 742GPIO_PORTA_DIR_R |=0xC8 ;// make PA3,6,7 out 743GPIO_PORTA_AFSEL_R &= ~0xC8 ;// disable alt funct on PA3,6,7 744GPIO_PORTA_DEN_R |=0xC8 ;// enable digital I/O on PA3,6,7 745// configure PA3,6,7 as GPIO 746GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0x00FF0FFF )+ 0x00000000 ; 747GPIO_PORTA_AMSEL_R &= ~0xC8 ;// disable analog functionality on PA3,6,7 748TFT_CS = TFT_CS_LOW ; 749RESET = RESET_HIGH ; 750Delay1ms (500 ); 751RESET = RESET_LOW ; 752Delay1ms (500 ); 753RESET = RESET_HIGH ; 754Delay1ms (500 ); 755 756// initialize SSI0 757GPIO_PORTA_AFSEL_R |=0x2C ;// enable alt funct on PA2,3,5 758GPIO_PORTA_DEN_R |=0x2C ;// enable digital I/O on PA2,3,5 759// configure PA2,3,5 as SSI 760GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0xFF0F00FF )+ 0x00202200 ; 761GPIO_PORTA_AMSEL_R &= ~0x2C ;// disable analog functionality on PA2,3,5 762SSI0_CR1_R &= ~SSI_CR1_SSE ;// disable SSI 763SSI0_CR1_R &= ~SSI_CR1_MS ;// master mode 764// configure for system clock/PLL baud clock source 765SSI0_CC_R = (SSI0_CC_R & ~SSI_CC_CS_M )+ SSI_CC_CS_SYSPLL ; 766// // clock divider for 3.125 MHz SSIClk (50 MHz PIOSC/16) 767// SSI0_CPSR_R = (SSI0_CPSR_R&~SSI_CPSR_CPSDVSR_M)+16; 768// clock divider for 8 MHz SSIClk (80 MHz PLL/24) 769// SysClk/(CPSDVSR*(1+SCR)) 770// 80/(10*(1+0)) = 8 MHz (slower than 4 MHz) 771SSI0_CPSR_R = (SSI0_CPSR_R & ~SSI_CPSR_CPSDVSR_M )+ 10 ;// must be even number 772SSI0_CR0_R &= ~(SSI_CR0_SCR_M |// SCR = 0 (8 Mbps data rate) 773SSI_CR0_SPH |// SPH = 0 774SSI_CR0_SPO );// SPO = 0 775// FRF = Freescale format 776SSI0_CR0_R = (SSI0_CR0_R & ~SSI_CR0_FRF_M )+ SSI_CR0_FRF_MOTO ; 777// DSS = 8-bit data 778SSI0_CR0_R = (SSI0_CR0_R & ~SSI_CR0_DSS_M )+ SSI_CR0_DSS_8 ; 779SSI0_CR1_R |=SSI_CR1_SSE ;// enable SSI 780 781if (cmdList )commandList (cmdList ); 782} 783 784 785//------------ST7735_InitB------------ 786// Initialization for ST7735B screens. 787// Input: none 788// Output: none 789void ST7735_InitB (void ) { 790commonInit (Bcmd ); 791ST7735_SetCursor (0 ,0 ); 792StTextColor = ST7735_YELLOW ; 793ST7735_FillScreen (0 );// set screen to black 794} 795 796 797//------------ST7735_InitR------------ 798// Initialization for ST7735R screens (green or red tabs). 799// Input: option one of the enumerated options depending on tabs 800// Output: none 801void ST7735_InitR (enum initRFlags option ) { 802commonInit (Rcmd1 ); 803if (option == INITR_GREENTAB ) { 804commandList (Rcmd2green ); 805ColStart = 2 ; 806RowStart = 1 ; 807 }else { 808// colstart, rowstart left at default '0' values 809commandList (Rcmd2red ); 810 } 811commandList (Rcmd3 ); 812 813// if black, change MADCTL color filter 814if (option == INITR_BLACKTAB ) { 815writecommand (ST7735_MADCTL ); 816writedata (0xC0 ); 817 } 818TabColor = option ; 819ST7735_SetCursor (0 ,0 ); 820StTextColor = ST7735_YELLOW ; 821ST7735_FillScreen (0 );// set screen to black 822} 823 824 825// Set the region of the screen RAM to be modified 826// Pixel colors are sent left to right, top to bottom 827// (same as Font table is encoded; different from regular bitmap) 828// Requires 11 bytes of transmission 829void static setAddrWindow (uint8_t x0 ,uint8_t y0 ,uint8_t x1 ,uint8_t y1 ) { 830 831writecommand (ST7735_CASET );// Column addr set 832writedata (0x00 ); 833writedata (x0 + ColStart );// XSTART 834writedata (0x00 ); 835writedata (x1 + ColStart );// XEND 836 837writecommand (ST7735_RASET );// Row addr set 838writedata (0x00 ); 839writedata (y0 + RowStart );// YSTART 840writedata (0x00 ); 841writedata (y1 + RowStart );// YEND 842 843writecommand (ST7735_RAMWR );// write to RAM 844} 845 846 847// Send two bytes of data, most significant byte first 848// Requires 2 bytes of transmission 849void static pushColor (uint16_t color ) { 850writedata ((uint8_t )(color >>8 )); 851writedata ((uint8_t )color ); 852} 853 854 855//------------ST7735_DrawPixel------------ 856// Color the pixel at the given coordinates with the given color. 857// Requires 13 bytes of transmission 858// Input: x horizontal position of the pixel, columns from the left edge 859// must be less than 128 860// 0 is on the left, 126 is near the right 861// y vertical position of the pixel, rows from the top edge 862// must be less than 160 863// 159 is near the wires, 0 is the side opposite the wires 864// color 16-bit color, which can be produced by ST7735_Color565() 865// Output: none 866void ST7735_DrawPixel (int16_t x ,int16_t y ,uint16_t color ) { 867 868if ((x < 0 )|| (x >=_width )|| (y < 0 )|| (y >=_height ))return ; 869 870// setAddrWindow(x,y,x+1,y+1); // original code, bug??? 871setAddrWindow (x ,y ,x ,y ); 872 873pushColor (color ); 874 875deselect (); 876} 877 878 879//------------ST7735_DrawFastVLine------------ 880// Draw a vertical line at the given coordinates with the given height and color. 881// A vertical line is parallel to the longer side of the rectangular display 882// Requires (11 + 2*h) bytes of transmission (assuming image fully on screen) 883// Input: x horizontal position of the start of the line, columns from the left edge 884// y vertical position of the start of the line, rows from the top edge 885// h vertical height of the line 886// color 16-bit color, which can be produced by ST7735_Color565() 887// Output: none 888void ST7735_DrawFastVLine (int16_t x ,int16_t y ,int16_t h ,uint16_t color ) { 889uint8_t hi = color >>8 ,lo = color ; 890 891// Rudimentary clipping 892if ((x >=_width )|| (y >=_height ))return ; 893if ((y + h - 1 ) >=_height )h = _height - y ; 894setAddrWindow (x ,y ,x ,y + h - 1 ); 895 896while (h -- ) { 897writedata (hi ); 898writedata (lo ); 899 } 900 901deselect (); 902} 903 904 905//------------ST7735_DrawFastHLine------------ 906// Draw a horizontal line at the given coordinates with the given width and color. 907// A horizontal line is parallel to the shorter side of the rectangular display 908// Requires (11 + 2*w) bytes of transmission (assuming image fully on screen) 909// Input: x horizontal position of the start of the line, columns from the left edge 910// y vertical position of the start of the line, rows from the top edge 911// w horizontal width of the line 912// color 16-bit color, which can be produced by ST7735_Color565() 913// Output: none 914void ST7735_DrawFastHLine (int16_t x ,int16_t y ,int16_t w ,uint16_t color ) { 915uint8_t hi = color >>8 ,lo = color ; 916 917// Rudimentary clipping 918if ((x >=_width )|| (y >=_height ))return ; 919if ((x + w - 1 ) >=_width )w = _width - x ; 920setAddrWindow (x ,y ,x + w - 1 ,y ); 921 922while (w -- ) { 923writedata (hi ); 924writedata (lo ); 925 } 926 927deselect (); 928} 929 930 931//------------ST7735_FillScreen------------ 932// Fill the screen with the given color. 933// Requires 40,971 bytes of transmission 934// Input: color 16-bit color, which can be produced by ST7735_Color565() 935// Output: none 936void ST7735_FillScreen (uint16_t color ) { 937ST7735_FillRect (0 ,0 ,_width ,_height ,color );// original 938// screen is actually 129 by 161 pixels, x 0 to 128, y goes from 0 to 160 939} 940 941 942//------------ST7735_FillRect------------ 943// Draw a filled rectangle at the given coordinates with the given width, height, and color. 944// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 945// Input: x horizontal position of the top left corner of the rectangle, columns from the left edge 946// y vertical position of the top left corner of the rectangle, rows from the top edge 947// w horizontal width of the rectangle 948// h vertical height of the rectangle 949// color 16-bit color, which can be produced by ST7735_Color565() 950// Output: none 951void ST7735_FillRect (int16_t x ,int16_t y ,int16_t w ,int16_t h ,uint16_t color ) { 952uint8_t hi = color >>8 ,lo = color ; 953 954// rudimentary clipping (drawChar w/big text requires this) 955if ((x >=_width )|| (y >=_height ))return ; 956if ((x + w - 1 ) >=_width )w = _width - x ; 957if ((y + h - 1 ) >=_height )h = _height - y ; 958 959setAddrWindow (x ,y ,x + w - 1 ,y + h - 1 ); 960 961for (y = h ;y > 0 ;y -- ) { 962for (x = w ;x > 0 ;x -- ) { 963writedata (hi ); 964writedata (lo ); 965 } 966 } 967 968deselect (); 969} 970 971//------------ST7735_DrawSmallCircle------------ 972// Draw a small circle (diameter of 6 pixels) 973// rectangle at the given coordinates with the given color. 974// Requires (11*6+24*2)=114 bytes of transmission (assuming image on screen) 975// Input: x horizontal position of the top left corner of the circle, columns from the left edge 976// y vertical position of the top left corner of the circle, rows from the top edge 977// color 16-bit color, which can be produced by ST7735_Color565() 978// Output: none 979int16_t const smallCircle [6 ][3 ]= { 980 {2 ,3 ,2 }, 981 {1 ,4 ,4 }, 982 {0 ,5 ,6 }, 983 {0 ,5 ,6 }, 984 {1 ,4 ,4 }, 985 {2 ,3 ,2 }}; 986void ST7735_DrawSmallCircle (int16_t x ,int16_t y ,uint16_t color ) { 987uint32_t i ,w ; 988uint8_t hi = color >>8 ,lo = color ; 989// rudimentary clipping 990if ((x > _width - 5 )|| (y > _height - 5 ))return ;// doesn't fit 991for (i = 0 ;i < 6 ;i ++ ){ 992setAddrWindow (x + smallCircle [i ][0 ],y + i ,x + smallCircle [i ][1 ],y + i ); 993w = smallCircle [i ][2 ]; 994while (w -- ) { 995writedata (hi ); 996writedata (lo ); 997 } 998 } 999deselect (); 1000} 1001//------------ST7735_DrawCircle------------ 1002// Draw a small circle (diameter of 10 pixels) 1003// rectangle at the given coordinates with the given color. 1004// Requires (11*10+68*2)=178 bytes of transmission (assuming image on screen) 1005// Input: x horizontal position of the top left corner of the circle, columns from the left edge 1006// y vertical position of the top left corner of the circle, rows from the top edge 1007// color 16-bit color, which can be produced by ST7735_Color565() 1008// Output: none 1009int16_t const circle [10 ][3 ]= { 1010 {4 ,5 ,2 }, 1011 {2 ,7 ,6 }, 1012 {1 ,8 ,8 }, 1013 {1 ,8 ,8 }, 1014 {0 ,9 ,10 }, 1015 {0 ,9 ,10 }, 1016 {1 ,8 ,8 }, 1017 {1 ,8 ,8 }, 1018 {2 ,7 ,6 }, 1019 {4 ,5 ,2 }}; 1020void ST7735_DrawCircle (int16_t x ,int16_t y ,uint16_t color ) { 1021uint32_t i ,w ; 1022uint8_t hi = color >>8 ,lo = color ; 1023// rudimentary clipping 1024if ((x > _width - 9 )|| (y > _height - 9 ))return ;// doesn't fit 1025for (i = 0 ;i < 10 ;i ++ ){ 1026setAddrWindow (x + circle [i ][0 ],y + i ,x + circle [i ][1 ],y + i ); 1027w = circle [i ][2 ]; 1028while (w -- ) { 1029writedata (hi ); 1030writedata (lo ); 1031 } 1032 } 1033deselect (); 1034} 1035 1036//------------ST7735_Color565------------ 1037// Pass 8-bit (each) R,G,B and get back 16-bit packed color. 1038// Input: r red value 1039// g green value 1040// b blue value 1041// Output: 16-bit color 1042uint16_t ST7735_Color565 (uint8_t r ,uint8_t g ,uint8_t b ) { 1043return ((b & 0xF8 ) <<8 ) | ((g & 0xFC ) <<3 ) | (r >>3 ); 1044} 1045 1046 1047//------------ST7735_SwapColor------------ 1048// Swaps the red and blue values of the given 16-bit packed color; 1049// green is unchanged. 1050// Input: x 16-bit color in format B, G, R 1051// Output: 16-bit color in format R, G, B 1052uint16_t ST7735_SwapColor (uint16_t x ) { 1053return (x <<11 ) | (x & 0x07E0 ) | (x >>11 ); 1054} 1055 1056 1057//------------ST7735_DrawBitmap------------ 1058// Displays a 16-bit color BMP image. A bitmap file that is created 1059// by a PC image processing program has a header and may be padded 1060// with dummy columns so the data have four byte alignment. This 1061// function assumes that all of that has been stripped out, and the 1062// array image[] has one 16-bit halfword for each pixel to be 1063// displayed on the screen (encoded in reverse order, which is 1064// standard for bitmap files). An array can be created in this 1065// format from a 24-bit-per-pixel .bmp file using the associated 1066// converter program. 1067// (x,y) is the screen location of the lower left corner of BMP image 1068// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 1069// Input: x horizontal position of the bottom left corner of the image, columns from the left edge 1070// y vertical position of the bottom left corner of the image, rows from the top edge 1071// image pointer to a 16-bit color BMP image 1072// w number of pixels wide 1073// h number of pixels tall 1074// Output: none 1075// Must be less than or equal to 128 pixels wide by 160 pixels high 1076void ST7735_DrawBitmap (int16_t x ,int16_t y ,const uint16_t * image ,int16_t w ,int16_t h ){ 1077int16_t skipC = 0 ;// non-zero if columns need to be skipped due to clipping 1078int16_t originalWidth = w ;// save this value; even if not all columns fit on the screen, the image is still this width in ROM 1079int i = w * (h - 1 ); 1080 1081if ((x >=_width )|| ((y - h + 1 ) >=_height )|| ((x + w ) <=0 )|| (y < 0 )){ 1082return ;// image is totally off the screen, do nothing 1083 } 1084if ((w > _width )|| (h > _height )){// image is too wide for the screen, do nothing 1085//***This isn't necessarily a fatal error, but it makes the 1086//following logic much more complicated, since you can have 1087//an image that exceeds multiple boundaries and needs to be 1088//clipped on more than one side. 1089return ; 1090 } 1091if ((x + w - 1 ) >=_width ){// image exceeds right of screen 1092skipC = (x + w )- _width ;// skip cut off columns 1093w = _width - x ; 1094 } 1095if ((y - h + 1 )< 0 ){// image exceeds top of screen 1096i = i - (h - y - 1 )* originalWidth ;// skip the last cut off rows 1097h = y + 1 ; 1098 } 1099if (x < 0 ){// image exceeds left of screen 1100w = w + x ; 1101skipC = -1 * x ;// skip cut off columns 1102i = i - x ;// skip the first cut off columns 1103x = 0 ; 1104 } 1105if (y >=_height ){// image exceeds bottom of screen 1106h = h - (y - _height + 1 ); 1107y = _height - 1 ; 1108 } 1109 1110setAddrWindow (x ,y - h + 1 ,x + w - 1 ,y ); 1111 1112for (y = 0 ;y < h ;y = y + 1 ){ 1113for (x = 0 ;x < w ;x = x + 1 ){ 1114// send the top 8 bits 1115writedata ((uint8_t )(image [i ] >>8 )); 1116// send the bottom 8 bits 1117writedata ((uint8_t )image [i ]); 1118i = i + 1 ;// go to the next pixel 1119 } 1120i = i + skipC ; 1121i = i - 2 * originalWidth ; 1122 } 1123 1124deselect (); 1125} 1126 1127 1128//------------ST7735_DrawCharS------------ 1129// Simple character draw function. This is the same function from 1130// Adafruit_GFX.c but adapted for this processor. However, each call 1131// to ST7735_DrawPixel() calls setAddrWindow(), which needs to send 1132// many extra data and commands. If the background color is the same 1133// as the text color, no background will be printed, and text can be 1134// drawn right over existing images without covering them with a box. 1135// Requires (11 + 2*size*size)*6*8 (image fully on screen; textcolor != bgColor) 1136// Input: x horizontal position of the top left corner of the character, columns from the left edge 1137// y vertical position of the top left corner of the character, rows from the top edge 1138// c character to be printed 1139// textColor 16-bit color of the character 1140// bgColor 16-bit color of the background 1141// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 1142// Output: none 1143void ST7735_DrawCharS (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ){ 1144uint8_t line ;// vertical column of pixels of character in font 1145int32_t i ,j ; 1146if ((x >=_width )|| // Clip right 1147 (y >=_height )|| // Clip bottom 1148 ((x + 6 * size - 1 )< 0 )|| // Clip left 1149 ((y + 8 * size - 1 )< 0 ))// Clip top 1150return ; 1151 1152for (i = 0 ;i < 6 ;i ++ ) { 1153if (i == 5 ) 1154line = 0x0 ; 1155else 1156line = Font [(c * 5 )+ i ]; 1157for (j = 0 ;j < 8 ;j ++ ) { 1158if (line & 0x1 ) { 1159if (size == 1 )// default size 1160ST7735_DrawPixel (x + i ,y + j ,textColor ); 1161else {// big size 1162ST7735_FillRect (x + (i * size ),y + (j * size ),size ,size ,textColor ); 1163 } 1164 }else if (bgColor != textColor ) { 1165if (size == 1 )// default size 1166ST7735_DrawPixel (x + i ,y + j ,bgColor ); 1167else {// big size 1168ST7735_FillRect (x + i * size ,y + j * size ,size ,size ,bgColor ); 1169 } 1170 } 1171line >>=1 ; 1172 } 1173 } 1174} 1175 1176 1177//------------ST7735_DrawChar------------ 1178// Advanced character draw function. This is similar to the function 1179// from Adafruit_GFX.c but adapted for this processor. However, this 1180// function only uses one call to setAddrWindow(), which allows it to 1181// run at least twice as fast. 1182// Requires (11 + size*size*6*8) bytes of transmission (assuming image fully on screen) 1183// Input: x horizontal position of the top left corner of the character, columns from the left edge 1184// y vertical position of the top left corner of the character, rows from the top edge 1185// c character to be printed 1186// textColor 16-bit color of the character 1187// bgColor 16-bit color of the background 1188// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 1189// Output: none 1190void ST7735_DrawChar (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ){ 1191uint8_t line ;// horizontal row of pixels of character 1192int32_t col ,row ,i ,j ;// loop indices 1193if (((x + 6 * size - 1 ) >=_width )|| // Clip right 1194 ((y + 8 * size - 1 ) >=_height )|| // Clip bottom 1195 ((x + 6 * size - 1 )< 0 )|| // Clip left 1196 ((y + 8 * size - 1 )< 0 )){// Clip top 1197return ; 1198 } 1199 1200setAddrWindow (x ,y ,x + 6 * size - 1 ,y + 8 * size - 1 ); 1201 1202line = 0x01 ;// print the top row first 1203// print the rows, starting at the top 1204for (row = 0 ;row < 8 ;row = row + 1 ){ 1205for (i = 0 ;i < size ;i = i + 1 ){ 1206// print the columns, starting on the left 1207for (col = 0 ;col < 5 ;col = col + 1 ){ 1208if (Font [(c * 5 )+ col ]& line ){ 1209// bit is set in Font, print pixel(s) in text color 1210for (j = 0 ;j < size ;j = j + 1 ){ 1211pushColor (textColor ); 1212 } 1213 }else { 1214// bit is cleared in Font, print pixel(s) in background color 1215for (j = 0 ;j < size ;j = j + 1 ){ 1216pushColor (bgColor ); 1217 } 1218 } 1219 } 1220// print blank column(s) to the right of character 1221for (j = 0 ;j < size ;j = j + 1 ){ 1222pushColor (bgColor ); 1223 } 1224 } 1225line = line <<1 ;// move up to the next row 1226 } 1227 1228deselect (); 1229} 1230//------------ST7735_DrawString------------ 1231// String draw function. 1232// 16 rows (0 to 15) and 21 characters (0 to 20) 1233// Requires (11 + size*size*6*8) bytes of transmission for each character 1234// Input: x columns from the left edge (0 to 20) 1235// y rows from the top edge (0 to 15) 1236// pt pointer to a null terminated string to be printed 1237// textColor 16-bit color of the characters 1238// bgColor is Black and size is 1 1239// Output: number of characters printed 1240uint32_t ST7735_DrawString (uint16_t x ,uint16_t y ,char * pt ,int16_t textColor ){ 1241uint32_t count = 0 ; 1242if (y > 15 )return 0 ; 1243while (* pt ){ 1244ST7735_DrawCharS (x * 6 ,y * 10 ,* pt ,textColor ,ST7735_BLACK ,1 ); 1245pt ++ ; 1246x = x + 1 ; 1247if (x > 20 )return count ;// number of characters printed 1248count ++ ; 1249 } 1250return count ;// number of characters printed 1251} 1252 1253//-----------------------fillmessage----------------------- 1254// Output a 32-bit number in unsigned decimal format 1255// Input: 32-bit number to be transferred 1256// Output: none 1257// Variable format 1-10 digits with no space before or after 1258char Message [12 ]; 1259uint32_t Messageindex ; 1260 1261void fillmessage (uint32_t n ){ 1262// This function uses recursion to convert decimal number 1263// of unspecified length as an ASCII string 1264if (n >=10 ){ 1265fillmessage (n /10 ); 1266n = n %10 ; 1267 } 1268Message [Messageindex ]= (n + '0' );/* n is between 0 and 9 */ 1269if (Messageindex < 11 )Messageindex ++ ; 1270} 1271//********ST7735_SetCursor***************** 1272// Move the cursor to the desired X- and Y-position. The 1273// next character will be printed here. X=0 is the leftmost 1274// column. Y=0 is the top row. 1275// inputs: newX new X-position of the cursor (0<=newX<=20) 1276// newY new Y-position of the cursor (0<=newY<=15) 1277// outputs: none 1278void ST7735_SetCursor (uint32_t newX ,uint32_t newY ){ 1279if ((newX > 20 )|| (newY > 15 )){// bad input 1280return ;// do nothing 1281 } 1282StX = newX ; 1283StY = newY ; 1284} 1285//-----------------------ST7735_OutUDec----------------------- 1286// Output a 32-bit number in unsigned decimal format 1287// Position determined by ST7735_SetCursor command 1288// Color set by ST7735_SetTextColor 1289// Input: 32-bit number to be transferred 1290// Output: none 1291// Variable format 1-10 digits with no space before or after 1292void ST7735_OutUDec (uint32_t n ){ 1293Messageindex = 0 ; 1294fillmessage (n ); 1295Message [Messageindex ]= 0 ;// terminate 1296ST7735_DrawString (StX ,StY ,Message ,StTextColor ); 1297StX = StX + Messageindex ; 1298if (StX > 20 ){ 1299StX = 20 ; 1300ST7735_DrawCharS (StX * 6 ,StY * 10 ,'*' ,ST7735_RED ,ST7735_BLACK ,1 ); 1301 } 1302} 1303 1304 1305 1306 1307 1308#define MADCTL_MY 0x80 1309#define MADCTL_MX 0x40 1310#define MADCTL_MV 0x20 1311#define MADCTL_ML 0x10 1312#define MADCTL_RGB 0x00 1313#define MADCTL_BGR 0x08 1314#define MADCTL_MH 0x04 1315 1316//------------ST7735_SetRotation------------ 1317// Change the image rotation. 1318// Requires 2 bytes of transmission 1319// Input: m new rotation value (0 to 3) 1320// Output: none 1321void ST7735_SetRotation (uint8_t m ) { 1322 1323writecommand (ST7735_MADCTL ); 1324Rotation = m %4 ;// can't be higher than 3 1325switch (Rotation ) { 1326case 0 : 1327if (TabColor == INITR_BLACKTAB ) { 1328writedata (MADCTL_MX |MADCTL_MY |MADCTL_RGB ); 1329 }else { 1330writedata (MADCTL_MX |MADCTL_MY |MADCTL_BGR ); 1331 } 1332_width = ST7735_TFTWIDTH ; 1333_height = ST7735_TFTHEIGHT ; 1334break ; 1335case 1 : 1336if (TabColor == INITR_BLACKTAB ) { 1337writedata (MADCTL_MY |MADCTL_MV |MADCTL_RGB ); 1338 }else { 1339writedata (MADCTL_MY |MADCTL_MV |MADCTL_BGR ); 1340 } 1341_width = ST7735_TFTHEIGHT ; 1342_height = ST7735_TFTWIDTH ; 1343break ; 1344case 2 : 1345if (TabColor == INITR_BLACKTAB ) { 1346writedata (MADCTL_RGB ); 1347 }else { 1348writedata (MADCTL_BGR ); 1349 } 1350_width = ST7735_TFTWIDTH ; 1351_height = ST7735_TFTHEIGHT ; 1352break ; 1353case 3 : 1354if (TabColor == INITR_BLACKTAB ) { 1355writedata (MADCTL_MX |MADCTL_MV |MADCTL_RGB ); 1356 }else { 1357writedata (MADCTL_MX |MADCTL_MV |MADCTL_BGR ); 1358 } 1359_width = ST7735_TFTHEIGHT ; 1360_height = ST7735_TFTWIDTH ; 1361break ; 1362 } 1363 1364deselect (); 1365} 1366 1367 1368//------------ST7735_InvertDisplay------------ 1369// Send the command to invert all of the colors. 1370// Requires 1 byte of transmission 1371// Input: i 0 to disable inversion; non-zero to enable inversion 1372// Output: none 1373void ST7735_InvertDisplay (int i ) { 1374if (i ){ 1375writecommand (ST7735_INVON ); 1376 }else { 1377writecommand (ST7735_INVOFF ); 1378 } 1379deselect (); 1380} 1381// graphics routines 1382// y coordinates 0 to 31 used for labels and messages 1383// y coordinates 32 to 159 128 pixels high 1384// x coordinates 0 to 127 128 pixels wide 1385 1386int32_t Ymax ,Ymin ,X ;// X goes from 0 to 127 1387int32_t Yrange ;//YrangeDiv2; 1388 1389// *************** ST7735_PlotClear ******************** 1390// Clear the graphics buffer, set X coordinate to 0 1391// This routine clears the display 1392// Inputs: ymin and ymax are range of the plot 1393// Outputs: none 1394void ST7735_PlotClear (int32_t ymin ,int32_t ymax ){ 1395ST7735_FillRect (0 ,32 ,128 ,128 ,ST7735_Color565 (228 ,228 ,228 ));// light grey 1396if (ymax > ymin ){ 1397Ymax = ymax ; 1398Ymin = ymin ; 1399Yrange = ymax - ymin ; 1400 }else { 1401Ymax = ymin ; 1402Ymin = ymax ; 1403Yrange = ymax - ymin ; 1404 } 1405//YrangeDiv2 = Yrange/2; 1406X = 0 ; 1407} 1408 1409// *************** ST7735_PlotPoint ******************** 1410// Used in the voltage versus time plot, plot one point at y 1411// It does output to display 1412// Inputs: y is the y coordinate of the point plotted 1413// Outputs: none 1414void ST7735_PlotPoint (int32_t y ){int32_t j ; 1415if (y < Ymin )y = Ymin ; 1416if (y > Ymax )y = Ymax ; 1417// X goes from 0 to 127 1418// j goes from 159 to 32 1419// y=Ymax maps to j=32 1420// y=Ymin maps to j=159 1421j = 32 + (127 * (Ymax - y ))/Yrange ; 1422if (j < 32 )j = 32 ; 1423if (j > 159 )j = 159 ; 1424ST7735_DrawPixel (X ,j ,ST7735_BLUE ); 1425ST7735_DrawPixel (X + 1 ,j ,ST7735_BLUE ); 1426ST7735_DrawPixel (X ,j + 1 ,ST7735_BLUE ); 1427ST7735_DrawPixel (X + 1 ,j + 1 ,ST7735_BLUE ); 1428} 1429// *************** ST7735_PlotLine ******************** 1430// Used in the voltage versus time plot, plot line to new point 1431// It does output to display 1432// Inputs: y is the y coordinate of the point plotted 1433// Outputs: none 1434int32_t lastj = 0 ; 1435void ST7735_PlotLine (int32_t y ){int32_t i ,j ; 1436if (y < Ymin )y = Ymin ; 1437if (y > Ymax )y = Ymax ; 1438// X goes from 0 to 127 1439// j goes from 159 to 32 1440// y=Ymax maps to j=32 1441// y=Ymin maps to j=159 1442j = 32 + (127 * (Ymax - y ))/Yrange ; 1443if (j < 32 )j = 32 ; 1444if (j > 159 )j = 159 ; 1445if (lastj < 32 )lastj = j ; 1446if (lastj > 159 )lastj = j ; 1447if (lastj < j ){ 1448for (i = lastj + 1 ;i <=j ;i ++ ){ 1449ST7735_DrawPixel (X ,i ,ST7735_BLUE ) ; 1450ST7735_DrawPixel (X + 1 ,i ,ST7735_BLUE ) ; 1451 } 1452 }else if (lastj > j ){ 1453for (i = j ;i < lastj ;i ++ ){ 1454ST7735_DrawPixel (X ,i ,ST7735_BLUE ) ; 1455ST7735_DrawPixel (X + 1 ,i ,ST7735_BLUE ) ; 1456 } 1457 }else { 1458ST7735_DrawPixel (X ,j ,ST7735_BLUE ) ; 1459ST7735_DrawPixel (X + 1 ,j ,ST7735_BLUE ) ; 1460 } 1461lastj = j ; 1462} 1463 1464// *************** ST7735_PlotPoints ******************** 1465// Used in the voltage versus time plot, plot two points at y1, y2 1466// It does output to display 1467// Inputs: y1 is the y coordinate of the first point plotted 1468// y2 is the y coordinate of the second point plotted 1469// Outputs: none 1470void ST7735_PlotPoints (int32_t y1 ,int32_t y2 ){int32_t j ; 1471if (y1 < Ymin )y1 = Ymin ; 1472if (y1 > Ymax )y1 = Ymax ; 1473// X goes from 0 to 127 1474// j goes from 159 to 32 1475// y=Ymax maps to j=32 1476// y=Ymin maps to j=159 1477j = 32 + (127 * (Ymax - y1 ))/Yrange ; 1478if (j < 32 )j = 32 ; 1479if (j > 159 )j = 159 ; 1480ST7735_DrawPixel (X ,j ,ST7735_BLUE ); 1481if (y2 < Ymin )y2 = Ymin ; 1482if (y2 > Ymax )y2 = Ymax ; 1483j = 32 + (127 * (Ymax - y2 ))/Yrange ; 1484if (j < 32 )j = 32 ; 1485if (j > 159 )j = 159 ; 1486ST7735_DrawPixel (X ,j ,ST7735_BLACK ); 1487} 1488// *************** ST7735_PlotBar ******************** 1489// Used in the voltage versus time bar, plot one bar at y 1490// It does not output to display until RIT128x96x4ShowPlot called 1491// Inputs: y is the y coordinate of the bar plotted 1492// Outputs: none 1493void ST7735_PlotBar (int32_t y ){ 1494int32_t j ; 1495if (y < Ymin )y = Ymin ; 1496if (y > Ymax )y = Ymax ; 1497// X goes from 0 to 127 1498// j goes from 159 to 32 1499// y=Ymax maps to j=32 1500// y=Ymin maps to j=159 1501j = 32 + (127 * (Ymax - y ))/Yrange ; 1502ST7735_DrawFastVLine (X ,j ,159 - j ,ST7735_BLACK ); 1503 1504} 1505 1506// full scaled defined as 3V 1507// Input is 0 to 511, 0 => 159 and 511 => 32 1508uint8_t const dBfs [512 ]= { 1509159 ,159 ,145 ,137 ,131 ,126 ,123 ,119 ,117 ,114 ,112 ,110 ,108 ,107 ,105 ,104 ,103 ,101 , 1510100 ,99 ,98 ,97 ,96 ,95 ,94 ,93 ,93 ,92 ,91 ,90 ,90 ,89 ,88 ,88 ,87 ,87 ,86 ,85 ,85 ,84 , 151184 ,83 ,83 ,82 ,82 ,81 ,81 ,81 ,80 ,80 ,79 ,79 ,79 ,78 ,78 ,77 ,77 ,77 ,76 ,76 ,76 ,75 , 151275 ,75 ,74 ,74 ,74 ,73 ,73 ,73 ,72 ,72 ,72 ,72 ,71 ,71 ,71 ,71 ,70 ,70 ,70 ,70 ,69 ,69 , 151369 ,69 ,68 ,68 ,68 ,68 ,67 ,67 ,67 ,67 ,66 ,66 ,66 ,66 ,66 ,65 ,65 ,65 ,65 ,65 ,64 ,64 , 151464 ,64 ,64 ,63 ,63 ,63 ,63 ,63 ,63 ,62 ,62 ,62 ,62 ,62 ,62 ,61 ,61 ,61 ,61 ,61 ,61 ,60 , 151560 ,60 ,60 ,60 ,60 ,59 ,59 ,59 ,59 ,59 ,59 ,59 ,58 ,58 ,58 ,58 ,58 ,58 ,58 ,57 ,57 ,57 , 151657 ,57 ,57 ,57 ,56 ,56 ,56 ,56 ,56 ,56 ,56 ,56 ,55 ,55 ,55 ,55 ,55 ,55 ,55 ,55 ,54 ,54 , 151754 ,54 ,54 ,54 ,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 ,36 ,35 ,35 ,35 ,35 ,35 , 152935 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,34 ,34 ,34 ,34 ,34 ,34 , 153034 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,33 ,33 ,33 ,33 ,33 , 153133 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,32 ,32 ,32 , 153232 ,32 ,32 ,32 ,32 ,32 ,32 ,32 ,32 ,32 1533}; 1534 1535// *************** ST7735_PlotdBfs ******************** 1536// Used in the amplitude versus frequency plot, plot bar point at y 1537// 0 to 0.625V scaled on a log plot from min to max 1538// It does output to display 1539// Inputs: y is the y ADC value of the bar plotted 1540// Outputs: none 1541void ST7735_PlotdBfs (int32_t y ){ 1542int32_t j ; 1543y = y /2 ;// 0 to 2047 1544if (y < 0 )y = 0 ; 1545if (y > 511 )y = 511 ; 1546// X goes from 0 to 127 1547// j goes from 159 to 32 1548// y=511 maps to j=32 1549// y=0 maps to j=159 1550j = dBfs [y ]; 1551ST7735_DrawFastVLine (X ,j ,159 - j ,ST7735_BLACK ); 1552 1553} 1554 1555// *************** ST7735_PlotNext ******************** 1556// Used in all the plots to step the X coordinate one pixel 1557// X steps from 0 to 127, then back to 0 again 1558// It does not output to display 1559// Inputs: none 1560// Outputs: none 1561void ST7735_PlotNext (void ){ 1562if (X == 127 ){ 1563X = 0 ; 1564 }else { 1565X ++ ; 1566 } 1567} 1568 1569// *************** ST7735_PlotNextErase ******************** 1570// Used in all the plots to step the X coordinate one pixel 1571// X steps from 0 to 127, then back to 0 again 1572// It clears the vertical space into which the next pixel will be drawn 1573// Inputs: none 1574// Outputs: none 1575void ST7735_PlotNextErase (void ){ 1576if (X == 127 ){ 1577X = 0 ; 1578 }else { 1579X ++ ; 1580 } 1581ST7735_DrawFastVLine (X ,32 ,128 ,ST7735_Color565 (228 ,228 ,228 )); 1582} 1583 1584// Used in all the plots to write buffer to LCD 1585// Example 1 Voltage versus time 1586// ST7735_PlotClear(0,4095); // range from 0 to 4095 1587// ST7735_PlotPoint(data); ST7735_PlotNext(); // called 128 times 1588 1589// Example 2a Voltage versus time (N data points/pixel, time scale) 1590// ST7735_PlotClear(0,4095); // range from 0 to 4095 1591// { for(j=0;j<N;j++){ 1592// ST7735_PlotPoint(data[i++]); // called N times 1593// } 1594// ST7735_PlotNext(); 1595// } // called 128 times 1596 1597// Example 2b Voltage versus time (N data points/pixel, time scale) 1598// ST7735_PlotClear(0,4095); // range from 0 to 4095 1599// { for(j=0;j<N;j++){ 1600// ST7735_PlotLine(data[i++]); // called N times 1601// } 1602// ST7735_PlotNext(); 1603// } // called 128 times 1604 1605// Example 3 Voltage versus frequency (512 points) 1606// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 1607// ST7735_PlotClear(0,1023); // clip large magnitudes 1608// { 1609// ST7735_PlotBar(mag[i++]); // called 4 times 1610// ST7735_PlotBar(mag[i++]); 1611// ST7735_PlotBar(mag[i++]); 1612// ST7735_PlotBar(mag[i++]); 1613// ST7735_PlotNext(); 1614// } // called 128 times 1615 1616// Example 4 Voltage versus frequency (512 points), dB scale 1617// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 1618// ST7735_PlotClear(0,511); // parameters ignored 1619// { 1620// ST7735_PlotdBfs(mag[i++]); // called 4 times 1621// ST7735_PlotdBfs(mag[i++]); 1622// ST7735_PlotdBfs(mag[i++]); 1623// ST7735_PlotdBfs(mag[i++]); 1624// ST7735_PlotNext(); 1625// } // called 128 times 1626 1627// *************** ST7735_OutChar ******************** 1628// Output one character to the LCD 1629// Position determined by ST7735_SetCursor command 1630// Color set by ST7735_SetTextColor 1631// Inputs: 8-bit ASCII character 1632// Outputs: none 1633void ST7735_OutChar (char ch ){ 1634if ((ch == 10 )|| (ch == 13 )|| (ch == 27 )){ 1635StY ++ ;StX = 0 ; 1636if (StY > 15 ){ 1637StY = 0 ; 1638 } 1639ST7735_DrawString (0 ,StY ," " ,StTextColor ); 1640return ; 1641 } 1642ST7735_DrawCharS (StX * 6 ,StY * 10 ,ch ,ST7735_YELLOW ,ST7735_BLACK ,1 ); 1643StX ++ ; 1644if (StX > 20 ){ 1645StX = 20 ; 1646ST7735_DrawCharS (StX * 6 ,StY * 10 ,'*' ,ST7735_RED ,ST7735_BLACK ,1 ); 1647 } 1648return ; 1649} 1650//********ST7735_OutString***************** 1651// Print a string of characters to the ST7735 LCD. 1652// Position determined by ST7735_SetCursor command 1653// Color set by ST7735_SetTextColor 1654// The string will not automatically wrap. 1655// inputs: ptr pointer to NULL-terminated ASCII string 1656// outputs: none 1657void ST7735_OutString (char * ptr ){ 1658while (* ptr ){ 1659ST7735_OutChar (* ptr ); 1660ptr = ptr + 1 ; 1661 } 1662} 1663// ************** ST7735_SetTextColor ************************ 1664// Sets the color in which the characters will be printed 1665// Background color is fixed at black 1666// Input: 16-bit packed color 1667// Output: none 1668// ******************************************************** 1669void ST7735_SetTextColor (uint16_t color ){ 1670StTextColor = color ; 1671} 1672 1673// Print a character to ST7735 LCD. 1674int fputc (int ch ,FILE * f ){ 1675ST7735_OutChar (ch ); 1676return 1 ; 1677} 1678// No input from Nokia, always return data. 1679int fgetc (FILE * f ){ 1680return 0 ; 1681} 1682// Abstraction of general output device 1683// Volume 2 section 3.4.5 1684 1685// *************** Output_Init ******************** 1686// Standard device driver initialization function for printf 1687// Initialize ST7735 LCD 1688// Inputs: none 1689// Outputs: none 1690void Output_Init (void ){ 1691ST7735_InitR (INITR_REDTAB ); 1692ST7735_FillScreen (0 );// set screen to black 1693} 1694 1695// Clear display 1696void Output_Clear (void ){// Clears the display 1697ST7735_FillScreen (0 );// set screen to black 1698} 1699// Turn off display (low power) 1700void Output_Off (void ){// Turns off the display 1701Output_Clear ();// not implemented 1702} 1703// Turn on display 1704void Output_On (void ){// Turns on the display 1705Output_Init ();// reinitialize 1706} 1707// set the color for future output 1708// Background color is fixed at black 1709// Input: 16-bit packed color 1710// Output: none 1711void Output_Color (uint32_t newColor ){// Set color of future output 1712ST7735_SetTextColor (newColor ); 1713} 1714