collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Vivek Malneediaudio streaming from sd cardeaf9b07

master
64.0 KiB1762 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.cpp
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 5, 2018
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) 2017
33
34 Copyright 2018 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 <stdio.h>
112#include "../inc/ST7735.h"
113#include "../inc/tm4c123gh6pm.h"
114
115#define SDC_CS_PB0 0
116#define SDC_CS_PD7 1
117
118// 16 rows (0 to 15) and 21 characters (0 to 20)
119// Requires (11 + size*size*6*8) bytes of transmission for each character
120uint32_t StX=0; // position along the horizonal axis 0 to 20
121uint32_t StY=0; // position along the vertical axis 0 to 15
122uint16_t StTextColor = ST7735_YELLOW;
123
124#define ST7735_NOP     0x00
125#define ST7735_SWRESET 0x01
126#define ST7735_RDDID   0x04
127#define ST7735_RDDST   0x09
128
129#define ST7735_SLPIN   0x10
130#define ST7735_SLPOUT  0x11
131#define ST7735_PTLON   0x12
132#define ST7735_NORON   0x13
133
134#define ST7735_INVOFF  0x20
135#define ST7735_INVON   0x21
136#define ST7735_DISPOFF 0x28
137#define ST7735_DISPON  0x29
138#define ST7735_CASET   0x2A
139#define ST7735_RASET   0x2B
140#define ST7735_RAMWR   0x2C
141#define ST7735_RAMRD   0x2E
142
143#define ST7735_PTLAR   0x30
144#define ST7735_COLMOD  0x3A
145#define ST7735_MADCTL  0x36
146
147#define ST7735_FRMCTR1 0xB1
148#define ST7735_FRMCTR2 0xB2
149#define ST7735_FRMCTR3 0xB3
150#define ST7735_INVCTR  0xB4
151#define ST7735_DISSET5 0xB6
152
153#define ST7735_PWCTR1  0xC0
154#define ST7735_PWCTR2  0xC1
155#define ST7735_PWCTR3  0xC2
156#define ST7735_PWCTR4  0xC3
157#define ST7735_PWCTR5  0xC4
158#define ST7735_VMCTR1  0xC5
159
160#define ST7735_RDID1   0xDA
161#define ST7735_RDID2   0xDB
162#define ST7735_RDID3   0xDC
163#define ST7735_RDID4   0xDD
164
165#define ST7735_PWCTR6  0xFC
166
167#define ST7735_GMCTRP1 0xE0
168#define ST7735_GMCTRN1 0xE1
169
170#define TFT_CS                  (*((volatile uint32_t *)0x40004020))
171#define TFT_CS_LOW              0           // CS normally controlled by hardware
172#define TFT_CS_HIGH             0x08
173
174#if SDC_CS_PD7
175// PD7 output used for SDC CS
176#define SDC_CS           (*((volatile uint32_t *)0x40007200))
177#define SDC_CS_LOW       0           // CS controlled by software
178#define SDC_CS_HIGH      0x80
179void SDC_CS_Init(void){
180  SYSCTL_RCGCGPIO_R |= 0x08;            // activate clock for Port D
181  while((SYSCTL_PRGPIO_R&0x08) == 0){}; // allow time for clock to stabilize
182  GPIO_PORTD_LOCK_R = 0x4C4F434B;       // unlock GPIO Port D
183  GPIO_PORTD_CR_R = 0xFF;               // allow changes to PD7-0
184  // only PD7 needs to be unlocked, other bits can't be locked
185  GPIO_PORTD_DIR_R |= 0x80;             // make PD7 out
186  GPIO_PORTD_AFSEL_R &= ~0x80;          // disable alt funct on PD7
187  GPIO_PORTD_DR4R_R |= 0x80;            // 4mA drive on outputs
188  GPIO_PORTD_PUR_R |= 0x80;             // enable weak pullup on PD7
189  GPIO_PORTD_DEN_R |= 0x80;             // enable digital I/O on PD7
190                                        // configure PD7 as GPIO
191  GPIO_PORTD_PCTL_R = (GPIO_PORTD_PCTL_R&0x0FFFFFFF)+0x00000000;
192  GPIO_PORTD_AMSEL_R &= ~0x80;          // disable analog functionality on PD7
193  SDC_CS = SDC_CS_HIGH;
194}
195#endif
196// PB0 output used for SDC CS
197#if SDC_CS_PB0
198#define SDC_CS           (*((volatile uint32_t *)0x40005004))
199#define SDC_CS_LOW       0           // CS controlled by software
200#define SDC_CS_HIGH      0x01
201void SDC_CS_Init(void){
202  SYSCTL_RCGCGPIO_R |= 0x02;            // activate clock for Port B
203  while((SYSCTL_PRGPIO_R&0x02) == 0){}; // allow time for clock to stabilize
204  GPIO_PORTB_DIR_R |= 0x01;             // make PB0 out
205  GPIO_PORTB_AFSEL_R &= ~0x01;          // disable alt funct on PB0
206  GPIO_PORTB_DR4R_R |= 0x01;            // 4mA drive on outputs
207  GPIO_PORTB_PUR_R |= 0x01;             // enable weak pullup on PB0
208  GPIO_PORTB_DEN_R |= 0x01;             // enable digital I/O on PB0
209                                        // configure PB0 as GPIO
210  GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R&0xFFFFFFF0)+0x00000000;
211  GPIO_PORTB_AMSEL_R &= ~0x01;          // disable analog functionality on PB0
212  SDC_CS = SDC_CS_HIGH;
213}
214#endif
215
216#define DC                      (*((volatile uint32_t *)0x40004100))
217#define DC_COMMAND              0
218#define DC_DATA                 0x40
219#define RESET                   (*((volatile uint32_t *)0x40004200))
220#define RESET_LOW               0
221#define RESET_HIGH              0x80
222
223#define SSI_CR0_SCR_M           0x0000FF00  // SSI Serial Clock Rate
224#define SSI_CR0_SPH             0x00000080  // SSI Serial Clock Phase
225#define SSI_CR0_SPO             0x00000040  // SSI Serial Clock Polarity
226#define SSI_CR0_FRF_M           0x00000030  // SSI Frame Format Select
227#define SSI_CR0_FRF_MOTO        0x00000000  // Freescale SPI Frame Format
228#define SSI_CR0_DSS_M           0x0000000F  // SSI Data Size Select
229#define SSI_CR0_DSS_8           0x00000007  // 8-bit data
230#define SSI_CR1_MS              0x00000004  // SSI Master/Slave Select
231#define SSI_CR1_SSE             0x00000002  // SSI Synchronous Serial Port
232                                            // Enable
233#define SSI_SR_BSY              0x00000010  // SSI Busy Bit
234#define SSI_SR_TNF              0x00000002  // SSI Transmit FIFO Not Full
235#define SSI_CPSR_CPSDVSR_M      0x000000FF  // SSI Clock Prescale Divisor
236#define SSI_CC_CS_M             0x0000000F  // SSI Baud Clock Source
237#define SSI_CC_CS_SYSPLL        0x00000000  // Either the system clock (if the
238                                            // PLL bypass is in effect) or the
239                                            // PLL output (default)
240#define SYSCTL_RCGC1_SSI0       0x00000010  // SSI0 Clock Gating Control
241#define SYSCTL_RCGC2_GPIOA      0x00000001  // port A Clock Gating Control
242#define ST7735_TFTWIDTH  128
243#define ST7735_TFTHEIGHT 160
244
245#define ST7735_NOP     0x00
246#define ST7735_SWRESET 0x01
247#define ST7735_RDDID   0x04
248#define ST7735_RDDST   0x09
249
250#define ST7735_SLPIN   0x10
251#define ST7735_SLPOUT  0x11
252#define ST7735_PTLON   0x12
253#define ST7735_NORON   0x13
254
255#define ST7735_INVOFF  0x20
256#define ST7735_INVON   0x21
257#define ST7735_DISPOFF 0x28
258#define ST7735_DISPON  0x29
259#define ST7735_CASET   0x2A
260#define ST7735_RASET   0x2B
261#define ST7735_RAMWR   0x2C
262#define ST7735_RAMRD   0x2E
263
264#define ST7735_PTLAR   0x30
265#define ST7735_COLMOD  0x3A
266#define ST7735_MADCTL  0x36
267
268#define ST7735_FRMCTR1 0xB1
269#define ST7735_FRMCTR2 0xB2
270#define ST7735_FRMCTR3 0xB3
271#define ST7735_INVCTR  0xB4
272#define ST7735_DISSET5 0xB6
273
274#define ST7735_PWCTR1  0xC0
275#define ST7735_PWCTR2  0xC1
276#define ST7735_PWCTR3  0xC2
277#define ST7735_PWCTR4  0xC3
278#define ST7735_PWCTR5  0xC4
279#define ST7735_VMCTR1  0xC5
280
281#define ST7735_RDID1   0xDA
282#define ST7735_RDID2   0xDB
283#define ST7735_RDID3   0xDC
284#define ST7735_RDID4   0xDD
285
286#define ST7735_PWCTR6  0xFC
287
288#define ST7735_GMCTRP1 0xE0
289#define ST7735_GMCTRN1 0xE1
290
291// standard ascii 5x7 font
292// originally from glcdfont.c from Adafruit project
293static const uint8_t Font[] = {
294  0x00, 0x00, 0x00, 0x00, 0x00,
295  0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
296  0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
297  0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
298  0x18, 0x3C, 0x7E, 0x3C, 0x18,
299  0x1C, 0x57, 0x7D, 0x57, 0x1C,
300  0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
301  0x00, 0x18, 0x3C, 0x18, 0x00,
302  0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
303  0x00, 0x18, 0x24, 0x18, 0x00,
304  0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
305  0x30, 0x48, 0x3A, 0x06, 0x0E,
306  0x26, 0x29, 0x79, 0x29, 0x26,
307  0x40, 0x7F, 0x05, 0x05, 0x07,
308  0x40, 0x7F, 0x05, 0x25, 0x3F,
309  0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
310  0x7F, 0x3E, 0x1C, 0x1C, 0x08,
311  0x08, 0x1C, 0x1C, 0x3E, 0x7F,
312  0x14, 0x22, 0x7F, 0x22, 0x14,
313  0x5F, 0x5F, 0x00, 0x5F, 0x5F,
314  0x06, 0x09, 0x7F, 0x01, 0x7F,
315  0x00, 0x66, 0x89, 0x95, 0x6A,
316  0x60, 0x60, 0x60, 0x60, 0x60,
317  0x94, 0xA2, 0xFF, 0xA2, 0x94,
318  0x08, 0x04, 0x7E, 0x04, 0x08,
319  0x10, 0x20, 0x7E, 0x20, 0x10,
320  0x08, 0x08, 0x2A, 0x1C, 0x08,
321  0x08, 0x1C, 0x2A, 0x08, 0x08,
322  0x1E, 0x10, 0x10, 0x10, 0x10,
323  0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
324  0x30, 0x38, 0x3E, 0x38, 0x30,
325  0x06, 0x0E, 0x3E, 0x0E, 0x06,
326  0x00, 0x00, 0x00, 0x00, 0x00,
327  0x00, 0x00, 0x5F, 0x00, 0x00,
328  0x00, 0x07, 0x00, 0x07, 0x00,
329  0x14, 0x7F, 0x14, 0x7F, 0x14,
330  0x24, 0x2A, 0x7F, 0x2A, 0x12,
331  0x23, 0x13, 0x08, 0x64, 0x62,
332  0x36, 0x49, 0x56, 0x20, 0x50,
333  0x00, 0x08, 0x07, 0x03, 0x00,
334  0x00, 0x1C, 0x22, 0x41, 0x00,
335  0x00, 0x41, 0x22, 0x1C, 0x00,
336  0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
337  0x08, 0x08, 0x3E, 0x08, 0x08,
338  0x00, 0x80, 0x70, 0x30, 0x00,
339  0x08, 0x08, 0x08, 0x08, 0x08,
340  0x00, 0x00, 0x60, 0x60, 0x00,
341  0x20, 0x10, 0x08, 0x04, 0x02,
342  0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
343  0x00, 0x42, 0x7F, 0x40, 0x00, // 1
344  0x72, 0x49, 0x49, 0x49, 0x46, // 2
345  0x21, 0x41, 0x49, 0x4D, 0x33, // 3
346  0x18, 0x14, 0x12, 0x7F, 0x10, // 4
347  0x27, 0x45, 0x45, 0x45, 0x39, // 5
348  0x3C, 0x4A, 0x49, 0x49, 0x31, // 6
349  0x41, 0x21, 0x11, 0x09, 0x07, // 7
350  0x36, 0x49, 0x49, 0x49, 0x36, // 8
351  0x46, 0x49, 0x49, 0x29, 0x1E, // 9
352  0x00, 0x00, 0x14, 0x00, 0x00,
353  0x00, 0x40, 0x34, 0x00, 0x00,
354  0x00, 0x08, 0x14, 0x22, 0x41,
355  0x14, 0x14, 0x14, 0x14, 0x14,
356  0x00, 0x41, 0x22, 0x14, 0x08,
357  0x02, 0x01, 0x59, 0x09, 0x06,
358  0x3E, 0x41, 0x5D, 0x59, 0x4E,
359  0x7C, 0x12, 0x11, 0x12, 0x7C, // A
360  0x7F, 0x49, 0x49, 0x49, 0x36, // B
361  0x3E, 0x41, 0x41, 0x41, 0x22, // C
362  0x7F, 0x41, 0x41, 0x41, 0x3E, // D
363  0x7F, 0x49, 0x49, 0x49, 0x41, // E
364  0x7F, 0x09, 0x09, 0x09, 0x01, // F
365  0x3E, 0x41, 0x41, 0x51, 0x73, // G
366  0x7F, 0x08, 0x08, 0x08, 0x7F, // H
367  0x00, 0x41, 0x7F, 0x41, 0x00, // I
368  0x20, 0x40, 0x41, 0x3F, 0x01, // J
369  0x7F, 0x08, 0x14, 0x22, 0x41, // K
370  0x7F, 0x40, 0x40, 0x40, 0x40, // L
371  0x7F, 0x02, 0x1C, 0x02, 0x7F, // M
372  0x7F, 0x04, 0x08, 0x10, 0x7F, // N
373  0x3E, 0x41, 0x41, 0x41, 0x3E, // O
374  0x7F, 0x09, 0x09, 0x09, 0x06, // P
375  0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
376  0x7F, 0x09, 0x19, 0x29, 0x46, // R
377  0x26, 0x49, 0x49, 0x49, 0x32, // S
378  0x03, 0x01, 0x7F, 0x01, 0x03, // T
379  0x3F, 0x40, 0x40, 0x40, 0x3F, // U
380  0x1F, 0x20, 0x40, 0x20, 0x1F, // V
381  0x3F, 0x40, 0x38, 0x40, 0x3F, // W
382  0x63, 0x14, 0x08, 0x14, 0x63, // X
383  0x03, 0x04, 0x78, 0x04, 0x03, // Y
384  0x61, 0x59, 0x49, 0x4D, 0x43, // Z
385  0x00, 0x7F, 0x41, 0x41, 0x41,
386  0x02, 0x04, 0x08, 0x10, 0x20,
387  0x00, 0x41, 0x41, 0x41, 0x7F,
388  0x04, 0x02, 0x01, 0x02, 0x04,
389  0x40, 0x40, 0x40, 0x40, 0x40,
390  0x00, 0x03, 0x07, 0x08, 0x00,
391  0x20, 0x54, 0x54, 0x78, 0x40, // a
392  0x7F, 0x28, 0x44, 0x44, 0x38, // b
393  0x38, 0x44, 0x44, 0x44, 0x28, // c
394  0x38, 0x44, 0x44, 0x28, 0x7F, // d
395  0x38, 0x54, 0x54, 0x54, 0x18, // e
396  0x00, 0x08, 0x7E, 0x09, 0x02, // f
397  0x18, 0xA4, 0xA4, 0x9C, 0x78, // g
398  0x7F, 0x08, 0x04, 0x04, 0x78, // h
399  0x00, 0x44, 0x7D, 0x40, 0x00, // i
400  0x20, 0x40, 0x40, 0x3D, 0x00, // j
401  0x7F, 0x10, 0x28, 0x44, 0x00, // k
402  0x00, 0x41, 0x7F, 0x40, 0x00, // l
403  0x7C, 0x04, 0x78, 0x04, 0x78, // m
404  0x7C, 0x08, 0x04, 0x04, 0x78, // n
405  0x38, 0x44, 0x44, 0x44, 0x38, // o
406  0xFC, 0x18, 0x24, 0x24, 0x18, // p
407  0x18, 0x24, 0x24, 0x18, 0xFC, // q
408  0x7C, 0x08, 0x04, 0x04, 0x08, // r
409  0x48, 0x54, 0x54, 0x54, 0x24, // s
410  0x04, 0x04, 0x3F, 0x44, 0x24, // t
411  0x3C, 0x40, 0x40, 0x20, 0x7C, // u
412  0x1C, 0x20, 0x40, 0x20, 0x1C, // v
413  0x3C, 0x40, 0x30, 0x40, 0x3C, // w
414  0x44, 0x28, 0x10, 0x28, 0x44, // x
415  0x4C, 0x90, 0x90, 0x90, 0x7C, // y
416  0x44, 0x64, 0x54, 0x4C, 0x44, // z
417  0x00, 0x08, 0x36, 0x41, 0x00,
418  0x00, 0x00, 0x77, 0x00, 0x00,
419  0x00, 0x41, 0x36, 0x08, 0x00,
420  0x02, 0x01, 0x02, 0x04, 0x02,
421  0x3C, 0x26, 0x23, 0x26, 0x3C,
422  0x1E, 0xA1, 0xA1, 0x61, 0x12,
423  0x3A, 0x40, 0x40, 0x20, 0x7A,
424  0x38, 0x54, 0x54, 0x55, 0x59,
425  0x21, 0x55, 0x55, 0x79, 0x41,
426  0x21, 0x54, 0x54, 0x78, 0x41,
427  0x21, 0x55, 0x54, 0x78, 0x40,
428  0x20, 0x54, 0x55, 0x79, 0x40,
429  0x0C, 0x1E, 0x52, 0x72, 0x12,
430  0x39, 0x55, 0x55, 0x55, 0x59,
431  0x39, 0x54, 0x54, 0x54, 0x59,
432  0x39, 0x55, 0x54, 0x54, 0x58,
433  0x00, 0x00, 0x45, 0x7C, 0x41,
434  0x00, 0x02, 0x45, 0x7D, 0x42,
435  0x00, 0x01, 0x45, 0x7C, 0x40,
436  0xF0, 0x29, 0x24, 0x29, 0xF0,
437  0xF0, 0x28, 0x25, 0x28, 0xF0,
438  0x7C, 0x54, 0x55, 0x45, 0x00,
439  0x20, 0x54, 0x54, 0x7C, 0x54,
440  0x7C, 0x0A, 0x09, 0x7F, 0x49,
441  0x32, 0x49, 0x49, 0x49, 0x32,
442  0x32, 0x48, 0x48, 0x48, 0x32,
443  0x32, 0x4A, 0x48, 0x48, 0x30,
444  0x3A, 0x41, 0x41, 0x21, 0x7A,
445  0x3A, 0x42, 0x40, 0x20, 0x78,
446  0x00, 0x9D, 0xA0, 0xA0, 0x7D,
447  0x39, 0x44, 0x44, 0x44, 0x39,
448  0x3D, 0x40, 0x40, 0x40, 0x3D,
449  0x3C, 0x24, 0xFF, 0x24, 0x24,
450  0x48, 0x7E, 0x49, 0x43, 0x66,
451  0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
452  0xFF, 0x09, 0x29, 0xF6, 0x20,
453  0xC0, 0x88, 0x7E, 0x09, 0x03,
454  0x20, 0x54, 0x54, 0x79, 0x41,
455  0x00, 0x00, 0x44, 0x7D, 0x41,
456  0x30, 0x48, 0x48, 0x4A, 0x32,
457  0x38, 0x40, 0x40, 0x22, 0x7A,
458  0x00, 0x7A, 0x0A, 0x0A, 0x72,
459  0x7D, 0x0D, 0x19, 0x31, 0x7D,
460  0x26, 0x29, 0x29, 0x2F, 0x28,
461  0x26, 0x29, 0x29, 0x29, 0x26,
462  0x30, 0x48, 0x4D, 0x40, 0x20,
463  0x38, 0x08, 0x08, 0x08, 0x08,
464  0x08, 0x08, 0x08, 0x08, 0x38,
465  0x2F, 0x10, 0xC8, 0xAC, 0xBA,
466  0x2F, 0x10, 0x28, 0x34, 0xFA,
467  0x00, 0x00, 0x7B, 0x00, 0x00,
468  0x08, 0x14, 0x2A, 0x14, 0x22,
469  0x22, 0x14, 0x2A, 0x14, 0x08,
470  0xAA, 0x00, 0x55, 0x00, 0xAA,
471  0xAA, 0x55, 0xAA, 0x55, 0xAA,
472  0x00, 0x00, 0x00, 0xFF, 0x00,
473  0x10, 0x10, 0x10, 0xFF, 0x00,
474  0x14, 0x14, 0x14, 0xFF, 0x00,
475  0x10, 0x10, 0xFF, 0x00, 0xFF,
476  0x10, 0x10, 0xF0, 0x10, 0xF0,
477  0x14, 0x14, 0x14, 0xFC, 0x00,
478  0x14, 0x14, 0xF7, 0x00, 0xFF,
479  0x00, 0x00, 0xFF, 0x00, 0xFF,
480  0x14, 0x14, 0xF4, 0x04, 0xFC,
481  0x14, 0x14, 0x17, 0x10, 0x1F,
482  0x10, 0x10, 0x1F, 0x10, 0x1F,
483  0x14, 0x14, 0x14, 0x1F, 0x00,
484  0x10, 0x10, 0x10, 0xF0, 0x00,
485  0x00, 0x00, 0x00, 0x1F, 0x10,
486  0x10, 0x10, 0x10, 0x1F, 0x10,
487  0x10, 0x10, 0x10, 0xF0, 0x10,
488  0x00, 0x00, 0x00, 0xFF, 0x10,
489  0x10, 0x10, 0x10, 0x10, 0x10,
490  0x10, 0x10, 0x10, 0xFF, 0x10,
491  0x00, 0x00, 0x00, 0xFF, 0x14,
492  0x00, 0x00, 0xFF, 0x00, 0xFF,
493  0x00, 0x00, 0x1F, 0x10, 0x17,
494  0x00, 0x00, 0xFC, 0x04, 0xF4,
495  0x14, 0x14, 0x17, 0x10, 0x17,
496  0x14, 0x14, 0xF4, 0x04, 0xF4,
497  0x00, 0x00, 0xFF, 0x00, 0xF7,
498  0x14, 0x14, 0x14, 0x14, 0x14,
499  0x14, 0x14, 0xF7, 0x00, 0xF7,
500  0x14, 0x14, 0x14, 0x17, 0x14,
501  0x10, 0x10, 0x1F, 0x10, 0x1F,
502  0x14, 0x14, 0x14, 0xF4, 0x14,
503  0x10, 0x10, 0xF0, 0x10, 0xF0,
504  0x00, 0x00, 0x1F, 0x10, 0x1F,
505  0x00, 0x00, 0x00, 0x1F, 0x14,
506  0x00, 0x00, 0x00, 0xFC, 0x14,
507  0x00, 0x00, 0xF0, 0x10, 0xF0,
508  0x10, 0x10, 0xFF, 0x10, 0xFF,
509  0x14, 0x14, 0x14, 0xFF, 0x14,
510  0x10, 0x10, 0x10, 0x1F, 0x00,
511  0x00, 0x00, 0x00, 0xF0, 0x10,
512  0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
513  0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
514  0xFF, 0xFF, 0xFF, 0x00, 0x00,
515  0x00, 0x00, 0x00, 0xFF, 0xFF,
516  0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
517  0x38, 0x44, 0x44, 0x38, 0x44,
518  0x7C, 0x2A, 0x2A, 0x3E, 0x14,
519  0x7E, 0x02, 0x02, 0x06, 0x06,
520  0x02, 0x7E, 0x02, 0x7E, 0x02,
521  0x63, 0x55, 0x49, 0x41, 0x63,
522  0x38, 0x44, 0x44, 0x3C, 0x04,
523  0x40, 0x7E, 0x20, 0x1E, 0x20,
524  0x06, 0x02, 0x7E, 0x02, 0x02,
525  0x99, 0xA5, 0xE7, 0xA5, 0x99,
526  0x1C, 0x2A, 0x49, 0x2A, 0x1C,
527  0x4C, 0x72, 0x01, 0x72, 0x4C,
528  0x30, 0x4A, 0x4D, 0x4D, 0x30,
529  0x30, 0x48, 0x78, 0x48, 0x30,
530  0xBC, 0x62, 0x5A, 0x46, 0x3D,
531  0x3E, 0x49, 0x49, 0x49, 0x00,
532  0x7E, 0x01, 0x01, 0x01, 0x7E,
533  0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
534  0x44, 0x44, 0x5F, 0x44, 0x44,
535  0x40, 0x51, 0x4A, 0x44, 0x40,
536  0x40, 0x44, 0x4A, 0x51, 0x40,
537  0x00, 0x00, 0xFF, 0x01, 0x03,
538  0xE0, 0x80, 0xFF, 0x00, 0x00,
539  0x08, 0x08, 0x6B, 0x6B, 0x08,
540  0x36, 0x12, 0x36, 0x24, 0x36,
541  0x06, 0x0F, 0x09, 0x0F, 0x06,
542  0x00, 0x00, 0x18, 0x18, 0x00,
543  0x00, 0x00, 0x10, 0x10, 0x00,
544  0x30, 0x40, 0xFF, 0x01, 0x01,
545  0x00, 0x1F, 0x01, 0x01, 0x1E,
546  0x00, 0x19, 0x1D, 0x17, 0x12,
547  0x00, 0x3C, 0x3C, 0x3C, 0x3C,
548  0x00, 0x00, 0x00, 0x00, 0x00,
549};
550
551
552static uint8_t ColStart, RowStart; // some displays need this changed
553static uint8_t Rotation;           // 0 to 3
554static enum initRFlags TabColor;
555static int16_t _width = ST7735_TFTWIDTH;   // this could probably be a constant, except it is used in Adafruit_GFX and depends on image rotation
556static int16_t _height = ST7735_TFTHEIGHT;
557
558
559// The Data/Command pin must be valid when the eighth bit is
560// sent.  The SSI module has hardware input and output FIFOs
561// that are 8 locations deep; however, they are not used in
562// this implementation.  Each function first stalls while
563// waiting for any pending SSI0 transfers to complete.  Once
564// the SSI0 module is idle, it then prepares the Chip Select
565// pins for the SD card and LCD and the Data/Command pin.
566// Next it starts transmitting the data or command.  Finally
567// once the hardware is idle again, it sets all chip select
568// pins high as required by the serial protocol.  This is a
569// significant change from previous implementations of this
570// function.  It is less efficient without the FIFOs, but it
571// should ensure that the Chip Select and Data/Command pin
572// statuses all match the byte that is actually being
573// transmitted.
574// NOTE: These functions will crash or stall indefinitely if
575// the SSI0 module is not initialized and enabled.
576void static writecommand(unsigned char c) {
577  volatile uint32_t response;
578                                        // wait until SSI0 not busy/transmit FIFO empty
579  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
580  SDC_CS = SDC_CS_HIGH;
581  TFT_CS = TFT_CS_LOW;
582  DC = DC_COMMAND;
583  SSI0_DR_R = c;                        // data out
584  while((SSI0_SR_R&SSI_SR_RNE)==0){};   // wait until response
585  TFT_CS = TFT_CS_HIGH;
586  response = SSI0_DR_R;                 // acknowledge response
587}
588
589
590void static writedata(unsigned char c) {
591  volatile uint32_t response;
592                                        // wait until SSI0 not busy/transmit FIFO empty
593  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
594  SDC_CS = SDC_CS_HIGH;
595  TFT_CS = TFT_CS_LOW;
596  DC = DC_DATA;
597  SSI0_DR_R = c;                        // data out
598  while((SSI0_SR_R&SSI_SR_RNE)==0){};   // wait until response
599  TFT_CS = TFT_CS_HIGH;
600  response = SSI0_DR_R;                 // acknowledge response
601}
602
603void static deselect(void) {
604                                        // wait until SSI0 not busy/transmit FIFO empty
605  while((SSI0_SR_R&SSI_SR_BSY)==SSI_SR_BSY){};
606  TFT_CS = TFT_CS_HIGH;
607}
608
609// Subroutine to wait 1 msec
610// Inputs: None
611// Outputs: None
612// Notes: ...
613void Delay1ms(uint32_t n){uint32_t volatile time;
614  while(n){
615    time = 72724*2/91;  // 1msec, tuned at 80 MHz
616    while(time){
617      time--;
618    }
619    n--;
620  }
621}
622
623// Rather than a bazillion writecommand() and writedata() calls, screen
624// initialization commands and arguments are organized in these tables
625// stored in ROM.  The table may look bulky, but that's mostly the
626// formatting -- storage-wise this is hundreds of bytes more compact
627// than the equivalent code.  Companion function follows.
628#define DELAY 0x80
629static const uint8_t
630  Bcmd[] = {                  // Initialization commands for 7735B screens
631    18,                       // 18 commands in list:
632    ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
633      50,                     //     50 ms delay
634    ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
635      255,                    //     255 = 500 ms delay
636    ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
637      0x05,                   //     16-bit color
638      10,                     //     10 ms delay
639    ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
640      0x00,                   //     fastest refresh
641      0x06,                   //     6 lines front porch
642      0x03,                   //     3 lines back porch
643      10,                     //     10 ms delay
644    ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
645      0x08,                   //     Row addr/col addr, bottom to top refresh
646    ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
647      0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
648                              //     rise, 3 cycle osc equalize
649      0x02,                   //     Fix on VTL
650    ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
651      0x0,                    //     Line inversion
652    ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
653      0x02,                   //     GVDD = 4.7V
654      0x70,                   //     1.0uA
655      10,                     //     10 ms delay
656    ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
657      0x05,                   //     VGH = 14.7V, VGL = -7.35V
658    ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
659      0x01,                   //     Opamp current small
660      0x02,                   //     Boost frequency
661    ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
662      0x3C,                   //     VCOMH = 4V
663      0x38,                   //     VCOML = -1.1V
664      10,                     //     10 ms delay
665    ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
666      0x11, 0x15,
667    ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
668      0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
669      0x21, 0x1B, 0x13, 0x19, //      these config values represent)
670      0x17, 0x15, 0x1E, 0x2B,
671      0x04, 0x05, 0x02, 0x0E,
672    ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
673      0x0B, 0x14, 0x08, 0x1E, //     (ditto)
674      0x22, 0x1D, 0x18, 0x1E,
675      0x1B, 0x1A, 0x24, 0x2B,
676      0x06, 0x06, 0x02, 0x0F,
677      10,                     //     10 ms delay
678    ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
679      0x00, 0x02,             //     XSTART = 2
680      0x00, 0x81,             //     XEND = 129
681    ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
682      0x00, 0x02,             //     XSTART = 1
683      0x00, 0x81,             //     XEND = 160
684    ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
685      10,                     //     10 ms delay
686    ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
687      255 };                  //     255 = 500 ms delay
688static const uint8_t
689  Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
690    15,                       // 15 commands in list:
691    ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
692      150,                    //     150 ms delay
693    ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
694      255,                    //     500 ms delay
695    ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
696      0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
697    ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
698      0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
699    ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
700      0x01, 0x2C, 0x2D,       //     Dot inversion mode
701      0x01, 0x2C, 0x2D,       //     Line inversion mode
702    ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
703      0x07,                   //     No inversion
704    ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
705      0xA2,
706      0x02,                   //     -4.6V
707      0x84,                   //     AUTO mode
708    ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
709      0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
710    ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
711      0x0A,                   //     Opamp current small
712      0x00,                   //     Boost frequency
713    ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
714      0x8A,                   //     BCLK/2, Opamp current small & Medium low
715      0x2A,
716    ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
717      0x8A, 0xEE,
718    ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
719      0x0E,
720    ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
721    ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
722      0xC8,                   //     row addr/col addr, bottom to top refresh
723    ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
724      0x05 };                 //     16-bit color
725static const uint8_t
726  Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
727    2,                        //  2 commands in list:
728    ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
729      0x00, 0x02,             //     XSTART = 0
730      0x00, 0x7F+0x02,        //     XEND = 127
731    ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
732      0x00, 0x01,             //     XSTART = 0
733      0x00, 0x9F+0x01 };      //     XEND = 159
734static const uint8_t
735  Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
736    2,                        //  2 commands in list:
737    ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
738      0x00, 0x00,             //     XSTART = 0
739      0x00, 0x7F,             //     XEND = 127
740    ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
741      0x00, 0x00,             //     XSTART = 0
742      0x00, 0x9F };           //     XEND = 159
743static const uint8_t
744  Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
745    4,                        //  4 commands in list:
746    ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
747      0x02, 0x1c, 0x07, 0x12,
748      0x37, 0x32, 0x29, 0x2d,
749      0x29, 0x25, 0x2B, 0x39,
750      0x00, 0x01, 0x03, 0x10,
751    ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
752      0x03, 0x1d, 0x07, 0x06,
753      0x2E, 0x2C, 0x29, 0x2D,
754      0x2E, 0x2E, 0x37, 0x3F,
755      0x00, 0x00, 0x02, 0x10,
756    ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
757      10,                     //     10 ms delay
758    ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
759      100 };                  //     100 ms delay
760
761
762// Companion code to the above tables.  Reads and issues
763// a series of LCD commands stored in ROM byte array.
764void static commandList(const uint8_t *addr) {
765
766  uint8_t numCommands, numArgs;
767  uint16_t ms;
768
769  numCommands = *(addr++);               // Number of commands to follow
770  while(numCommands--) {                 // For each command...
771    writecommand(*(addr++));             //   Read, issue command
772    numArgs  = *(addr++);                //   Number of args to follow
773    ms       = numArgs & DELAY;          //   If hibit set, delay follows args
774    numArgs &= ~DELAY;                   //   Mask out delay bit
775    while(numArgs--) {                   //   For each argument...
776      writedata(*(addr++));              //     Read, issue argument
777    }
778
779    if(ms) {
780      ms = *(addr++);             // Read post-command delay time (ms)
781      if(ms == 255) ms = 500;     // If 255, delay for 500 ms
782      Delay1ms(ms);
783    }
784  }
785}
786
787
788// Initialization code common to both 'B' and 'R' type displays
789void static commonInit(const uint8_t *cmdList) {
790  ColStart  = RowStart = 0; // May be overridden in init func
791                                        // activate clock for Port A
792  SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;
793                                        // allow time for clock to stabilize
794  while((SYSCTL_PRGPIO_R&SYSCTL_PRGPIO_R0) == 0){};
795                                        // activate clock for SSI0
796  SYSCTL_RCGCSSI_R |= SYSCTL_RCGCSSI_R0;
797                                        // allow time for clock to stabilize
798  while((SYSCTL_PRSSI_R&SYSCTL_PRSSI_R0) == 0){};
799
800  // toggle RST low to reset; CS low so it'll listen to us
801  // SSI0Fss is used as GPIO
802  GPIO_PORTA_DIR_R |= 0xC8;             // make PA3,6,7 out
803  GPIO_PORTA_AFSEL_R &= ~0xC8;          // disable alt funct on PA3,6,7
804  GPIO_PORTA_DEN_R |= 0xC8;             // enable digital I/O on PA3,6,7
805                                        // configure PA3,6,7 as GPIO
806  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0x00FF0FFF)+0x00000000;
807  GPIO_PORTA_AMSEL_R &= ~0xC8;          // disable analog functionality on PA3,6,7
808  TFT_CS = TFT_CS_LOW;
809  RESET = RESET_HIGH;
810  Delay1ms(500);
811  RESET = RESET_LOW;
812  Delay1ms(500);
813  RESET = RESET_HIGH;
814  Delay1ms(500);
815
816  // initialize SSI0
817  GPIO_PORTA_AFSEL_R |= 0x34;           // enable alt funct on PA2,4,5
818  GPIO_PORTA_PUR_R |= 0x3C;             // enable weak pullup on PA2,3,4,5
819  GPIO_PORTA_DEN_R |= 0x34;             // enable digital I/O on PA2,4,5
820                                        // configure PA2,4,5 as SSI
821  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFF00F0FF)+0x00220200;
822  GPIO_PORTA_AMSEL_R &= ~0x34;          // disable analog functionality on PA2,4,5
823  SDC_CS_Init();                        // SDC CS on PB0 or PD7
824  SDC_CS = SDC_CS_HIGH;
825  SSI0_CR1_R &= ~SSI_CR1_SSE;           // disable SSI
826  SSI0_CR1_R &= ~SSI_CR1_MS;            // master mode
827                                        // configure for clock from source PIOSC for baud clock source
828  SSI0_CC_R = (SSI0_CC_R&~SSI_CC_CS_M)+SSI_CC_CS_PIOSC;
829                                        // clock divider for 8 MHz SSIClk (16 MHz PIOSC/2)
830                                        // PIOSC/(CPSDVSR*(1+SCR))
831                                        // 16/(2*(1+0)) = 8 MHz
832  SSI0_CPSR_R = (SSI0_CPSR_R&~SSI_CPSR_CPSDVSR_M)+2; // must be even number
833  SSI0_CR0_R &= ~(SSI_CR0_SCR_M |       // SCR = 0 (8 Mbps data rate)
834                  SSI_CR0_SPH |         // SPH = 0
835                  SSI_CR0_SPO);         // SPO = 0
836                                        // FRF = Freescale format
837  SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_FRF_M)+SSI_CR0_FRF_MOTO;
838                                        // DSS = 8-bit data
839  SSI0_CR0_R = (SSI0_CR0_R&~SSI_CR0_DSS_M)+SSI_CR0_DSS_8;
840  SSI0_CR1_R |= SSI_CR1_SSE;            // enable SSI
841
842  if(cmdList) commandList(cmdList);
843}
844
845
846//------------ST7735_InitB------------
847// Initialization for ST7735B screens.
848// Input: none
849// Output: none
850void ST7735_InitB(void) {
851  commonInit(Bcmd);
852  ST7735_SetCursor(0,0);
853  StTextColor = ST7735_YELLOW;
854  ST7735_FillScreen(0);                 // set screen to black
855}
856
857
858//------------ST7735_InitR------------
859// Initialization for ST7735R screens (green or red tabs).
860// Input: option one of the enumerated options depending on tabs
861// Output: none
862void ST7735_InitR(enum initRFlags option) {
863  commonInit(Rcmd1);
864  if(option == INITR_GREENTAB) {
865    commandList(Rcmd2green);
866    ColStart = 2;
867    RowStart = 1;
868  } else {
869    // colstart, rowstart left at default '0' values
870    commandList(Rcmd2red);
871  }
872  commandList(Rcmd3);
873
874  // if black, change MADCTL color filter
875  if (option == INITR_BLACKTAB) {
876    writecommand(ST7735_MADCTL);
877    writedata(0xC0);
878  }
879  TabColor = option;
880  ST7735_SetCursor(0,0);
881  StTextColor = ST7735_YELLOW;
882  ST7735_FillScreen(0);                 // set screen to black
883}
884
885
886// Set the region of the screen RAM to be modified
887// Pixel colors are sent left to right, top to bottom
888// (same as Font table is encoded; different from regular bitmap)
889// Requires 11 bytes of transmission
890void static setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
891
892  writecommand(ST7735_CASET); // Column addr set
893  writedata(0x00);
894  writedata(x0+ColStart);     // XSTART
895  writedata(0x00);
896  writedata(x1+ColStart);     // XEND
897
898  writecommand(ST7735_RASET); // Row addr set
899  writedata(0x00);
900  writedata(y0+RowStart);     // YSTART
901  writedata(0x00);
902  writedata(y1+RowStart);     // YEND
903
904  writecommand(ST7735_RAMWR); // write to RAM
905}
906
907
908// Send two bytes of data, most significant byte first
909// Requires 2 bytes of transmission
910void static pushColor(uint16_t color) {
911  writedata((uint8_t)(color >> 8));
912  writedata((uint8_t)color);
913}
914
915
916//------------ST7735_DrawPixel------------
917// Color the pixel at the given coordinates with the given color.
918// Requires 13 bytes of transmission
919// Input: x     horizontal position of the pixel, columns from the left edge
920//               must be less than 128
921//               0 is on the left, 126 is near the right
922//        y     vertical position of the pixel, rows from the top edge
923//               must be less than 160
924//               159 is near the wires, 0 is the side opposite the wires
925//        color 16-bit color, which can be produced by ST7735_Color565()
926// Output: none
927void ST7735_DrawPixel(int16_t x, int16_t y, uint16_t color) {
928
929  if((x < 0) || (x >= _width) || (y < 0) || (y >= _height)) return;
930
931//  setAddrWindow(x,y,x+1,y+1); // original code, bug???
932  setAddrWindow(x,y,x,y);
933
934  pushColor(color);
935
936  deselect();
937}
938
939
940//------------ST7735_DrawFastVLine------------
941// Draw a vertical line at the given coordinates with the given height and color.
942// A vertical line is parallel to the longer side of the rectangular display
943// Requires (11 + 2*h) bytes of transmission (assuming image fully on screen)
944// Input: x     horizontal position of the start of the line, columns from the left edge
945//        y     vertical position of the start of the line, rows from the top edge
946//        h     vertical height of the line
947//        color 16-bit color, which can be produced by ST7735_Color565()
948// Output: none
949void ST7735_DrawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
950  uint8_t hi = color >> 8, lo = color;
951
952  // Rudimentary clipping
953  if((x >= _width) || (y >= _height)) return;
954  if((y+h-1) >= _height) h = _height-y;
955  setAddrWindow(x, y, x, y+h-1);
956
957  while (h--) {
958    writedata(hi);
959    writedata(lo);
960  }
961
962  deselect();
963}
964
965
966//------------ST7735_DrawFastHLine------------
967// Draw a horizontal line at the given coordinates with the given width and color.
968// A horizontal line is parallel to the shorter side of the rectangular display
969// Requires (11 + 2*w) bytes of transmission (assuming image fully on screen)
970// Input: x     horizontal position of the start of the line, columns from the left edge
971//        y     vertical position of the start of the line, rows from the top edge
972//        w     horizontal width of the line
973//        color 16-bit color, which can be produced by ST7735_Color565()
974// Output: none
975void ST7735_DrawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
976  uint8_t hi = color >> 8, lo = color;
977
978  // Rudimentary clipping
979  if((x >= _width) || (y >= _height)) return;
980  if((x+w-1) >= _width)  w = _width-x;
981  setAddrWindow(x, y, x+w-1, y);
982
983  while (w--) {
984    writedata(hi);
985    writedata(lo);
986  }
987
988  deselect();
989}
990
991
992//------------ST7735_FillScreen------------
993// Fill the screen with the given color.
994// Requires 40,971 bytes of transmission
995// Input: color 16-bit color, which can be produced by ST7735_Color565()
996// Output: none
997void ST7735_FillScreen(uint16_t color) {
998  ST7735_FillRect(0, 0, _width, _height, color);  // original
999//  screen is actually 129 by 161 pixels, x 0 to 128, y goes from 0 to 160
1000}
1001
1002
1003//------------ST7735_FillRect------------
1004// Draw a filled rectangle at the given coordinates with the given width, height, and color.
1005// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen)
1006// Input: x     horizontal position of the top left corner of the rectangle, columns from the left edge
1007//        y     vertical position of the top left corner of the rectangle, rows from the top edge
1008//        w     horizontal width of the rectangle
1009//        h     vertical height of the rectangle
1010//        color 16-bit color, which can be produced by ST7735_Color565()
1011// Output: none
1012void ST7735_FillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
1013  uint8_t hi = color >> 8, lo = color;
1014
1015  // rudimentary clipping (drawChar w/big text requires this)
1016  if((x >= _width) || (y >= _height)) return;
1017  if((x + w - 1) >= _width)  w = _width  - x;
1018  if((y + h - 1) >= _height) h = _height - y;
1019
1020  setAddrWindow(x, y, x+w-1, y+h-1);
1021
1022  for(y=h; y>0; y--) {
1023    for(x=w; x>0; x--) {
1024      writedata(hi);
1025      writedata(lo);
1026    }
1027  }
1028
1029  deselect();
1030}
1031
1032//------------ST7735_DrawSmallCircle------------
1033// Draw a small circle (diameter of 6 pixels)
1034// rectangle at the given coordinates with the given color.
1035// Requires (11*6+24*2)=114 bytes of transmission (assuming image on screen)
1036// Input: x     horizontal position of the top left corner of the circle, columns from the left edge
1037//        y     vertical position of the top left corner of the circle, rows from the top edge
1038//        color 16-bit color, which can be produced by ST7735_Color565()
1039// Output: none
1040int16_t const smallCircle[6][3]={
1041    {2,3,    2},
1042  {1  ,  4,  4},
1043 {0   ,   5, 6},
1044 {0   ,   5, 6},
1045  {1  ,  4,  4},
1046    {2,3,    2}};
1047void ST7735_DrawSmallCircle(int16_t x, int16_t y, uint16_t color) {
1048  uint32_t i,w;
1049  uint8_t hi = color >> 8, lo = color;
1050  // rudimentary clipping
1051  if((x>_width-5)||(y>_height-5)) return; // doesn't fit
1052  for(i=0; i<6; i++){
1053    setAddrWindow(x+smallCircle[i][0], y+i, x+smallCircle[i][1], y+i);
1054    w = smallCircle[i][2];
1055    while (w--) {
1056      writedata(hi);
1057      writedata(lo);
1058    }
1059  }
1060  deselect();
1061}
1062//------------ST7735_DrawCircle------------
1063// Draw a small circle (diameter of 10 pixels)
1064// rectangle at the given coordinates with the given color.
1065// Requires (11*10+68*2)=178 bytes of transmission (assuming image on screen)
1066// Input: x     horizontal position of the top left corner of the circle, columns from the left edge
1067//        y     vertical position of the top left corner of the circle, rows from the top edge
1068//        color 16-bit color, which can be produced by ST7735_Color565()
1069// Output: none
1070int16_t const circle[10][3]={
1071      { 4,5,         2},
1072    {2   ,   7,      6},
1073  {1     ,      8,   8},
1074  {1     ,      8,   8},
1075 {0      ,       9, 10},
1076 {0      ,       9, 10},
1077  {1     ,      8,   8},
1078  {1     ,      8,   8},
1079    {2   ,    7,     6},
1080     {  4,5,         2}};
1081void ST7735_DrawCircle(int16_t x, int16_t y, uint16_t color) {
1082  uint32_t i,w;
1083  uint8_t hi = color >> 8, lo = color;
1084  // rudimentary clipping
1085  if((x>_width-9)||(y>_height-9)) return; // doesn't fit
1086  for(i=0; i<10; i++){
1087    setAddrWindow(x+circle[i][0], y+i, x+circle[i][1], y+i);
1088    w = circle[i][2];
1089    while (w--) {
1090      writedata(hi);
1091      writedata(lo);
1092    }
1093  }
1094  deselect();
1095}
1096
1097//------------ST7735_Color565------------
1098// Pass 8-bit (each) R,G,B and get back 16-bit packed color.
1099// Input: r red value
1100//        g green value
1101//        b blue value
1102// Output: 16-bit color
1103uint16_t ST7735_Color565(uint8_t r, uint8_t g, uint8_t b) {
1104  return ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
1105}
1106
1107
1108//------------ST7735_SwapColor------------
1109// Swaps the red and blue values of the given 16-bit packed color;
1110// green is unchanged.
1111// Input: x 16-bit color in format B, G, R
1112// Output: 16-bit color in format R, G, B
1113uint16_t ST7735_SwapColor(uint16_t x) {
1114  return (x << 11) | (x & 0x07E0) | (x >> 11);
1115}
1116
1117
1118//------------ST7735_DrawBitmap------------
1119// Displays a 16-bit color BMP image.  A bitmap file that is created
1120// by a PC image processing program has a header and may be padded
1121// with dummy columns so the data have four byte alignment.  This
1122// function assumes that all of that has been stripped out, and the
1123// array image[] has one 16-bit halfword for each pixel to be
1124// displayed on the screen (encoded in reverse order, which is
1125// standard for bitmap files).  An array can be created in this
1126// format from a 24-bit-per-pixel .bmp file using the associated
1127// converter program.
1128// (x,y) is the screen location of the lower left corner of BMP image
1129// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen)
1130// Input: x     horizontal position of the bottom left corner of the image, columns from the left edge
1131//        y     vertical position of the bottom left corner of the image, rows from the top edge
1132//        image pointer to a 16-bit color BMP image
1133//        w     number of pixels wide
1134//        h     number of pixels tall
1135// Output: none
1136// Must be less than or equal to 128 pixels wide by 160 pixels high
1137void ST7735_DrawBitmap(int16_t x, int16_t y, const uint16_t *image, int16_t w, int16_t h){
1138  int16_t skipC = 0;                      // non-zero if columns need to be skipped due to clipping
1139  int16_t originalWidth = w;              // save this value; even if not all columns fit on the screen, the image is still this width in ROM
1140  int i = w*(h - 1);
1141
1142  if((x >= _width) || ((y - h + 1) >= _height) || ((x + w) <= 0) || (y < 0)){
1143    return;                             // image is totally off the screen, do nothing
1144  }
1145  if((w > _width) || (h > _height)){    // image is too wide for the screen, do nothing
1146    //***This isn't necessarily a fatal error, but it makes the
1147    //following logic much more complicated, since you can have
1148    //an image that exceeds multiple boundaries and needs to be
1149    //clipped on more than one side.
1150    return;
1151  }
1152  if((x + w - 1) >= _width){            // image exceeds right of screen
1153    skipC = (x + w) - _width;           // skip cut off columns
1154    w = _width - x;
1155  }
1156  if((y - h + 1) < 0){                  // image exceeds top of screen
1157    i = i - (h - y - 1)*originalWidth;  // skip the last cut off rows
1158    h = y + 1;
1159  }
1160  if(x < 0){                            // image exceeds left of screen
1161    w = w + x;
1162    skipC = -1*x;                       // skip cut off columns
1163    i = i - x;                          // skip the first cut off columns
1164    x = 0;
1165  }
1166  if(y >= _height){                     // image exceeds bottom of screen
1167    h = h - (y - _height + 1);
1168    y = _height - 1;
1169  }
1170
1171  setAddrWindow(x, y-h+1, x+w-1, y);
1172
1173  for(y=0; y<h; y=y+1){
1174    for(x=0; x<w; x=x+1){
1175                                        // send the top 8 bits
1176      writedata((uint8_t)(image[i] >> 8));
1177                                        // send the bottom 8 bits
1178      writedata((uint8_t)image[i]);
1179      i = i + 1;                        // go to the next pixel
1180    }
1181    i = i + skipC;
1182    i = i - 2*originalWidth;
1183  }
1184
1185  deselect();
1186}
1187
1188
1189//------------ST7735_DrawCharS------------
1190// Simple character draw function.  This is the same function from
1191// Adafruit_GFX.c but adapted for this processor.  However, each call
1192// to ST7735_DrawPixel() calls setAddrWindow(), which needs to send
1193// many extra data and commands.  If the background color is the same
1194// as the text color, no background will be printed, and text can be
1195// drawn right over existing images without covering them with a box.
1196// Requires (11 + 2*size*size)*6*8 (image fully on screen; textcolor != bgColor)
1197// Input: x         horizontal position of the top left corner of the character, columns from the left edge
1198//        y         vertical position of the top left corner of the character, rows from the top edge
1199//        c         character to be printed
1200//        textColor 16-bit color of the character
1201//        bgColor   16-bit color of the background
1202//        size      number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square)
1203// Output: none
1204void ST7735_DrawCharS(int16_t x, int16_t y, char c, int16_t textColor, int16_t bgColor, uint8_t size){
1205  uint8_t line; // vertical column of pixels of character in font
1206  int32_t i, j;
1207  if((x >= _width)            || // Clip right
1208     (y >= _height)           || // Clip bottom
1209     ((x + 6 * size - 1) < 0) || // Clip left
1210     ((y + 8 * size - 1) < 0))   // Clip top
1211    return;
1212
1213  for (i=0; i<6; i++ ) {
1214    if (i == 5)
1215      line = 0x0;
1216    else
1217      line = Font[(c*5)+i];
1218    for (j = 0; j<8; j++) {
1219      if (line & 0x1) {
1220        if (size == 1) // default size
1221          ST7735_DrawPixel(x+i, y+j, textColor);
1222        else {  // big size
1223          ST7735_FillRect(x+(i*size), y+(j*size), size, size, textColor);
1224        }
1225      } else if (bgColor != textColor) {
1226        if (size == 1) // default size
1227          ST7735_DrawPixel(x+i, y+j, bgColor);
1228        else {  // big size
1229          ST7735_FillRect(x+i*size, y+j*size, size, size, bgColor);
1230        }
1231      }
1232      line >>= 1;
1233    }
1234  }
1235}
1236
1237
1238//------------ST7735_DrawChar------------
1239// Advanced character draw function.  This is similar to the function
1240// from Adafruit_GFX.c but adapted for this processor.  However, this
1241// function only uses one call to setAddrWindow(), which allows it to
1242// run at least twice as fast.
1243// Requires (11 + size*size*6*8) bytes of transmission (assuming image fully on screen)
1244// Input: x         horizontal position of the top left corner of the character, columns from the left edge
1245//        y         vertical position of the top left corner of the character, rows from the top edge
1246//        c         character to be printed
1247//        textColor 16-bit color of the character
1248//        bgColor   16-bit color of the background
1249//        size      number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square)
1250// Output: none
1251void ST7735_DrawChar(int16_t x, int16_t y, char c, int16_t textColor, int16_t bgColor, uint8_t size){
1252  uint8_t line; // horizontal row of pixels of character
1253  int32_t col, row, i, j;// loop indices
1254  if(((x + 6*size - 1) >= _width)  || // Clip right
1255     ((y + 8*size - 1) >= _height) || // Clip bottom
1256     ((x + 6*size - 1) < 0)        || // Clip left
1257     ((y + 8*size - 1) < 0)){         // Clip top
1258    return;
1259  }
1260
1261  setAddrWindow(x, y, x+6*size-1, y+8*size-1);
1262
1263  line = 0x01;        // print the top row first
1264  // print the rows, starting at the top
1265  for(row=0; row<8; row=row+1){
1266    for(i=0; i<size; i=i+1){
1267      // print the columns, starting on the left
1268      for(col=0; col<5; col=col+1){
1269        if(Font[(c*5)+col]&line){
1270          // bit is set in Font, print pixel(s) in text color
1271          for(j=0; j<size; j=j+1){
1272            pushColor(textColor);
1273          }
1274        } else{
1275          // bit is cleared in Font, print pixel(s) in background color
1276          for(j=0; j<size; j=j+1){
1277            pushColor(bgColor);
1278          }
1279        }
1280      }
1281      // print blank column(s) to the right of character
1282      for(j=0; j<size; j=j+1){
1283        pushColor(bgColor);
1284      }
1285    }
1286    line = line<<1;   // move up to the next row
1287  }
1288
1289  deselect();
1290}
1291//------------ST7735_DrawString------------
1292// String draw function.
1293// 16 rows (0 to 15) and 21 characters (0 to 20)
1294// Requires (11 + size*size*6*8) bytes of transmission for each character
1295// Input: x         columns from the left edge (0 to 20)
1296//        y         rows from the top edge (0 to 15)
1297//        pt        pointer to a null terminated string to be printed
1298//        textColor 16-bit color of the characters
1299// bgColor is Black and size is 1
1300// Output: number of characters printed
1301uint32_t ST7735_DrawString(uint16_t x, uint16_t y, char *pt, int16_t textColor){
1302  uint32_t count = 0;
1303  if(y>15) return 0;
1304  while(*pt){
1305    ST7735_DrawCharS(x*6, y*10, *pt, textColor, ST7735_BLACK, 1);
1306    pt++;
1307    x = x+1;
1308    if(x>20) return count;  // number of characters printed
1309    count++;
1310  }
1311  return count;  // number of characters printed
1312}
1313
1314//-----------------------fillmessage-----------------------
1315// Output a 32-bit number in unsigned decimal format
1316// Input: 32-bit number to be transferred
1317// Output: none
1318// Variable format 1-10 digits with no space before or after
1319char Message[12];
1320uint32_t Messageindex;
1321
1322void fillmessage(uint32_t n){
1323// This function uses recursion to convert decimal number
1324//   of unspecified length as an ASCII string
1325  if(n >= 10){
1326    fillmessage(n/10);
1327    n = n%10;
1328  }
1329  Message[Messageindex] = (n+'0'); /* n is between 0 and 9 */
1330  if(Messageindex<11)Messageindex++;
1331}
1332//********ST7735_SetCursor*****************
1333// Move the cursor to the desired X- and Y-position.  The
1334// next character will be printed here.  X=0 is the leftmost
1335// column.  Y=0 is the top row.
1336// inputs: newX  new X-position of the cursor (0<=newX<=20)
1337//         newY  new Y-position of the cursor (0<=newY<=15)
1338// outputs: none
1339void ST7735_SetCursor(uint32_t newX, uint32_t newY){
1340  if((newX > 20) || (newY > 15)){       // bad input
1341    return;                             // do nothing
1342  }
1343  StX = newX;
1344  StY = newY;
1345}
1346//-----------------------ST7735_OutUDec-----------------------
1347// Output a 32-bit number in unsigned decimal format
1348// Position determined by ST7735_SetCursor command
1349// Color set by ST7735_SetTextColor
1350// Input: 32-bit number to be transferred
1351// Output: none
1352// Variable format 1-10 digits with no space before or after
1353void ST7735_OutUDec(uint32_t n){
1354  Messageindex = 0;
1355  fillmessage(n);
1356  Message[Messageindex] = 0; // terminate
1357  ST7735_DrawString(StX,StY,Message,StTextColor);
1358  StX = StX+Messageindex;
1359  if(StX>20){
1360    StX = 20;
1361    ST7735_DrawCharS(StX*6,StY*10,'*',ST7735_RED,ST7735_BLACK, 1);
1362  }
1363}
1364
1365
1366
1367
1368
1369#define MADCTL_MY  0x80
1370#define MADCTL_MX  0x40
1371#define MADCTL_MV  0x20
1372#define MADCTL_ML  0x10
1373#define MADCTL_RGB 0x00
1374#define MADCTL_BGR 0x08
1375#define MADCTL_MH  0x04
1376
1377//------------ST7735_SetRotation------------
1378// Change the image rotation.
1379// Requires 2 bytes of transmission
1380// Input: m new rotation value (0 to 3)
1381// Output: none
1382void ST7735_SetRotation(uint8_t m) {
1383
1384  writecommand(ST7735_MADCTL);
1385  Rotation = m % 4; // can't be higher than 3
1386  switch (Rotation) {
1387   case 0:
1388     if (TabColor == INITR_BLACKTAB) {
1389       writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
1390     } else {
1391       writedata(MADCTL_MX | MADCTL_MY | MADCTL_BGR);
1392     }
1393     _width  = ST7735_TFTWIDTH;
1394     _height = ST7735_TFTHEIGHT;
1395     break;
1396   case 1:
1397     if (TabColor == INITR_BLACKTAB) {
1398       writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
1399     } else {
1400       writedata(MADCTL_MY | MADCTL_MV | MADCTL_BGR);
1401     }
1402     _width  = ST7735_TFTHEIGHT;
1403     _height = ST7735_TFTWIDTH;
1404     break;
1405  case 2:
1406     if (TabColor == INITR_BLACKTAB) {
1407       writedata(MADCTL_RGB);
1408     } else {
1409       writedata(MADCTL_BGR);
1410     }
1411     _width  = ST7735_TFTWIDTH;
1412     _height = ST7735_TFTHEIGHT;
1413    break;
1414   case 3:
1415     if (TabColor == INITR_BLACKTAB) {
1416       writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
1417     } else {
1418       writedata(MADCTL_MX | MADCTL_MV | MADCTL_BGR);
1419     }
1420     _width  = ST7735_TFTHEIGHT;
1421     _height = ST7735_TFTWIDTH;
1422     break;
1423  }
1424
1425  deselect();
1426}
1427
1428
1429//------------ST7735_InvertDisplay------------
1430// Send the command to invert all of the colors.
1431// Requires 1 byte of transmission
1432// Input: i 0 to disable inversion; non-zero to enable inversion
1433// Output: none
1434void ST7735_InvertDisplay(int i) {
1435  if(i){
1436    writecommand(ST7735_INVON);
1437  } else{
1438    writecommand(ST7735_INVOFF);
1439  }
1440  deselect();
1441}
1442// graphics routines
1443// y coordinates 0 to 31 used for labels and messages
1444// y coordinates 32 to 159  128 pixels high
1445// x coordinates 0 to 127   128 pixels wide
1446
1447int32_t Ymax,Ymin,X;        // X goes from 0 to 127
1448int32_t Yrange; //YrangeDiv2;
1449
1450// *************** ST7735_PlotClear ********************
1451// Clear the graphics buffer, set X coordinate to 0
1452// This routine clears the display
1453// Inputs: ymin and ymax are range of the plot
1454// Outputs: none
1455void ST7735_PlotClear(int32_t ymin, int32_t ymax){
1456  ST7735_FillRect(0, 32, 128, 128, ST7735_Color565(228,228,228)); // light grey
1457  if(ymax>ymin){
1458    Ymax = ymax;
1459    Ymin = ymin;
1460    Yrange = ymax-ymin;
1461  } else{
1462    Ymax = ymin;
1463    Ymin = ymax;
1464    Yrange = ymax-ymin;
1465  }
1466  //YrangeDiv2 = Yrange/2;
1467  X = 0;
1468}
1469
1470// *************** ST7735_PlotPoint ********************
1471// Used in the voltage versus time plot, plot one point at y
1472// It does output to display
1473// Inputs: y is the y coordinate of the point plotted
1474// Outputs: none
1475void ST7735_PlotPoint(int32_t y){int32_t j;
1476  if(y<Ymin) y=Ymin;
1477  if(y>Ymax) y=Ymax;
1478  // X goes from 0 to 127
1479  // j goes from 159 to 32
1480  // y=Ymax maps to j=32
1481  // y=Ymin maps to j=159
1482  j = 32+(127*(Ymax-y))/Yrange;
1483  if(j<32) j = 32;
1484  if(j>159) j = 159;
1485  ST7735_DrawPixel(X,   j,   ST7735_BLUE);
1486  ST7735_DrawPixel(X+1, j,   ST7735_BLUE);
1487  ST7735_DrawPixel(X,   j+1, ST7735_BLUE);
1488  ST7735_DrawPixel(X+1, j+1, ST7735_BLUE);
1489}
1490// *************** ST7735_PlotLine ********************
1491// Used in the voltage versus time plot, plot line to new point
1492// It does output to display
1493// Inputs: y is the y coordinate of the point plotted
1494// Outputs: none
1495int32_t lastj=0;
1496void ST7735_PlotLine(int32_t y){int32_t i,j;
1497  if(y<Ymin) y=Ymin;
1498  if(y>Ymax) y=Ymax;
1499  // X goes from 0 to 127
1500  // j goes from 159 to 32
1501  // y=Ymax maps to j=32
1502  // y=Ymin maps to j=159
1503  j = 32+(127*(Ymax-y))/Yrange;
1504  if(j < 32) j = 32;
1505  if(j > 159) j = 159;
1506  if(lastj < 32) lastj = j;
1507  if(lastj > 159) lastj = j;
1508  if(lastj < j){
1509    for(i = lastj+1; i<=j ; i++){
1510      ST7735_DrawPixel(X,   i,   ST7735_BLUE) ;
1511      ST7735_DrawPixel(X+1, i,   ST7735_BLUE) ;
1512    }
1513  }else if(lastj > j){
1514    for(i = j; i<lastj ; i++){
1515      ST7735_DrawPixel(X,   i,   ST7735_BLUE) ;
1516      ST7735_DrawPixel(X+1, i,   ST7735_BLUE) ;
1517    }
1518  }else{
1519    ST7735_DrawPixel(X,   j,   ST7735_BLUE) ;
1520    ST7735_DrawPixel(X+1, j,   ST7735_BLUE) ;
1521  }
1522  lastj = j;
1523}
1524
1525// *************** ST7735_PlotPoints ********************
1526// Used in the voltage versus time plot, plot two points at y1, y2
1527// It does output to display
1528// Inputs: y1 is the y coordinate of the first point plotted
1529//         y2 is the y coordinate of the second point plotted
1530// Outputs: none
1531void ST7735_PlotPoints(int32_t y1,int32_t y2){int32_t j;
1532  if(y1<Ymin) y1=Ymin;
1533  if(y1>Ymax) y1=Ymax;
1534  // X goes from 0 to 127
1535  // j goes from 159 to 32
1536  // y=Ymax maps to j=32
1537  // y=Ymin maps to j=159
1538  j = 32+(127*(Ymax-y1))/Yrange;
1539  if(j<32) j = 32;
1540  if(j>159) j = 159;
1541  ST7735_DrawPixel(X, j, ST7735_BLUE);
1542  if(y2<Ymin) y2=Ymin;
1543  if(y2>Ymax) y2=Ymax;
1544  j = 32+(127*(Ymax-y2))/Yrange;
1545  if(j<32) j = 32;
1546  if(j>159) j = 159;
1547  ST7735_DrawPixel(X, j, ST7735_BLACK);
1548}
1549// *************** ST7735_PlotBar ********************
1550// Used in the voltage versus time bar, plot one bar at y
1551// It does not output to display until RIT128x96x4ShowPlot called
1552// Inputs: y is the y coordinate of the bar plotted
1553// Outputs: none
1554void ST7735_PlotBar(int32_t y){
1555int32_t j;
1556  if(y<Ymin) y=Ymin;
1557  if(y>Ymax) y=Ymax;
1558  // X goes from 0 to 127
1559  // j goes from 159 to 32
1560  // y=Ymax maps to j=32
1561  // y=Ymin maps to j=159
1562  j = 32+(127*(Ymax-y))/Yrange;
1563  ST7735_DrawFastVLine(X, j, 159-j, ST7735_BLACK);
1564
1565}
1566
1567// full scaled defined as 3V
1568// Input is 0 to 511, 0 => 159 and 511 => 32
1569uint8_t const dBfs[512]={
1570159, 159, 145, 137, 131, 126, 123, 119, 117, 114, 112, 110, 108, 107, 105, 104, 103, 101,
1571  100, 99, 98, 97, 96, 95, 94, 93, 93, 92, 91, 90, 90, 89, 88, 88, 87, 87, 86, 85, 85, 84,
1572  84, 83, 83, 82, 82, 81, 81, 81, 80, 80, 79, 79, 79, 78, 78, 77, 77, 77, 76, 76, 76, 75,
1573  75, 75, 74, 74, 74, 73, 73, 73, 72, 72, 72, 72, 71, 71, 71, 71, 70, 70, 70, 70, 69, 69,
1574  69, 69, 68, 68, 68, 68, 67, 67, 67, 67, 66, 66, 66, 66, 66, 65, 65, 65, 65, 65, 64, 64,
1575  64, 64, 64, 63, 63, 63, 63, 63, 63, 62, 62, 62, 62, 62, 62, 61, 61, 61, 61, 61, 61, 60,
1576  60, 60, 60, 60, 60, 59, 59, 59, 59, 59, 59, 59, 58, 58, 58, 58, 58, 58, 58, 57, 57, 57,
1577  57, 57, 57, 57, 56, 56, 56, 56, 56, 56, 56, 56, 55, 55, 55, 55, 55, 55, 55, 55, 54, 54,
1578  54, 54, 54, 54, 54, 54, 53, 53, 53, 53, 53, 53, 53, 53, 53, 52, 52, 52, 52, 52, 52, 52,
1579  52, 52, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 50, 50, 50, 50, 50, 50, 50, 50, 50,
1580  50, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
1581  48, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 46, 46, 46, 46, 46, 46, 46, 46, 46,
1582  46, 46, 46, 46, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 44, 44, 44, 44, 44,
1583  44, 44, 44, 44, 44, 44, 44, 44, 44, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
1584  43, 43, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 41, 41, 41, 41, 41,
1585  41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
1586  40, 40, 40, 40, 40, 40, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39,
1587  39, 39, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 37,
1588  37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 36, 36, 36, 36,
1589  36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 35, 35, 35, 35, 35,
1590  35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 34, 34, 34, 34, 34, 34,
1591  34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 33, 33, 33, 33, 33,
1592  33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32,
1593  32, 32, 32, 32, 32, 32, 32, 32, 32, 32
1594};
1595
1596// *************** ST7735_PlotdBfs ********************
1597// Used in the amplitude versus frequency plot, plot bar point at y
1598// 0 to 0.625V scaled on a log plot from min to max
1599// It does output to display
1600// Inputs: y is the y ADC value of the bar plotted
1601// Outputs: none
1602void ST7735_PlotdBfs(int32_t y){
1603int32_t j;
1604  y = y/2; // 0 to 2047
1605  if(y<0) y=0;
1606  if(y>511) y=511;
1607  // X goes from 0 to 127
1608  // j goes from 159 to 32
1609  // y=511 maps to j=32
1610  // y=0 maps to j=159
1611  j = dBfs[y];
1612  ST7735_DrawFastVLine(X, j, 159-j, ST7735_BLACK);
1613
1614}
1615
1616// *************** ST7735_PlotNext ********************
1617// Used in all the plots to step the X coordinate one pixel
1618// X steps from 0 to 127, then back to 0 again
1619// It does not output to display
1620// Inputs: none
1621// Outputs: none
1622void ST7735_PlotNext(void){
1623  if(X==127){
1624    X = 0;
1625  } else{
1626    X++;
1627  }
1628}
1629
1630// *************** ST7735_PlotNextErase ********************
1631// Used in all the plots to step the X coordinate one pixel
1632// X steps from 0 to 127, then back to 0 again
1633// It clears the vertical space into which the next pixel will be drawn
1634// Inputs: none
1635// Outputs: none
1636void ST7735_PlotNextErase(void){
1637  if(X==127){
1638    X = 0;
1639  } else{
1640    X++;
1641  }
1642  ST7735_DrawFastVLine(X,32,128,ST7735_Color565(228,228,228));
1643}
1644
1645// Used in all the plots to write buffer to LCD
1646// Example 1 Voltage versus time
1647//    ST7735_PlotClear(0,4095);  // range from 0 to 4095
1648//    ST7735_PlotPoint(data); ST7735_PlotNext(); // called 128 times
1649
1650// Example 2a Voltage versus time (N data points/pixel, time scale)
1651//    ST7735_PlotClear(0,4095);  // range from 0 to 4095
1652//    {   for(j=0;j<N;j++){
1653//          ST7735_PlotPoint(data[i++]); // called N times
1654//        }
1655//        ST7735_PlotNext();
1656//    }   // called 128 times
1657
1658// Example 2b Voltage versus time (N data points/pixel, time scale)
1659//    ST7735_PlotClear(0,4095);  // range from 0 to 4095
1660//    {   for(j=0;j<N;j++){
1661//          ST7735_PlotLine(data[i++]); // called N times
1662//        }
1663//        ST7735_PlotNext();
1664//    }   // called 128 times
1665
1666// Example 3 Voltage versus frequency (512 points)
1667//    perform FFT to get 512 magnitudes, mag[i] (0 to 4095)
1668//    ST7735_PlotClear(0,1023);  // clip large magnitudes
1669//    {
1670//        ST7735_PlotBar(mag[i++]); // called 4 times
1671//        ST7735_PlotBar(mag[i++]);
1672//        ST7735_PlotBar(mag[i++]);
1673//        ST7735_PlotBar(mag[i++]);
1674//        ST7735_PlotNext();
1675//    }   // called 128 times
1676
1677// Example 4 Voltage versus frequency (512 points), dB scale
1678//    perform FFT to get 512 magnitudes, mag[i] (0 to 4095)
1679//    ST7735_PlotClear(0,511);  // parameters ignored
1680//    {
1681//        ST7735_PlotdBfs(mag[i++]); // called 4 times
1682//        ST7735_PlotdBfs(mag[i++]);
1683//        ST7735_PlotdBfs(mag[i++]);
1684//        ST7735_PlotdBfs(mag[i++]);
1685//        ST7735_PlotNext();
1686//    }   // called 128 times
1687
1688// *************** ST7735_OutChar ********************
1689// Output one character to the LCD
1690// Position determined by ST7735_SetCursor command
1691// Color set by ST7735_SetTextColor
1692// Inputs: 8-bit ASCII character
1693// Outputs: none
1694void ST7735_OutChar(char ch){
1695  if((ch == 10) || (ch == 13) || (ch == 27)){
1696    StY++; StX=0;
1697    if(StY>15){
1698      StY = 0;
1699    }
1700    ST7735_DrawString(0,StY,"                     ",StTextColor);
1701    return;
1702  }
1703  ST7735_DrawCharS(StX*6,StY*10,ch,ST7735_YELLOW,ST7735_BLACK, 1);
1704  StX++;
1705  if(StX>20){
1706    StX = 20;
1707    ST7735_DrawCharS(StX*6,StY*10,'*',ST7735_RED,ST7735_BLACK, 1);
1708  }
1709  return;
1710}
1711//********ST7735_OutString*****************
1712// Print a string of characters to the ST7735 LCD.
1713// Position determined by ST7735_SetCursor command
1714// Color set by ST7735_SetTextColor
1715// The string will not automatically wrap.
1716// inputs: ptr  pointer to NULL-terminated ASCII string
1717// outputs: none
1718void ST7735_OutString(char *ptr){
1719  while(*ptr){
1720    ST7735_OutChar(*ptr);
1721    ptr = ptr + 1;
1722  }
1723}
1724// ************** ST7735_SetTextColor ************************
1725// Sets the color in which the characters will be printed
1726// Background color is fixed at black
1727// Input:  16-bit packed color
1728// Output: none
1729// ********************************************************
1730void ST7735_SetTextColor(uint16_t color){
1731  StTextColor = color;
1732}
1733
1734// *************** Output_Init ********************
1735// Standard device driver initialization function for printf
1736// Initialize ST7735 LCD
1737// Inputs: none
1738// Outputs: none
1739void Output_Init(void){
1740  ST7735_InitR(INITR_REDTAB);
1741  ST7735_FillScreen(0);                 // set screen to black
1742}
1743
1744// Clear display
1745void Output_Clear(void){ // Clears the display
1746  ST7735_FillScreen(0);    // set screen to black
1747}
1748// Turn off display (low power)
1749void Output_Off(void){   // Turns off the display
1750  Output_Clear();  // not implemented
1751}
1752// Turn on display
1753void Output_On(void){ // Turns on the display
1754  Output_Init();      // reinitialize
1755}
1756// set the color for future output
1757// Background color is fixed at black
1758// Input:  16-bit packed color
1759// Output: none
1760void Output_Color(uint32_t newColor){ // Set color of future output
1761  ST7735_SetTextColor(newColor);
1762}