+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

Joseph RyanAdd other starter filesc12dcba

master
63.2 KiB1714 linesraw
1/***************************************************
2  This is a library for the Adafruit 1.8" SPI display.
3  This library works with the Adafruit 1.8" TFT Breakout w/SD card
4  ----> http://www.adafruit.com/products/358
5  as well as Adafruit raw 1.8" TFT display
6  ----> http://www.adafruit.com/products/618
7
8  Check out the links above for our tutorials and wiring diagrams
9  These displays use SPI to communicate, 4 or 5 pins are required to
10  interface (RST is optional)
11  Adafruit invests time and resources providing this open source code,
12  please support Adafruit and open-source hardware by purchasing
13  products from Adafruit!
14
15  Written by Limor Fried/Ladyada for Adafruit Industries.
16  MIT 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",
32   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2016
33
34 Copyright 2016 by Jonathan W. Valvano, valvano@mail.utexas.edu
35    You may use, edit, run or distribute this file
36    as long as the above copyright notice remains
37 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
38 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
39 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
40 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
41 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
42 For more information about my classes, my research, and my books, see
43 http://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[] = {
247  0x00, 0x00, 0x00, 0x00, 0x00,
248  0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
249  0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
250  0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
251  0x18, 0x3C, 0x7E, 0x3C, 0x18,
252  0x1C, 0x57, 0x7D, 0x57, 0x1C,
253  0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
254  0x00, 0x18, 0x3C, 0x18, 0x00,
255  0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
256  0x00, 0x18, 0x24, 0x18, 0x00,
257  0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
258  0x30, 0x48, 0x3A, 0x06, 0x0E,
259  0x26, 0x29, 0x79, 0x29, 0x26,
260  0x40, 0x7F, 0x05, 0x05, 0x07,
261  0x40, 0x7F, 0x05, 0x25, 0x3F,
262  0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
263  0x7F, 0x3E, 0x1C, 0x1C, 0x08,
264  0x08, 0x1C, 0x1C, 0x3E, 0x7F,
265  0x14, 0x22, 0x7F, 0x22, 0x14,
266  0x5F, 0x5F, 0x00, 0x5F, 0x5F,
267  0x06, 0x09, 0x7F, 0x01, 0x7F,
268  0x00, 0x66, 0x89, 0x95, 0x6A,
269  0x60, 0x60, 0x60, 0x60, 0x60,
270  0x94, 0xA2, 0xFF, 0xA2, 0x94,
271  0x08, 0x04, 0x7E, 0x04, 0x08,
272  0x10, 0x20, 0x7E, 0x20, 0x10,
273  0x08, 0x08, 0x2A, 0x1C, 0x08,
274  0x08, 0x1C, 0x2A, 0x08, 0x08,
275  0x1E, 0x10, 0x10, 0x10, 0x10,
276  0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
277  0x30, 0x38, 0x3E, 0x38, 0x30,
278  0x06, 0x0E, 0x3E, 0x0E, 0x06,
279  0x00, 0x00, 0x00, 0x00, 0x00,
280  0x00, 0x00, 0x5F, 0x00, 0x00,
281  0x00, 0x07, 0x00, 0x07, 0x00,
282  0x14, 0x7F, 0x14, 0x7F, 0x14,
283  0x24, 0x2A, 0x7F, 0x2A, 0x12,
284  0x23, 0x13, 0x08, 0x64, 0x62,
285  0x36, 0x49, 0x56, 0x20, 0x50,
286  0x00, 0x08, 0x07, 0x03, 0x00,
287  0x00, 0x1C, 0x22, 0x41, 0x00,
288  0x00, 0x41, 0x22, 0x1C, 0x00,
289  0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
290  0x08, 0x08, 0x3E, 0x08, 0x08,
291  0x00, 0x80, 0x70, 0x30, 0x00,
292  0x08, 0x08, 0x08, 0x08, 0x08,
293  0x00, 0x00, 0x60, 0x60, 0x00,
294  0x20, 0x10, 0x08, 0x04, 0x02,
295  0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
296  0x00, 0x42, 0x7F, 0x40, 0x00, // 1
297  0x72, 0x49, 0x49, 0x49, 0x46, // 2
298  0x21, 0x41, 0x49, 0x4D, 0x33, // 3
299  0x18, 0x14, 0x12, 0x7F, 0x10, // 4
300  0x27, 0x45, 0x45, 0x45, 0x39, // 5
301  0x3C, 0x4A, 0x49, 0x49, 0x31, // 6
302  0x41, 0x21, 0x11, 0x09, 0x07, // 7
303  0x36, 0x49, 0x49, 0x49, 0x36, // 8
304  0x46, 0x49, 0x49, 0x29, 0x1E, // 9
305  0x00, 0x00, 0x14, 0x00, 0x00,
306  0x00, 0x40, 0x34, 0x00, 0x00,
307  0x00, 0x08, 0x14, 0x22, 0x41,
308  0x14, 0x14, 0x14, 0x14, 0x14,
309  0x00, 0x41, 0x22, 0x14, 0x08,
310  0x02, 0x01, 0x59, 0x09, 0x06,
311  0x3E, 0x41, 0x5D, 0x59, 0x4E,
312  0x7C, 0x12, 0x11, 0x12, 0x7C, // A
313  0x7F, 0x49, 0x49, 0x49, 0x36, // B
314  0x3E, 0x41, 0x41, 0x41, 0x22, // C
315  0x7F, 0x41, 0x41, 0x41, 0x3E, // D
316  0x7F, 0x49, 0x49, 0x49, 0x41, // E
317  0x7F, 0x09, 0x09, 0x09, 0x01, // F
318  0x3E, 0x41, 0x41, 0x51, 0x73, // G
319  0x7F, 0x08, 0x08, 0x08, 0x7F, // H
320  0x00, 0x41, 0x7F, 0x41, 0x00, // I
321  0x20, 0x40, 0x41, 0x3F, 0x01, // J
322  0x7F, 0x08, 0x14, 0x22, 0x41, // K
323  0x7F, 0x40, 0x40, 0x40, 0x40, // L
324  0x7F, 0x02, 0x1C, 0x02, 0x7F, // M
325  0x7F, 0x04, 0x08, 0x10, 0x7F, // N
326  0x3E, 0x41, 0x41, 0x41, 0x3E, // O
327  0x7F, 0x09, 0x09, 0x09, 0x06, // P
328  0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
329  0x7F, 0x09, 0x19, 0x29, 0x46, // R
330  0x26, 0x49, 0x49, 0x49, 0x32, // S
331  0x03, 0x01, 0x7F, 0x01, 0x03, // T
332  0x3F, 0x40, 0x40, 0x40, 0x3F, // U
333  0x1F, 0x20, 0x40, 0x20, 0x1F, // V
334  0x3F, 0x40, 0x38, 0x40, 0x3F, // W
335  0x63, 0x14, 0x08, 0x14, 0x63, // X
336  0x03, 0x04, 0x78, 0x04, 0x03, // Y
337  0x61, 0x59, 0x49, 0x4D, 0x43, // Z
338  0x00, 0x7F, 0x41, 0x41, 0x41,
339  0x02, 0x04, 0x08, 0x10, 0x20,
340  0x00, 0x41, 0x41, 0x41, 0x7F,
341  0x04, 0x02, 0x01, 0x02, 0x04,
342  0x40, 0x40, 0x40, 0x40, 0x40,
343  0x00, 0x03, 0x07, 0x08, 0x00,
344  0x20, 0x54, 0x54, 0x78, 0x40, // a
345  0x7F, 0x28, 0x44, 0x44, 0x38, // b
346  0x38, 0x44, 0x44, 0x44, 0x28, // c
347  0x38, 0x44, 0x44, 0x28, 0x7F, // d
348  0x38, 0x54, 0x54, 0x54, 0x18, // e
349  0x00, 0x08, 0x7E, 0x09, 0x02, // f
350  0x18, 0xA4, 0xA4, 0x9C, 0x78, // g
351  0x7F, 0x08, 0x04, 0x04, 0x78, // h
352  0x00, 0x44, 0x7D, 0x40, 0x00, // i
353  0x20, 0x40, 0x40, 0x3D, 0x00, // j
354  0x7F, 0x10, 0x28, 0x44, 0x00, // k
355  0x00, 0x41, 0x7F, 0x40, 0x00, // l
356  0x7C, 0x04, 0x78, 0x04, 0x78, // m
357  0x7C, 0x08, 0x04, 0x04, 0x78, // n
358  0x38, 0x44, 0x44, 0x44, 0x38, // o
359  0xFC, 0x18, 0x24, 0x24, 0x18, // p
360  0x18, 0x24, 0x24, 0x18, 0xFC, // q
361  0x7C, 0x08, 0x04, 0x04, 0x08, // r
362  0x48, 0x54, 0x54, 0x54, 0x24, // s
363  0x04, 0x04, 0x3F, 0x44, 0x24, // t
364  0x3C, 0x40, 0x40, 0x20, 0x7C, // u
365  0x1C, 0x20, 0x40, 0x20, 0x1C, // v
366  0x3C, 0x40, 0x30, 0x40, 0x3C, // w
367  0x44, 0x28, 0x10, 0x28, 0x44, // x
368  0x4C, 0x90, 0x90, 0x90, 0x7C, // y
369  0x44, 0x64, 0x54, 0x4C, 0x44, // z
370  0x00, 0x08, 0x36, 0x41, 0x00,
371  0x00, 0x00, 0x77, 0x00, 0x00,
372  0x00, 0x41, 0x36, 0x08, 0x00,
373  0x02, 0x01, 0x02, 0x04, 0x02,
374  0x3C, 0x26, 0x23, 0x26, 0x3C,
375  0x1E, 0xA1, 0xA1, 0x61, 0x12,
376  0x3A, 0x40, 0x40, 0x20, 0x7A,
377  0x38, 0x54, 0x54, 0x55, 0x59,
378  0x21, 0x55, 0x55, 0x79, 0x41,
379  0x21, 0x54, 0x54, 0x78, 0x41,
380  0x21, 0x55, 0x54, 0x78, 0x40,
381  0x20, 0x54, 0x55, 0x79, 0x40,
382  0x0C, 0x1E, 0x52, 0x72, 0x12,
383  0x39, 0x55, 0x55, 0x55, 0x59,
384  0x39, 0x54, 0x54, 0x54, 0x59,
385  0x39, 0x55, 0x54, 0x54, 0x58,
386  0x00, 0x00, 0x45, 0x7C, 0x41,
387  0x00, 0x02, 0x45, 0x7D, 0x42,
388  0x00, 0x01, 0x45, 0x7C, 0x40,
389  0xF0, 0x29, 0x24, 0x29, 0xF0,
390  0xF0, 0x28, 0x25, 0x28, 0xF0,
391  0x7C, 0x54, 0x55, 0x45, 0x00,
392  0x20, 0x54, 0x54, 0x7C, 0x54,
393  0x7C, 0x0A, 0x09, 0x7F, 0x49,
394  0x32, 0x49, 0x49, 0x49, 0x32,
395  0x32, 0x48, 0x48, 0x48, 0x32,
396  0x32, 0x4A, 0x48, 0x48, 0x30,
397  0x3A, 0x41, 0x41, 0x21, 0x7A,
398  0x3A, 0x42, 0x40, 0x20, 0x78,
399  0x00, 0x9D, 0xA0, 0xA0, 0x7D,
400  0x39, 0x44, 0x44, 0x44, 0x39,
401  0x3D, 0x40, 0x40, 0x40, 0x3D,
402  0x3C, 0x24, 0xFF, 0x24, 0x24,
403  0x48, 0x7E, 0x49, 0x43, 0x66,
404  0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
405  0xFF, 0x09, 0x29, 0xF6, 0x20,
406  0xC0, 0x88, 0x7E, 0x09, 0x03,
407  0x20, 0x54, 0x54, 0x79, 0x41,
408  0x00, 0x00, 0x44, 0x7D, 0x41,
409  0x30, 0x48, 0x48, 0x4A, 0x32,
410  0x38, 0x40, 0x40, 0x22, 0x7A,
411  0x00, 0x7A, 0x0A, 0x0A, 0x72,
412  0x7D, 0x0D, 0x19, 0x31, 0x7D,
413  0x26, 0x29, 0x29, 0x2F, 0x28,
414  0x26, 0x29, 0x29, 0x29, 0x26,
415  0x30, 0x48, 0x4D, 0x40, 0x20,
416  0x38, 0x08, 0x08, 0x08, 0x08,
417  0x08, 0x08, 0x08, 0x08, 0x38,
418  0x2F, 0x10, 0xC8, 0xAC, 0xBA,
419  0x2F, 0x10, 0x28, 0x34, 0xFA,
420  0x00, 0x00, 0x7B, 0x00, 0x00,
421  0x08, 0x14, 0x2A, 0x14, 0x22,
422  0x22, 0x14, 0x2A, 0x14, 0x08,
423  0xAA, 0x00, 0x55, 0x00, 0xAA,
424  0xAA, 0x55, 0xAA, 0x55, 0xAA,
425  0x00, 0x00, 0x00, 0xFF, 0x00,
426  0x10, 0x10, 0x10, 0xFF, 0x00,
427  0x14, 0x14, 0x14, 0xFF, 0x00,
428  0x10, 0x10, 0xFF, 0x00, 0xFF,
429  0x10, 0x10, 0xF0, 0x10, 0xF0,
430  0x14, 0x14, 0x14, 0xFC, 0x00,
431  0x14, 0x14, 0xF7, 0x00, 0xFF,
432  0x00, 0x00, 0xFF, 0x00, 0xFF,
433  0x14, 0x14, 0xF4, 0x04, 0xFC,
434  0x14, 0x14, 0x17, 0x10, 0x1F,
435  0x10, 0x10, 0x1F, 0x10, 0x1F,
436  0x14, 0x14, 0x14, 0x1F, 0x00,
437  0x10, 0x10, 0x10, 0xF0, 0x00,
438  0x00, 0x00, 0x00, 0x1F, 0x10,
439  0x10, 0x10, 0x10, 0x1F, 0x10,
440  0x10, 0x10, 0x10, 0xF0, 0x10,
441  0x00, 0x00, 0x00, 0xFF, 0x10,
442  0x10, 0x10, 0x10, 0x10, 0x10,
443  0x10, 0x10, 0x10, 0xFF, 0x10,
444  0x00, 0x00, 0x00, 0xFF, 0x14,
445  0x00, 0x00, 0xFF, 0x00, 0xFF,
446  0x00, 0x00, 0x1F, 0x10, 0x17,
447  0x00, 0x00, 0xFC, 0x04, 0xF4,
448  0x14, 0x14, 0x17, 0x10, 0x17,
449  0x14, 0x14, 0xF4, 0x04, 0xF4,
450  0x00, 0x00, 0xFF, 0x00, 0xF7,
451  0x14, 0x14, 0x14, 0x14, 0x14,
452  0x14, 0x14, 0xF7, 0x00, 0xF7,
453  0x14, 0x14, 0x14, 0x17, 0x14,
454  0x10, 0x10, 0x1F, 0x10, 0x1F,
455  0x14, 0x14, 0x14, 0xF4, 0x14,
456  0x10, 0x10, 0xF0, 0x10, 0xF0,
457  0x00, 0x00, 0x1F, 0x10, 0x1F,
458  0x00, 0x00, 0x00, 0x1F, 0x14,
459  0x00, 0x00, 0x00, 0xFC, 0x14,
460  0x00, 0x00, 0xF0, 0x10, 0xF0,
461  0x10, 0x10, 0xFF, 0x10, 0xFF,
462  0x14, 0x14, 0x14, 0xFF, 0x14,
463  0x10, 0x10, 0x10, 0x1F, 0x00,
464  0x00, 0x00, 0x00, 0xF0, 0x10,
465  0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
466  0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
467  0xFF, 0xFF, 0xFF, 0x00, 0x00,
468  0x00, 0x00, 0x00, 0xFF, 0xFF,
469  0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
470  0x38, 0x44, 0x44, 0x38, 0x44,
471  0x7C, 0x2A, 0x2A, 0x3E, 0x14,
472  0x7E, 0x02, 0x02, 0x06, 0x06,
473  0x02, 0x7E, 0x02, 0x7E, 0x02,
474  0x63, 0x55, 0x49, 0x41, 0x63,
475  0x38, 0x44, 0x44, 0x3C, 0x04,
476  0x40, 0x7E, 0x20, 0x1E, 0x20,
477  0x06, 0x02, 0x7E, 0x02, 0x02,
478  0x99, 0xA5, 0xE7, 0xA5, 0x99,
479  0x1C, 0x2A, 0x49, 0x2A, 0x1C,
480  0x4C, 0x72, 0x01, 0x72, 0x4C,
481  0x30, 0x4A, 0x4D, 0x4D, 0x30,
482  0x30, 0x48, 0x78, 0x48, 0x30,
483  0xBC, 0x62, 0x5A, 0x46, 0x3D,
484  0x3E, 0x49, 0x49, 0x49, 0x00,
485  0x7E, 0x01, 0x01, 0x01, 0x7E,
486  0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
487  0x44, 0x44, 0x5F, 0x44, 0x44,
488  0x40, 0x51, 0x4A, 0x44, 0x40,
489  0x40, 0x44, 0x4A, 0x51, 0x40,
490  0x00, 0x00, 0xFF, 0x01, 0x03,
491  0xE0, 0x80, 0xFF, 0x00, 0x00,
492  0x08, 0x08, 0x6B, 0x6B, 0x08,
493  0x36, 0x12, 0x36, 0x24, 0x36,
494  0x06, 0x0F, 0x09, 0x0F, 0x06,
495  0x00, 0x00, 0x18, 0x18, 0x00,
496  0x00, 0x00, 0x10, 0x10, 0x00,
497  0x30, 0x40, 0xFF, 0x01, 0x01,
498  0x00, 0x1F, 0x01, 0x01, 0x1E,
499  0x00, 0x19, 0x1D, 0x17, 0x12,
500  0x00, 0x3C, 0x3C, 0x3C, 0x3C,
501  0x00, 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
531  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
532  TFT_CS = TFT_CS_LOW;
533  DC = DC_COMMAND;
534  SSI0_DR_R = c;                        // data out
535                                        // wait until SSI0 not busy/transmit FIFO empty
536  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
537}
538
539
540void static writedata(uint8_t c) {
541  while((SSI0_SR_R&SSI_SR_TNF)==0){};   // wait until transmit FIFO not full
542  DC = DC_DATA;
543  SSI0_DR_R = c;                        // data out
544}
545
546void static deselect(void) {
547                                        // wait until SSI0 not busy/transmit FIFO empty
548  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
549  TFT_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;
557  while(n){
558    time = 72724*2/91;  // 1msec, tuned at 80 MHz
559    while(time){
560      time--;
561    }
562    n--;
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
573  Bcmd[] = {                  // Initialization commands for 7735B screens
574    18,                       // 18 commands in list:
575    ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
576      50,                     //     50 ms delay
577    ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
578      255,                    //     255 = 500 ms delay
579    ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
580      0x05,                   //     16-bit color
581      10,                     //     10 ms delay
582    ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
583      0x00,                   //     fastest refresh
584      0x06,                   //     6 lines front porch
585      0x03,                   //     3 lines back porch
586      10,                     //     10 ms delay
587    ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
588      0x08,                   //     Row addr/col addr, bottom to top refresh
589    ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
590      0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
591                              //     rise, 3 cycle osc equalize
592      0x02,                   //     Fix on VTL
593    ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
594      0x0,                    //     Line inversion
595    ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
596      0x02,                   //     GVDD = 4.7V
597      0x70,                   //     1.0uA
598      10,                     //     10 ms delay
599    ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
600      0x05,                   //     VGH = 14.7V, VGL = -7.35V
601    ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
602      0x01,                   //     Opamp current small
603      0x02,                   //     Boost frequency
604    ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
605      0x3C,                   //     VCOMH = 4V
606      0x38,                   //     VCOML = -1.1V
607      10,                     //     10 ms delay
608    ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
609      0x11, 0x15,
610    ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
611      0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
612      0x21, 0x1B, 0x13, 0x19, //      these config values represent)
613      0x17, 0x15, 0x1E, 0x2B,
614      0x04, 0x05, 0x02, 0x0E,
615    ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
616      0x0B, 0x14, 0x08, 0x1E, //     (ditto)
617      0x22, 0x1D, 0x18, 0x1E,
618      0x1B, 0x1A, 0x24, 0x2B,
619      0x06, 0x06, 0x02, 0x0F,
620      10,                     //     10 ms delay
621    ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
622      0x00, 0x02,             //     XSTART = 2
623      0x00, 0x81,             //     XEND = 129
624    ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
625      0x00, 0x02,             //     XSTART = 1
626      0x00, 0x81,             //     XEND = 160
627    ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
628      10,                     //     10 ms delay
629    ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
630      255 };                  //     255 = 500 ms delay
631static const uint8_t
632  Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
633    15,                       // 15 commands in list:
634    ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
635      150,                    //     150 ms delay
636    ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
637      255,                    //     500 ms delay
638    ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
639      0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
640    ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
641      0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
642    ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
643      0x01, 0x2C, 0x2D,       //     Dot inversion mode
644      0x01, 0x2C, 0x2D,       //     Line inversion mode
645    ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
646      0x07,                   //     No inversion
647    ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
648      0xA2,
649      0x02,                   //     -4.6V
650      0x84,                   //     AUTO mode
651    ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
652      0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
653    ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
654      0x0A,                   //     Opamp current small
655      0x00,                   //     Boost frequency
656    ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
657      0x8A,                   //     BCLK/2, Opamp current small & Medium low
658      0x2A,
659    ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
660      0x8A, 0xEE,
661    ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
662      0x0E,
663    ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
664    ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
665      0xC8,                   //     row addr/col addr, bottom to top refresh
666    ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
667      0x05 };                 //     16-bit color
668static const uint8_t
669  Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
670    2,                        //  2 commands in list:
671    ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
672      0x00, 0x02,             //     XSTART = 0
673      0x00, 0x7F+0x02,        //     XEND = 127
674    ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
675      0x00, 0x01,             //     XSTART = 0
676      0x00, 0x9F+0x01 };      //     XEND = 159
677static const uint8_t
678  Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
679    2,                        //  2 commands in list:
680    ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
681      0x00, 0x00,             //     XSTART = 0
682      0x00, 0x7F,             //     XEND = 127
683    ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
684      0x00, 0x00,             //     XSTART = 0
685      0x00, 0x9F };           //     XEND = 159
686static const uint8_t
687  Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
688    4,                        //  4 commands in list:
689    ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
690      0x02, 0x1c, 0x07, 0x12,
691      0x37, 0x32, 0x29, 0x2d,
692      0x29, 0x25, 0x2B, 0x39,
693      0x00, 0x01, 0x03, 0x10,
694    ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
695      0x03, 0x1d, 0x07, 0x06,
696      0x2E, 0x2C, 0x29, 0x2D,
697      0x2E, 0x2E, 0x37, 0x3F,
698      0x00, 0x00, 0x02, 0x10,
699    ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
700      10,                     //     10 ms delay
701    ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
702      100 };                  //     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
709  uint8_t numCommands, numArgs;
710  uint16_t ms;
711
712  numCommands = *(addr++);               // Number of commands to follow
713  while(numCommands--) {                 // For each command...
714    writecommand(*(addr++));             //   Read, issue command
715    numArgs  = *(addr++);                //   Number of args to follow
716    ms       = numArgs & DELAY;          //   If hibit set, delay follows args
717    numArgs &= ~DELAY;                   //   Mask out delay bit
718    while(numArgs--) {                   //   For each argument...
719      writedata(*(addr++));              //     Read, issue argument
720    }
721
722    if(ms) {
723      ms = *(addr++);             // Read post-command delay time (ms)
724      if(ms == 255) ms = 500;     // If 255, delay for 500 ms
725      Delay1ms(ms);
726    }
727  }
728}
729
730
731// Initialization code common to both 'B' and 'R' type displays
732void static commonInit(const uint8_t *cmdList) {
733  volatile uint32_t delay;
734  ColStart  = RowStart = 0; // May be overridden in init func
735
736  SYSCTL_RCGCSSI_R |= 0x01;  // activate SSI0
737  SYSCTL_RCGCGPIO_R |= 0x01; // activate port A
738  while((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
742  GPIO_PORTA_DIR_R |= 0xC8;             // make PA3,6,7 out
743  GPIO_PORTA_AFSEL_R &= ~0xC8;          // disable alt funct on PA3,6,7
744  GPIO_PORTA_DEN_R |= 0xC8;             // enable digital I/O on PA3,6,7
745                                        // configure PA3,6,7 as GPIO
746  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0x00FF0FFF)+0x00000000;
747  GPIO_PORTA_AMSEL_R &= ~0xC8;          // disable analog functionality on PA3,6,7
748  TFT_CS = TFT_CS_LOW;
749  RESET = RESET_HIGH;
750  Delay1ms(500);
751  RESET = RESET_LOW;
752  Delay1ms(500);
753  RESET = RESET_HIGH;
754  Delay1ms(500);
755
756  // initialize SSI0
757  GPIO_PORTA_AFSEL_R |= 0x2C;           // enable alt funct on PA2,3,5
758  GPIO_PORTA_DEN_R |= 0x2C;             // enable digital I/O on PA2,3,5
759                                        // configure PA2,3,5 as SSI
760  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFF0F00FF)+0x00202200;
761  GPIO_PORTA_AMSEL_R &= ~0x2C;          // disable analog functionality on PA2,3,5
762  SSI0_CR1_R &= ~SSI_CR1_SSE;           // disable SSI
763  SSI0_CR1_R &= ~SSI_CR1_MS;            // master mode
764                                        // configure for system clock/PLL baud clock source
765  SSI0_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)
771  SSI0_CPSR_R = (SSI0_CPSR_R&~SSI_CPSR_CPSDVSR_M)+10; // must be even number
772  SSI0_CR0_R &= ~(SSI_CR0_SCR_M |       // SCR = 0 (8 Mbps data rate)
773                  SSI_CR0_SPH |         // SPH = 0
774                  SSI_CR0_SPO);         // SPO = 0
775                                        // FRF = Freescale format
776  SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_FRF_M)+SSI_CR0_FRF_MOTO;
777                                        // DSS = 8-bit data
778  SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_DSS_M)+SSI_CR0_DSS_8;
779  SSI0_CR1_R |= SSI_CR1_SSE;            // enable SSI
780
781  if(cmdList) commandList(cmdList);
782}
783
784
785//------------ST7735_InitB------------
786// Initialization for ST7735B screens.
787// Input: none
788// Output: none
789void ST7735_InitB(void) {
790  commonInit(Bcmd);
791  ST7735_SetCursor(0,0);
792  StTextColor = ST7735_YELLOW;
793  ST7735_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) {
802  commonInit(Rcmd1);
803  if(option == INITR_GREENTAB) {
804    commandList(Rcmd2green);
805    ColStart = 2;
806    RowStart = 1;
807  } else {
808    // colstart, rowstart left at default '0' values
809    commandList(Rcmd2red);
810  }
811  commandList(Rcmd3);
812
813  // if black, change MADCTL color filter
814  if (option == INITR_BLACKTAB) {
815    writecommand(ST7735_MADCTL);
816    writedata(0xC0);
817  }
818  TabColor = option;
819  ST7735_SetCursor(0,0);
820  StTextColor = ST7735_YELLOW;
821  ST7735_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
831  writecommand(ST7735_CASET); // Column addr set
832  writedata(0x00);
833  writedata(x0+ColStart);     // XSTART
834  writedata(0x00);
835  writedata(x1+ColStart);     // XEND
836
837  writecommand(ST7735_RASET); // Row addr set
838  writedata(0x00);
839  writedata(y0+RowStart);     // YSTART
840  writedata(0x00);
841  writedata(y1+RowStart);     // YEND
842
843  writecommand(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) {
850  writedata((uint8_t)(color >> 8));
851  writedata((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
868  if((x < 0) || (x >= _width) || (y < 0) || (y >= _height)) return;
869
870//  setAddrWindow(x,y,x+1,y+1); // original code, bug???
871  setAddrWindow(x,y,x,y);
872
873  pushColor(color);
874
875  deselect();
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) {
889  uint8_t hi = color >> 8, lo = color;
890
891  // Rudimentary clipping
892  if((x >= _width) || (y >= _height)) return;
893  if((y+h-1) >= _height) h = _height-y;
894  setAddrWindow(x, y, x, y+h-1);
895
896  while (h--) {
897    writedata(hi);
898    writedata(lo);
899  }
900
901  deselect();
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) {
915  uint8_t hi = color >> 8, lo = color;
916
917  // Rudimentary clipping
918  if((x >= _width) || (y >= _height)) return;
919  if((x+w-1) >= _width)  w = _width-x;
920  setAddrWindow(x, y, x+w-1, y);
921
922  while (w--) {
923    writedata(hi);
924    writedata(lo);
925  }
926
927  deselect();
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) {
937  ST7735_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) {
952  uint8_t hi = color >> 8, lo = color;
953
954  // rudimentary clipping (drawChar w/big text requires this)
955  if((x >= _width) || (y >= _height)) return;
956  if((x + w - 1) >= _width)  w = _width  - x;
957  if((y + h - 1) >= _height) h = _height - y;
958
959  setAddrWindow(x, y, x+w-1, y+h-1);
960
961  for(y=h; y>0; y--) {
962    for(x=w; x>0; x--) {
963      writedata(hi);
964      writedata(lo);
965    }
966  }
967
968  deselect();
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) {
987  uint32_t i,w;
988  uint8_t hi = color >> 8, lo = color;
989  // rudimentary clipping 
990  if((x>_width-5)||(y>_height-5)) return; // doesn't fit
991  for(i=0; i<6; i++){
992    setAddrWindow(x+smallCircle[i][0], y+i, x+smallCircle[i][1], y+i);
993    w = smallCircle[i][2];
994    while (w--) {
995      writedata(hi);
996      writedata(lo);
997    }
998  }
999  deselect();
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) {
1021  uint32_t i,w;
1022  uint8_t hi = color >> 8, lo = color;
1023  // rudimentary clipping 
1024  if((x>_width-9)||(y>_height-9)) return; // doesn't fit
1025  for(i=0; i<10; i++){
1026    setAddrWindow(x+circle[i][0], y+i, x+circle[i][1], y+i);
1027    w = circle[i][2];
1028    while (w--) {
1029      writedata(hi);
1030      writedata(lo);
1031    }
1032  }
1033  deselect();
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) {
1043  return ((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) {
1053  return (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){
1077  int16_t skipC = 0;                      // non-zero if columns need to be skipped due to clipping
1078  int16_t originalWidth = w;              // save this value; even if not all columns fit on the screen, the image is still this width in ROM
1079  int i = w*(h - 1);
1080
1081  if((x >= _width) || ((y - h + 1) >= _height) || ((x + w) <= 0) || (y < 0)){
1082    return;                             // image is totally off the screen, do nothing
1083  }
1084  if((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.
1089    return;
1090  }
1091  if((x + w - 1) >= _width){            // image exceeds right of screen
1092    skipC = (x + w) - _width;           // skip cut off columns
1093    w = _width - x;
1094  }
1095  if((y - h + 1) < 0){                  // image exceeds top of screen
1096    i = i - (h - y - 1)*originalWidth;  // skip the last cut off rows
1097    h = y + 1;
1098  }
1099  if(x < 0){                            // image exceeds left of screen
1100    w = w + x;
1101    skipC = -1*x;                       // skip cut off columns
1102    i = i - x;                          // skip the first cut off columns
1103    x = 0;
1104  }
1105  if(y >= _height){                     // image exceeds bottom of screen
1106    h = h - (y - _height + 1);
1107    y = _height - 1;
1108  }
1109
1110  setAddrWindow(x, y-h+1, x+w-1, y);
1111
1112  for(y=0; y<h; y=y+1){
1113    for(x=0; x<w; x=x+1){
1114                                        // send the top 8 bits
1115      writedata((uint8_t)(image[i] >> 8));
1116                                        // send the bottom 8 bits
1117      writedata((uint8_t)image[i]);
1118      i = i + 1;                        // go to the next pixel
1119    }
1120    i = i + skipC;
1121    i = i - 2*originalWidth;
1122  }
1123
1124  deselect();
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){
1144  uint8_t line; // vertical column of pixels of character in font
1145  int32_t i, j;
1146  if((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
1150    return;
1151
1152  for (i=0; i<6; i++ ) {
1153    if (i == 5)
1154      line = 0x0;
1155    else
1156      line = Font[(c*5)+i];
1157    for (j = 0; j<8; j++) {
1158      if (line & 0x1) {
1159        if (size == 1) // default size
1160          ST7735_DrawPixel(x+i, y+j, textColor);
1161        else {  // big size
1162          ST7735_FillRect(x+(i*size), y+(j*size), size, size, textColor);
1163        }
1164      } else if (bgColor != textColor) {
1165        if (size == 1) // default size
1166          ST7735_DrawPixel(x+i, y+j, bgColor);
1167        else {  // big size
1168          ST7735_FillRect(x+i*size, y+j*size, size, size, bgColor);
1169        }
1170      }
1171      line >>= 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){
1191  uint8_t line; // horizontal row of pixels of character
1192  int32_t col, row, i, j;// loop indices
1193  if(((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
1197    return;
1198  }
1199
1200  setAddrWindow(x, y, x+6*size-1, y+8*size-1);
1201
1202  line = 0x01;        // print the top row first
1203  // print the rows, starting at the top
1204  for(row=0; row<8; row=row+1){
1205    for(i=0; i<size; i=i+1){
1206      // print the columns, starting on the left
1207      for(col=0; col<5; col=col+1){
1208        if(Font[(c*5)+col]&line){
1209          // bit is set in Font, print pixel(s) in text color
1210          for(j=0; j<size; j=j+1){
1211            pushColor(textColor);
1212          }
1213        } else{
1214          // bit is cleared in Font, print pixel(s) in background color
1215          for(j=0; j<size; j=j+1){
1216            pushColor(bgColor);
1217          }
1218        }
1219      }
1220      // print blank column(s) to the right of character
1221      for(j=0; j<size; j=j+1){
1222        pushColor(bgColor);
1223      }
1224    }
1225    line = line<<1;   // move up to the next row
1226  }
1227
1228  deselect();
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){
1241  uint32_t count = 0;
1242  if(y>15) return 0;
1243  while(*pt){
1244    ST7735_DrawCharS(x*6, y*10, *pt, textColor, ST7735_BLACK, 1);
1245    pt++;
1246    x = x+1;
1247    if(x>20) return count;  // number of characters printed
1248    count++;
1249  }
1250  return 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
1264  if(n >= 10){
1265    fillmessage(n/10);
1266    n = n%10;
1267  }
1268  Message[Messageindex] = (n+'0'); /* n is between 0 and 9 */
1269  if(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){
1279  if((newX > 20) || (newY > 15)){       // bad input
1280    return;                             // do nothing
1281  }
1282  StX = newX;
1283  StY = 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){
1293  Messageindex = 0;
1294  fillmessage(n);
1295  Message[Messageindex] = 0; // terminate
1296  ST7735_DrawString(StX,StY,Message,StTextColor);
1297  StX = StX+Messageindex;
1298  if(StX>20){
1299    StX = 20;
1300    ST7735_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
1323  writecommand(ST7735_MADCTL);
1324  Rotation = m % 4; // can't be higher than 3
1325  switch (Rotation) {
1326   case 0:
1327     if (TabColor == INITR_BLACKTAB) {
1328       writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
1329     } else {
1330       writedata(MADCTL_MX | MADCTL_MY | MADCTL_BGR);
1331     }
1332     _width  = ST7735_TFTWIDTH;
1333     _height = ST7735_TFTHEIGHT;
1334     break;
1335   case 1:
1336     if (TabColor == INITR_BLACKTAB) {
1337       writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
1338     } else {
1339       writedata(MADCTL_MY | MADCTL_MV | MADCTL_BGR);
1340     }
1341     _width  = ST7735_TFTHEIGHT;
1342     _height = ST7735_TFTWIDTH;
1343     break;
1344  case 2:
1345     if (TabColor == INITR_BLACKTAB) {
1346       writedata(MADCTL_RGB);
1347     } else {
1348       writedata(MADCTL_BGR);
1349     }
1350     _width  = ST7735_TFTWIDTH;
1351     _height = ST7735_TFTHEIGHT;
1352    break;
1353   case 3:
1354     if (TabColor == INITR_BLACKTAB) {
1355       writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
1356     } else {
1357       writedata(MADCTL_MX | MADCTL_MV | MADCTL_BGR);
1358     }
1359     _width  = ST7735_TFTHEIGHT;
1360     _height = ST7735_TFTWIDTH;
1361     break;
1362  }
1363
1364  deselect();
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) {
1374  if(i){
1375    writecommand(ST7735_INVON);
1376  } else{
1377    writecommand(ST7735_INVOFF);
1378  }  
1379  deselect();
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){
1395  ST7735_FillRect(0, 32, 128, 128, ST7735_Color565(228,228,228)); // light grey
1396  if(ymax>ymin){
1397    Ymax = ymax;
1398    Ymin = ymin;
1399    Yrange = ymax-ymin;
1400  } else{
1401    Ymax = ymin;
1402    Ymin = ymax;
1403    Yrange = ymax-ymin;
1404  }
1405  //YrangeDiv2 = Yrange/2;
1406  X = 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;
1415  if(y<Ymin) y=Ymin;
1416  if(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
1421  j = 32+(127*(Ymax-y))/Yrange;
1422  if(j<32) j = 32;
1423  if(j>159) j = 159;
1424  ST7735_DrawPixel(X,   j,   ST7735_BLUE);
1425  ST7735_DrawPixel(X+1, j,   ST7735_BLUE);
1426  ST7735_DrawPixel(X,   j+1, ST7735_BLUE);
1427  ST7735_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;
1436  if(y<Ymin) y=Ymin;
1437  if(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
1442  j = 32+(127*(Ymax-y))/Yrange;
1443  if(j < 32) j = 32;
1444  if(j > 159) j = 159;
1445  if(lastj < 32) lastj = j;
1446  if(lastj > 159) lastj = j;
1447  if(lastj < j){
1448    for(i = lastj+1; i<=j ; i++){
1449      ST7735_DrawPixel(X,   i,   ST7735_BLUE) ;
1450      ST7735_DrawPixel(X+1, i,   ST7735_BLUE) ;
1451    }
1452  }else if(lastj > j){
1453    for(i = j; i<lastj ; i++){
1454      ST7735_DrawPixel(X,   i,   ST7735_BLUE) ;
1455      ST7735_DrawPixel(X+1, i,   ST7735_BLUE) ;
1456    }
1457  }else{
1458    ST7735_DrawPixel(X,   j,   ST7735_BLUE) ;
1459    ST7735_DrawPixel(X+1, j,   ST7735_BLUE) ;
1460  }
1461  lastj = 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;
1471  if(y1<Ymin) y1=Ymin;
1472  if(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
1477  j = 32+(127*(Ymax-y1))/Yrange;
1478  if(j<32) j = 32;
1479  if(j>159) j = 159;
1480  ST7735_DrawPixel(X, j, ST7735_BLUE);
1481  if(y2<Ymin) y2=Ymin;
1482  if(y2>Ymax) y2=Ymax;
1483  j = 32+(127*(Ymax-y2))/Yrange;
1484  if(j<32) j = 32;
1485  if(j>159) j = 159;
1486  ST7735_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;
1495  if(y<Ymin) y=Ymin;
1496  if(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
1501  j = 32+(127*(Ymax-y))/Yrange;
1502  ST7735_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,
1510  100, 99, 98, 97, 96, 95, 94, 93, 93, 92, 91, 90, 90, 89, 88, 88, 87, 87, 86, 85, 85, 84,
1511  84, 83, 83, 82, 82, 81, 81, 81, 80, 80, 79, 79, 79, 78, 78, 77, 77, 77, 76, 76, 76, 75,
1512  75, 75, 74, 74, 74, 73, 73, 73, 72, 72, 72, 72, 71, 71, 71, 71, 70, 70, 70, 70, 69, 69,
1513  69, 69, 68, 68, 68, 68, 67, 67, 67, 67, 66, 66, 66, 66, 66, 65, 65, 65, 65, 65, 64, 64,
1514  64, 64, 64, 63, 63, 63, 63, 63, 63, 62, 62, 62, 62, 62, 62, 61, 61, 61, 61, 61, 61, 60,
1515  60, 60, 60, 60, 60, 59, 59, 59, 59, 59, 59, 59, 58, 58, 58, 58, 58, 58, 58, 57, 57, 57,
1516  57, 57, 57, 57, 56, 56, 56, 56, 56, 56, 56, 56, 55, 55, 55, 55, 55, 55, 55, 55, 54, 54,
1517  54, 54, 54, 54, 54, 54, 53, 53, 53, 53, 53, 53, 53, 53, 53, 52, 52, 52, 52, 52, 52, 52,
1518  52, 52, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 50, 50, 50, 50, 50, 50, 50, 50, 50,
1519  50, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
1520  48, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 46, 46, 46, 46, 46, 46, 46, 46, 46,
1521  46, 46, 46, 46, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 44, 44, 44, 44, 44,
1522  44, 44, 44, 44, 44, 44, 44, 44, 44, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
1523  43, 43, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 41, 41, 41, 41, 41,
1524  41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
1525  40, 40, 40, 40, 40, 40, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
1526  39, 39, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 37,
1527  37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 36, 36, 36, 36,
1528  36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 35, 35, 35, 35, 35,
1529  35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 34, 34, 34, 34, 34, 34,
1530  34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 33, 33, 33, 33, 33,
1531  33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32,
1532  32, 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;
1543  y = y/2; // 0 to 2047
1544  if(y<0) y=0;
1545  if(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
1550  j = dBfs[y];
1551  ST7735_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){
1562  if(X==127){
1563    X = 0;
1564  } else{
1565    X++;
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){
1576  if(X==127){
1577    X = 0;
1578  } else{
1579    X++;
1580  }
1581  ST7735_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){
1634  if((ch == 10) || (ch == 13) || (ch == 27)){
1635    StY++; StX=0;
1636    if(StY>15){
1637      StY = 0;
1638    }
1639    ST7735_DrawString(0,StY,"                     ",StTextColor);
1640    return;
1641  }
1642  ST7735_DrawCharS(StX*6,StY*10,ch,ST7735_YELLOW,ST7735_BLACK, 1);
1643  StX++;
1644  if(StX>20){
1645    StX = 20;
1646    ST7735_DrawCharS(StX*6,StY*10,'*',ST7735_RED,ST7735_BLACK, 1);
1647  }
1648  return;
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){
1658  while(*ptr){
1659    ST7735_OutChar(*ptr);
1660    ptr = 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){
1670  StTextColor = color;
1671}
1672
1673// Print a character to ST7735 LCD.
1674int fputc(int ch, FILE *f){
1675  ST7735_OutChar(ch);
1676  return 1;
1677}
1678// No input from Nokia, always return data.
1679int fgetc (FILE *f){
1680  return 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){
1691  ST7735_InitR(INITR_REDTAB);
1692  ST7735_FillScreen(0);                 // set screen to black
1693}
1694
1695// Clear display
1696void Output_Clear(void){ // Clears the display
1697  ST7735_FillScreen(0);    // set screen to black
1698}
1699// Turn off display (low power)
1700void Output_Off(void){   // Turns off the display
1701  Output_Clear();  // not implemented
1702}
1703// Turn on display
1704void Output_On(void){ // Turns on the display
1705  Output_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
1712  ST7735_SetTextColor(newColor);
1713}
1714