1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | * Copyright (C) 2004-2012 by Embedded and Real-Time Systems Laboratory
|
---|
9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
10 | * Copyright (C) 2015-2017 by TOPPERS PROJECT Educational Working Group.
|
---|
11 | *
|
---|
12 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
13 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
14 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
15 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
16 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
17 | * スコード中に含まれていること.
|
---|
18 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
19 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
20 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
21 | * の無保証規定を掲載すること.
|
---|
22 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
23 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
24 | * と.
|
---|
25 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
26 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
27 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
28 | * 報告すること.
|
---|
29 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
30 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
31 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
32 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
33 | * 免責すること.
|
---|
34 | *
|
---|
35 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
36 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
37 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
38 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
39 | * の責任を負わない.
|
---|
40 | *
|
---|
41 | * $Id$
|
---|
42 | */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Written by Limor Fried/Ladyada for Adafruit Industries.
|
---|
46 | * MIT license, all text above must be included in any redistribution
|
---|
47 | * http://opensource.org/licenses/mit-license.php
|
---|
48 | */
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * ADAFRUIT ST7735 1.8"LCD制御プログラムの本体
|
---|
52 | */
|
---|
53 |
|
---|
54 | #include <kernel.h>
|
---|
55 | #include <t_syslog.h>
|
---|
56 | #include <t_stdlib.h>
|
---|
57 | #include <target_syssvc.h>
|
---|
58 | #include "syssvc/serial.h"
|
---|
59 | #include "syssvc/syslog.h"
|
---|
60 | #include "gpio_api.h"
|
---|
61 | #include "spi_api.h"
|
---|
62 | #include "adafruit_st7735.h"
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * サービスコールのエラーのログ出力
|
---|
66 | */
|
---|
67 | Inline void
|
---|
68 | svc_perror(const char *file, int_t line, const char *expr, ER ercd)
|
---|
69 | {
|
---|
70 | if (ercd < 0) {
|
---|
71 | t_perror(LOG_ERROR, file, line, expr, ercd);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | #define SVC_PERROR(expr) svc_perror(__FILE__, __LINE__, #expr, (expr))
|
---|
76 | #define ABS(X) ((X) > 0 ? (X) : -(X))
|
---|
77 |
|
---|
78 |
|
---|
79 | // Rather than a bazillion writecommand() and writedata() calls, screen
|
---|
80 | // initialization commands and arguments are organized in these tables
|
---|
81 | // stored in PROGMEM. The table may look bulky, but that's mostly the
|
---|
82 | // formatting -- storage-wise this is hundreds of bytes more compact
|
---|
83 | // than the equivalent code. Companion function follows.
|
---|
84 | #define DELAY 0x80
|
---|
85 | static const uint8_t
|
---|
86 | Bcmd[] = { // Initialization commands for 7735B screens
|
---|
87 | 18, // 18 commands in list:
|
---|
88 | ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
|
---|
89 | 50, // 50 ms delay
|
---|
90 | ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
|
---|
91 | 255, // 255 = 500 ms delay
|
---|
92 | ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
|
---|
93 | 0x05, // 16-bit color
|
---|
94 | 10, // 10 ms delay
|
---|
95 | ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
|
---|
96 | 0x00, // fastest refresh
|
---|
97 | 0x06, // 6 lines front porch
|
---|
98 | 0x03, // 3 lines back porch
|
---|
99 | 10, // 10 ms delay
|
---|
100 | ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
|
---|
101 | 0x08, // Row addr/col addr, bottom to top refresh
|
---|
102 | ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
|
---|
103 | 0x15, // 1 clk cycle nonoverlap, 2 cycle gate
|
---|
104 | // rise, 3 cycle osc equalize
|
---|
105 | 0x02, // Fix on VTL
|
---|
106 | ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
|
---|
107 | 0x0, // Line inversion
|
---|
108 | ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
|
---|
109 | 0x02, // GVDD = 4.7V
|
---|
110 | 0x70, // 1.0uA
|
---|
111 | 10, // 10 ms delay
|
---|
112 | ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
|
---|
113 | 0x05, // VGH = 14.7V, VGL = -7.35V
|
---|
114 | ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
|
---|
115 | 0x01, // Opamp current small
|
---|
116 | 0x02, // Boost frequency
|
---|
117 | ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
|
---|
118 | 0x3C, // VCOMH = 4V
|
---|
119 | 0x38, // VCOML = -1.1V
|
---|
120 | 10, // 10 ms delay
|
---|
121 | ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
|
---|
122 | 0x11, 0x15,
|
---|
123 | ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
|
---|
124 | 0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
|
---|
125 | 0x21, 0x1B, 0x13, 0x19, // these config values represent)
|
---|
126 | 0x17, 0x15, 0x1E, 0x2B,
|
---|
127 | 0x04, 0x05, 0x02, 0x0E,
|
---|
128 | ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
|
---|
129 | 0x0B, 0x14, 0x08, 0x1E, // (ditto)
|
---|
130 | 0x22, 0x1D, 0x18, 0x1E,
|
---|
131 | 0x1B, 0x1A, 0x24, 0x2B,
|
---|
132 | 0x06, 0x06, 0x02, 0x0F,
|
---|
133 | 10, // 10 ms delay
|
---|
134 | ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
|
---|
135 | 0x00, 0x02, // XSTART = 2
|
---|
136 | 0x00, 0x81, // XEND = 129
|
---|
137 | ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
|
---|
138 | 0x00, 0x02, // XSTART = 1
|
---|
139 | 0x00, 0x81, // XEND = 160
|
---|
140 | ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
|
---|
141 | 10, // 10 ms delay
|
---|
142 | ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
|
---|
143 | 255}; // 255 = 500 ms delay
|
---|
144 |
|
---|
145 | static const uint8_t
|
---|
146 | Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
|
---|
147 | 15, // 15 commands in list:
|
---|
148 | ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
|
---|
149 | 150, // 150 ms delay
|
---|
150 | ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
|
---|
151 | 255, // 500 ms delay
|
---|
152 | ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
|
---|
153 | 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
---|
154 | ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
|
---|
155 | 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
---|
156 | ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
|
---|
157 | 0x01, 0x2C, 0x2D, // Dot inversion mode
|
---|
158 | 0x01, 0x2C, 0x2D, // Line inversion mode
|
---|
159 | ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
|
---|
160 | 0x07, // No inversion
|
---|
161 | ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
|
---|
162 | 0xA2,
|
---|
163 | 0x02, // -4.6V
|
---|
164 | 0x84, // AUTO mode
|
---|
165 | ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
|
---|
166 | 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
|
---|
167 | ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
|
---|
168 | 0x0A, // Opamp current small
|
---|
169 | 0x00, // Boost frequency
|
---|
170 | ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
|
---|
171 | 0x8A, // BCLK/2, Opamp current small & Medium low
|
---|
172 | 0x2A,
|
---|
173 | ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
|
---|
174 | 0x8A, 0xEE,
|
---|
175 | ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
|
---|
176 | 0x0E,
|
---|
177 | ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
|
---|
178 | ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
|
---|
179 | 0xC8, // row addr/col addr, bottom to top refresh
|
---|
180 | ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
|
---|
181 | 0x05 }; // 16-bit color
|
---|
182 |
|
---|
183 | static const uint8_t
|
---|
184 | Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
|
---|
185 | 2, // 2 commands in list:
|
---|
186 | ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
|
---|
187 | 0x00, 0x02, // XSTART = 0
|
---|
188 | 0x00, 0x7F+0x02, // XEND = 127
|
---|
189 | ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
|
---|
190 | 0x00, 0x01, // XSTART = 0
|
---|
191 | 0x00, 0x9F+0x01 }; // XEND = 159
|
---|
192 |
|
---|
193 | static const uint8_t
|
---|
194 | Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
|
---|
195 | 2, // 2 commands in list:
|
---|
196 | ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
|
---|
197 | 0x00, 0x00, // XSTART = 0
|
---|
198 | 0x00, 0x7F, // XEND = 127
|
---|
199 | ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
|
---|
200 | 0x00, 0x00, // XSTART = 0
|
---|
201 | 0x00, 0x9F }; // XEND = 159
|
---|
202 |
|
---|
203 | static const uint8_t
|
---|
204 | Rcmd2green144[] = { // Init for 7735R, part 2 (green 1.44 tab)
|
---|
205 | 2, // 2 commands in list:
|
---|
206 | ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
|
---|
207 | 0x00, 0x00, // XSTART = 0
|
---|
208 | 0x00, 0x7F, // XEND = 127
|
---|
209 | ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
|
---|
210 | 0x00, 0x00, // XSTART = 0
|
---|
211 | 0x00, 0x7F }; // XEND = 127
|
---|
212 |
|
---|
213 | static const uint8_t
|
---|
214 | Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
|
---|
215 | 4, // 4 commands in list:
|
---|
216 | ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
|
---|
217 | 0x02, 0x1c, 0x07, 0x12,
|
---|
218 | 0x37, 0x32, 0x29, 0x2d,
|
---|
219 | 0x29, 0x25, 0x2B, 0x39,
|
---|
220 | 0x00, 0x01, 0x03, 0x10,
|
---|
221 | ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
|
---|
222 | 0x03, 0x1d, 0x07, 0x06,
|
---|
223 | 0x2E, 0x2C, 0x29, 0x2D,
|
---|
224 | 0x2E, 0x2E, 0x37, 0x3F,
|
---|
225 | 0x00, 0x00, 0x02, 0x10,
|
---|
226 | ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
|
---|
227 | 10, // 10 ms delay
|
---|
228 | ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
|
---|
229 | 100 }; // 100 ms delay
|
---|
230 |
|
---|
231 |
|
---|
232 |
|
---|
233 |
|
---|
234 | /* Buffer used for transmission */
|
---|
235 | static uint8_t aTxBuffer[4];
|
---|
236 |
|
---|
237 | #define PORT_HIGH 1
|
---|
238 | #define PORT_LOW 0
|
---|
239 |
|
---|
240 | #define cs_set(sw) gpio_write(&hlcd->cs_pin, sw)
|
---|
241 | #define rs_set(sw) gpio_write(&hlcd->rs_pin, sw)
|
---|
242 | #define rst_set(sw) gpio_write(&hlcd->rst_pin, sw)
|
---|
243 | #define cs2_set(sw) gpio_write(&hlcd->cs2_pin, sw)
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * LCDへのコマンド送信関数
|
---|
247 | */
|
---|
248 | ER
|
---|
249 | lcd_writecommand(LCD_Handler_t *hlcd, uint8_t c)
|
---|
250 | {
|
---|
251 | ER ercd = E_OK;
|
---|
252 |
|
---|
253 | if(hlcd->spi_lock != 0){
|
---|
254 | if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
|
---|
255 | return ercd;
|
---|
256 | }
|
---|
257 | rs_set(PORT_LOW);
|
---|
258 | cs_set(PORT_LOW);
|
---|
259 | spi_master_write(&hlcd->hspi, c);
|
---|
260 | cs_set(PORT_HIGH);
|
---|
261 | if(hlcd->spi_lock != 0)
|
---|
262 | sig_sem(hlcd->spi_lock);
|
---|
263 | return ercd;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * LCDへのデータ送信関数
|
---|
268 | */
|
---|
269 | ER
|
---|
270 | lcd_writedata(LCD_Handler_t *hlcd, uint8_t c)
|
---|
271 | {
|
---|
272 | ER ercd = E_OK;
|
---|
273 |
|
---|
274 | if(hlcd->spi_lock != 0){
|
---|
275 | if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
|
---|
276 | return ercd;
|
---|
277 | }
|
---|
278 | rs_set(PORT_HIGH);
|
---|
279 | cs_set(PORT_LOW);
|
---|
280 | //dly_tsk(100/*us*/);
|
---|
281 | spi_master_write(&hlcd->hspi, c);
|
---|
282 | //dly_tsk(100/*us*/);
|
---|
283 | cs_set(PORT_HIGH);
|
---|
284 | if(hlcd->spi_lock != 0)
|
---|
285 | sig_sem(hlcd->spi_lock);
|
---|
286 | return ercd;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * LCDへの2バイトデータ送信関数
|
---|
291 | */
|
---|
292 | ER
|
---|
293 | lcd_writedata2(LCD_Handler_t *hlcd, uint16_t c, int cnt)
|
---|
294 | {
|
---|
295 | ER ercd = E_OK;
|
---|
296 | int i;
|
---|
297 |
|
---|
298 | if(cnt == 0)
|
---|
299 | return ercd;
|
---|
300 | if(hlcd->spi_lock != 0){
|
---|
301 | if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
|
---|
302 | return ercd;
|
---|
303 | }
|
---|
304 | rs_set(PORT_HIGH);
|
---|
305 | cs_set(PORT_LOW);
|
---|
306 | //dly_tsk(100/*us*/);
|
---|
307 | for(i = 0 ; i < cnt ; i++){
|
---|
308 | spi_master_write(&hlcd->hspi, c >> 8);
|
---|
309 | spi_master_write(&hlcd->hspi, c);
|
---|
310 | }
|
---|
311 | //dly_tsk(100/*us*/);
|
---|
312 | cs_set(PORT_HIGH);
|
---|
313 | if(hlcd->spi_lock != 0)
|
---|
314 | sig_sem(hlcd->spi_lock);
|
---|
315 | return ercd;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * LCDへのRGBデータ送信関数
|
---|
320 | */
|
---|
321 | ER
|
---|
322 | lcd_writedata3(LCD_Handler_t *hlcd, uint8_t *pbmp, uint16_t width, uint16_t height)
|
---|
323 | {
|
---|
324 | ER ercd = E_OK;
|
---|
325 | int index, i;
|
---|
326 |
|
---|
327 | if(width == 0 || height == 0)
|
---|
328 | return ercd;
|
---|
329 | if(hlcd->spi_lock != 0){
|
---|
330 | if((ercd = wai_sem(hlcd->spi_lock)) != E_OK)
|
---|
331 | return ercd;
|
---|
332 | }
|
---|
333 | rs_set(PORT_HIGH);
|
---|
334 | cs_set(PORT_LOW);
|
---|
335 | //dly_tsk(100/*us*/);
|
---|
336 | for(index=0; index < height; index++){
|
---|
337 | uint8_t *p = pbmp;
|
---|
338 | for (i = 0; i < width; i++){
|
---|
339 | aTxBuffer[0] = (*p++) & 0xf8;
|
---|
340 | aTxBuffer[0] |= *p >> 5;
|
---|
341 | aTxBuffer[1] = (*p++ << 3) & 0xE0;
|
---|
342 | aTxBuffer[1] |= (*p++ >> 3) & 0x1F;
|
---|
343 | spi_master_write(&hlcd->hspi, aTxBuffer[0]);
|
---|
344 | spi_master_write(&hlcd->hspi, aTxBuffer[1]);
|
---|
345 | }
|
---|
346 | pbmp -= width*3;
|
---|
347 | }
|
---|
348 | //dly_tsk(100/*us*/);
|
---|
349 | cs_set(PORT_HIGH);
|
---|
350 | if(hlcd->spi_lock != 0)
|
---|
351 | sig_sem(hlcd->spi_lock);
|
---|
352 | return ercd;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /*
|
---|
356 | * ST7735へのコマンドリスト送信
|
---|
357 | */
|
---|
358 | static void
|
---|
359 | Adafruit_ST7735_commandList(LCD_Handler_t *hlcd, const uint8_t *addr)
|
---|
360 | {
|
---|
361 | uint8_t numCommands, numArgs;
|
---|
362 | uint16_t ms;
|
---|
363 |
|
---|
364 | numCommands = *addr++; // Number of commands to follow
|
---|
365 | while(numCommands--) { // For each command...
|
---|
366 | SVC_PERROR(lcd_writecommand(hlcd, *addr++)); // Read, issue command
|
---|
367 | numArgs = *addr++; // Number of args to follow
|
---|
368 | ms = numArgs & DELAY; // If hibit set, delay follows args
|
---|
369 | numArgs &= ~DELAY; // Mask out delay bit
|
---|
370 | while(numArgs--) { // For each argument...
|
---|
371 | SVC_PERROR(lcd_writedata(hlcd, *addr++)); // Read, issue argument
|
---|
372 | }
|
---|
373 |
|
---|
374 | if(ms) {
|
---|
375 | ms = *addr++; // Read post-command delay time (ms)
|
---|
376 | if(ms == 255) ms = 500; // If 255, delay for 500 ms
|
---|
377 | dly_tsk(ms * 1000);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * ST7735B または ST7735RタイプのLEDの共通初期化関数
|
---|
385 | */
|
---|
386 | static void
|
---|
387 | Adafruit_ST7735_commonInit(LCD_Handler_t *hlcd, const uint8_t *cmdList)
|
---|
388 | {
|
---|
389 | hlcd->colstart = hlcd->rowstart = 0; // May be overridden in init func
|
---|
390 | hlcd->_width = ST7735_TFTWIDTH;
|
---|
391 | hlcd->_height = ST7735_TFTHEIGHT_18;
|
---|
392 |
|
---|
393 | // toggle RST low to reset; CS low so it'll listen to us
|
---|
394 | cs_set(PORT_LOW);
|
---|
395 | rst_set(PORT_HIGH);
|
---|
396 | dly_tsk(500 * 1000);
|
---|
397 | rst_set(PORT_LOW);
|
---|
398 | dly_tsk(500 * 1000);
|
---|
399 | rst_set(PORT_HIGH);
|
---|
400 |
|
---|
401 | if(cmdList) Adafruit_ST7735_commandList(hlcd, cmdList);
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | /*
|
---|
406 | * 表示ウィンドウ設定
|
---|
407 | */
|
---|
408 | void
|
---|
409 | lcd_setAddrWindow(LCD_Handler_t *hlcd, uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
|
---|
410 | {
|
---|
411 | SVC_PERROR(lcd_writecommand(hlcd, ST7735_CASET)); // Column addr set
|
---|
412 | SVC_PERROR(lcd_writedata(hlcd, 0x00));
|
---|
413 | SVC_PERROR(lcd_writedata(hlcd, x0+hlcd->colstart)); // XSTART
|
---|
414 | SVC_PERROR(lcd_writedata(hlcd, 0x00));
|
---|
415 | SVC_PERROR(lcd_writedata(hlcd, x1+hlcd->colstart)); // XEND
|
---|
416 |
|
---|
417 | SVC_PERROR(lcd_writecommand(hlcd, ST7735_RASET)); // Row addr set
|
---|
418 | SVC_PERROR(lcd_writedata(hlcd, 0x00));
|
---|
419 | SVC_PERROR(lcd_writedata(hlcd, y0+hlcd->rowstart)); // YSTART
|
---|
420 | SVC_PERROR(lcd_writedata(hlcd, 0x00));
|
---|
421 | SVC_PERROR(lcd_writedata(hlcd, y1+hlcd->rowstart)); // YEND
|
---|
422 |
|
---|
423 | SVC_PERROR(lcd_writecommand(hlcd, ST7735_RAMWR)); // write to RAM
|
---|
424 | }
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * ST7735B screensの初期化関数
|
---|
428 | */
|
---|
429 | void
|
---|
430 | lcd_initB(LCD_Handler_t *hlcd)
|
---|
431 | {
|
---|
432 | Adafruit_ST7735_commonInit(hlcd, Bcmd);
|
---|
433 | }
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * ST7735R screens (green or red tabs)の初期化関数
|
---|
437 | */
|
---|
438 | void
|
---|
439 | lcd_initR(LCD_Handler_t *hlcd, uint8_t options)
|
---|
440 | {
|
---|
441 | Adafruit_ST7735_commonInit(hlcd, Rcmd1);
|
---|
442 |
|
---|
443 | if(options == INITR_GREENTAB){
|
---|
444 | Adafruit_ST7735_commandList(hlcd, Rcmd2green);
|
---|
445 | hlcd->colstart = 2;
|
---|
446 | hlcd->rowstart = 1;
|
---|
447 | }
|
---|
448 | else if(options == INITR_144GREENTAB){
|
---|
449 | hlcd->_height = ST7735_TFTHEIGHT_144;
|
---|
450 | Adafruit_ST7735_commandList(hlcd, Rcmd2green144);
|
---|
451 | hlcd->colstart = 2;
|
---|
452 | hlcd->rowstart = 3;
|
---|
453 | }
|
---|
454 | else{
|
---|
455 | // colstart, rowstart left at default '0' values
|
---|
456 | Adafruit_ST7735_commandList(hlcd, Rcmd2red);
|
---|
457 | }
|
---|
458 | Adafruit_ST7735_commandList(hlcd, Rcmd3);
|
---|
459 |
|
---|
460 | // if black, change MADCTL color filter
|
---|
461 | if(options == INITR_BLACKTAB){
|
---|
462 | SVC_PERROR(lcd_writecommand(hlcd, ST7735_MADCTL));
|
---|
463 | SVC_PERROR(lcd_writedata(hlcd, 0xC0));
|
---|
464 | }
|
---|
465 | syslog_2(LOG_NOTICE, "## width(%d) height(%d) ##", hlcd->_width, hlcd->_height);
|
---|
466 | hlcd->tabcolor = options;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * RECTANGLEのフィル描画
|
---|
471 | * param1 hlcd: Pointer to LCD Handler
|
---|
472 | * param2 x: start X position
|
---|
473 | * param3 y: start Y position
|
---|
474 | * param4 w: width
|
---|
475 | * param5 h: height
|
---|
476 | * param6 color: color value
|
---|
477 | */
|
---|
478 | void
|
---|
479 | lcd_fillRect(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
|
---|
480 | {
|
---|
481 | // rudimentary clipping (drawChar w/big text requires this)
|
---|
482 | if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
|
---|
483 | if((x + w - 1) >= hlcd->_width) w = hlcd->_width - x;
|
---|
484 | if((y + h - 1) >= hlcd->_height) h = hlcd->_height - y;
|
---|
485 |
|
---|
486 | lcd_setAddrWindow(hlcd, x, y, x+w-1, y+h-1);
|
---|
487 | SVC_PERROR(lcd_writedata2(hlcd, color, h*w));
|
---|
488 | }
|
---|
489 |
|
---|
490 | void
|
---|
491 | lcd_pushColor(LCD_Handler_t *hlcd, uint16_t color)
|
---|
492 | {
|
---|
493 | SVC_PERROR(lcd_writedata2(hlcd, color, 1));
|
---|
494 | }
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * PIXEL描画
|
---|
498 | * param1 hlcd: Pointer to LCD Handler
|
---|
499 | * param2 x: X position
|
---|
500 | * param3 y: Y position
|
---|
501 | * param4 color: color value
|
---|
502 | */
|
---|
503 | void
|
---|
504 | lcd_drawPixel(LCD_Handler_t *hlcd, int16_t x, int16_t y, uint16_t color)
|
---|
505 | {
|
---|
506 | if((x < 0) ||(x >= hlcd->_width) || (y < 0) || (y >= hlcd->_height)) return;
|
---|
507 |
|
---|
508 | lcd_setAddrWindow(hlcd, x, y, x+1, y+1);
|
---|
509 | SVC_PERROR(lcd_writedata2(hlcd, color, 1));
|
---|
510 | }
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * 垂直方向LINEの高速描画
|
---|
514 | * param1 hlcd: Pointer to LCD Handler
|
---|
515 | * param2 x: start X position
|
---|
516 | * param3 y: start Y position
|
---|
517 | * param4 h: height
|
---|
518 | * param5 color: color value
|
---|
519 | */
|
---|
520 | void
|
---|
521 | lcd_drawFastVLine(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t h, uint16_t color)
|
---|
522 | {
|
---|
523 | // Rudimentary clipping
|
---|
524 | if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
|
---|
525 | if((y+h-1) >= hlcd->_height) h = hlcd->_height-y;
|
---|
526 | lcd_setAddrWindow(hlcd, x, y, x, y+h-1);
|
---|
527 | SVC_PERROR(lcd_writedata2(hlcd, color, h));
|
---|
528 | }
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * 水平方向LINEの高速描画
|
---|
532 | * param1 hlcd: Pointer to LCD Handler
|
---|
533 | * param2 x: start X position
|
---|
534 | * param3 y: start Y position
|
---|
535 | * param4 w: width
|
---|
536 | * param5 color: color value
|
---|
537 | */
|
---|
538 | void lcd_drawFastHLine(LCD_Handler_t *hlcd, int16_t x, int16_t y, int16_t w, uint16_t color)
|
---|
539 | {
|
---|
540 | // Rudimentary clipping
|
---|
541 | if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
|
---|
542 | if((x+w-1) >= hlcd->_width) w = hlcd->_width-x;
|
---|
543 | lcd_setAddrWindow(hlcd, x, y, x+w-1, y);
|
---|
544 | SVC_PERROR(lcd_writedata2(hlcd, color, w));
|
---|
545 | }
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * BITMAP描画
|
---|
549 | * param1 hlcd: Pointer to LCD Handler
|
---|
550 | * param2 x0: Bmp X position in the LCD
|
---|
551 | * param3 y0: Bmp Y position in the LCD
|
---|
552 | * param4 pbmp: Pointer to Bmp picture address in the internal Flash
|
---|
553 | */
|
---|
554 | void
|
---|
555 | lcd_drawBitmap(LCD_Handler_t *hlcd, uint16_t x0, uint16_t y0, uint8_t *pbmp)
|
---|
556 | {
|
---|
557 | uint32_t index = 0, width = 0, height = 0, bit_pixel = 0;
|
---|
558 | uint32_t input_color_mode = 0;
|
---|
559 |
|
---|
560 | /* Get bitmap data address offset */
|
---|
561 | index = *(uint16_t *) (pbmp + 10);
|
---|
562 | index |= (*(uint16_t *) (pbmp + 12)) << 16;
|
---|
563 |
|
---|
564 | /* Read bitmap width */
|
---|
565 | width = *(uint16_t *) (pbmp + 18);
|
---|
566 | width |= (*(uint16_t *) (pbmp + 20)) << 16;
|
---|
567 |
|
---|
568 | /* Read bitmap height */
|
---|
569 | height = *(uint16_t *) (pbmp + 22);
|
---|
570 | height |= (*(uint16_t *) (pbmp + 24)) << 16;
|
---|
571 |
|
---|
572 | /* Read bit/pixel */
|
---|
573 | bit_pixel = *(uint16_t *) (pbmp + 28);
|
---|
574 |
|
---|
575 | /* Get the layer pixel format */
|
---|
576 | if ((bit_pixel/8) == 4){
|
---|
577 | input_color_mode = CM_ARGB8888;
|
---|
578 | }
|
---|
579 | else if ((bit_pixel/8) == 2){
|
---|
580 | input_color_mode = CM_RGB565;
|
---|
581 | }
|
---|
582 | else{
|
---|
583 | input_color_mode = CM_RGB888;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /* Bypass the bitmap header */
|
---|
587 | pbmp += (index + (width * (height - 1) * (bit_pixel/8)));
|
---|
588 | syslog_4(LOG_NOTICE, "## input_color_mode(%d) width(%d) height(%d) bit_pixel(%d) ##", input_color_mode, width, height, bit_pixel);
|
---|
589 |
|
---|
590 | lcd_setAddrWindow(hlcd, x0, y0, x0+width-1, y0+height-1);
|
---|
591 | lcd_writedata3(hlcd, pbmp, width, height);
|
---|
592 | }
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * INVERT DISPLAY
|
---|
596 | * param1 hlcd: Pointer to LCD Handler
|
---|
597 | * param2 i: invert value
|
---|
598 | */
|
---|
599 | void
|
---|
600 | lcd_invertDisplay(LCD_Handler_t *hlcd, bool_t i)
|
---|
601 | {
|
---|
602 | SVC_PERROR(lcd_writecommand(hlcd, i ? ST7735_INVON : ST7735_INVOFF));
|
---|
603 | }
|
---|
604 |
|
---|
605 |
|
---|
606 | /*
|
---|
607 | * スクリーンフィル
|
---|
608 | * param1 pDrawProp: Pointer to Draw Prop
|
---|
609 | */
|
---|
610 | void
|
---|
611 | lcd_fillScreen(LCD_DrawProp_t *pDrawProp)
|
---|
612 | {
|
---|
613 | lcd_fillRect(pDrawProp->hlcd, 0, 0, pDrawProp->hlcd->_width, pDrawProp->hlcd->_height, pDrawProp->BackColor);
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * RECTANGLE描画
|
---|
618 | * param1 pDrawProp: Pointer to Draw Prop
|
---|
619 | * param2 x: left X position
|
---|
620 | * param3 y: top Y position
|
---|
621 | * param4 w: width
|
---|
622 | * param5 h: height
|
---|
623 | */
|
---|
624 | void
|
---|
625 | lcd_drawRect(LCD_DrawProp_t *pDrawProp, int16_t x, int16_t y, int16_t w, int16_t h)
|
---|
626 | {
|
---|
627 | LCD_Handler_t *hlcd = pDrawProp->hlcd;
|
---|
628 | uint16_t color;
|
---|
629 |
|
---|
630 | // rudimentary clipping (drawChar w/big text requires this)
|
---|
631 | if((x >= hlcd->_width) || (y >= hlcd->_height)) return;
|
---|
632 | if((x + w - 1) >= hlcd->_width) w = hlcd->_width - x;
|
---|
633 | if((y + h - 1) >= hlcd->_height) h = hlcd->_height - y;
|
---|
634 |
|
---|
635 | color = pDrawProp->TextColor;
|
---|
636 | lcd_drawFastVLine(hlcd, x, y, h, color);
|
---|
637 | lcd_drawFastHLine(hlcd, x, y+h-1, w, color);
|
---|
638 | lcd_drawFastVLine(hlcd, x+w-1, y, h, color);
|
---|
639 | lcd_drawFastHLine(hlcd, x, y, w, color);
|
---|
640 | }
|
---|
641 |
|
---|
642 | /*
|
---|
643 | * 円描画
|
---|
644 | * param1 pDrawProp: Pointer to Draw Prop
|
---|
645 | * param2 x0: X position
|
---|
646 | * param3 y0: Y position
|
---|
647 | * param4 Radius: Circle radius
|
---|
648 | */
|
---|
649 | void
|
---|
650 | lcd_DrawCircle(LCD_DrawProp_t *pDrawProp, uint16_t x0, uint16_t y0, uint16_t Radius)
|
---|
651 | {
|
---|
652 | LCD_Handler_t *hlcd = pDrawProp->hlcd;
|
---|
653 | int32_t decision; /* Decision Variable */
|
---|
654 | uint32_t current_x; /* Current X Value */
|
---|
655 | uint32_t current_y; /* Current Y Value */
|
---|
656 |
|
---|
657 | decision = 3 - (Radius << 1);
|
---|
658 | current_x = 0;
|
---|
659 | current_y = Radius;
|
---|
660 |
|
---|
661 | while(current_x <= current_y){
|
---|
662 | lcd_drawPixel(hlcd, (x0 + current_x), (y0 - current_y), pDrawProp->TextColor);
|
---|
663 | lcd_drawPixel(hlcd, (x0 - current_x), (y0 - current_y), pDrawProp->TextColor);
|
---|
664 | lcd_drawPixel(hlcd, (x0 + current_y), (y0 - current_x), pDrawProp->TextColor);
|
---|
665 | lcd_drawPixel(hlcd, (x0 - current_y), (y0 - current_x), pDrawProp->TextColor);
|
---|
666 | lcd_drawPixel(hlcd, (x0 + current_x), (y0 + current_y), pDrawProp->TextColor);
|
---|
667 | lcd_drawPixel(hlcd, (x0 - current_x), (y0 + current_y), pDrawProp->TextColor);
|
---|
668 | lcd_drawPixel(hlcd, (x0 + current_y), (y0 + current_x), pDrawProp->TextColor);
|
---|
669 | lcd_drawPixel(hlcd, (x0 - current_y), (y0 + current_x), pDrawProp->TextColor);
|
---|
670 |
|
---|
671 | if (decision < 0){
|
---|
672 | decision += (current_x << 2) + 6;
|
---|
673 | }
|
---|
674 | else{
|
---|
675 | decision += ((current_x - current_y) << 2) + 10;
|
---|
676 | current_y--;
|
---|
677 | }
|
---|
678 | current_x++;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | /*
|
---|
683 | * 線描画
|
---|
684 | * param1 pDrawProp: Pointer to Draw Prop
|
---|
685 | * param2 x1: Point 1 X position
|
---|
686 | * param3 y1: Point 1 Y position
|
---|
687 | * param4 x2: Point 2 X position
|
---|
688 | * param5 y2: Point 2 Y position
|
---|
689 | */
|
---|
690 | void
|
---|
691 | lcd_drawLine(LCD_DrawProp_t *pDrawProp, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
---|
692 | {
|
---|
693 | LCD_Handler_t *hlcd = pDrawProp->hlcd;
|
---|
694 | int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
|
---|
695 | yinc1 = 0, yinc2 = 0, den = 0, num = 0, num_add = 0, num_pixels = 0,
|
---|
696 | curpixel = 0;
|
---|
697 |
|
---|
698 | deltax = ABS(x2 - x1); /* The difference between the x's */
|
---|
699 | deltay = ABS(y2 - y1); /* The difference between the y's */
|
---|
700 | x = x1; /* Start x off at the first pixel */
|
---|
701 | y = y1; /* Start y off at the first pixel */
|
---|
702 |
|
---|
703 | if(x2 >= x1){ /* The x-values are increasing */
|
---|
704 | xinc1 = 1;
|
---|
705 | xinc2 = 1;
|
---|
706 | }
|
---|
707 | else{ /* The x-values are decreasing */
|
---|
708 | xinc1 = -1;
|
---|
709 | xinc2 = -1;
|
---|
710 | }
|
---|
711 |
|
---|
712 | if(y2 >= y1){ /* The y-values are increasing */
|
---|
713 | yinc1 = 1;
|
---|
714 | yinc2 = 1;
|
---|
715 | }
|
---|
716 | else{ /* The y-values are decreasing */
|
---|
717 | yinc1 = -1;
|
---|
718 | yinc2 = -1;
|
---|
719 | }
|
---|
720 |
|
---|
721 | if(deltax >= deltay){ /* There is at least one x-value for every y-value */
|
---|
722 | xinc1 = 0; /* Don't change the x when numerator >= denominator */
|
---|
723 | yinc2 = 0; /* Don't change the y for every iteration */
|
---|
724 | den = deltax;
|
---|
725 | num = deltax / 2;
|
---|
726 | num_add = deltay;
|
---|
727 | num_pixels = deltax; /* There are more x-values than y-values */
|
---|
728 | }
|
---|
729 | else{ /* There is at least one y-value for every x-value */
|
---|
730 | xinc2 = 0; /* Don't change the x for every iteration */
|
---|
731 | yinc1 = 0; /* Don't change the y when numerator >= denominator */
|
---|
732 | den = deltay;
|
---|
733 | num = deltay / 2;
|
---|
734 | num_add = deltax;
|
---|
735 | num_pixels = deltay; /* There are more y-values than x-values */
|
---|
736 | }
|
---|
737 |
|
---|
738 | for (curpixel = 0; curpixel <= num_pixels; curpixel++){
|
---|
739 | lcd_drawPixel(hlcd, x, y, pDrawProp->TextColor); /* Draw the current pixel */
|
---|
740 | num += num_add; /* Increase the numerator by the top of the fraction */
|
---|
741 | if(num >= den){ /* Check if numerator >= denominator */
|
---|
742 | num -= den; /* Calculate the new numerator value */
|
---|
743 | x += xinc1; /* Change the x as appropriate */
|
---|
744 | y += yinc1; /* Change the y as appropriate */
|
---|
745 | }
|
---|
746 | x += xinc2; /* Change the x as appropriate */
|
---|
747 | y += yinc2; /* Change the y as appropriate */
|
---|
748 | }
|
---|
749 | }
|
---|
750 |
|
---|
751 | /*
|
---|
752 | * PLOY-LINE描画
|
---|
753 | * param1 pDrawProp: Pointer to Draw Prop
|
---|
754 | * param2 Points: Pointer to the points array
|
---|
755 | * param3 PointCount: Number of points
|
---|
756 | */
|
---|
757 | void
|
---|
758 | lcd_drawPolygon(LCD_DrawProp_t *pDrawProp, pPoint Points, uint16_t PointCount)
|
---|
759 | {
|
---|
760 | int16_t x = 0, y = 0;
|
---|
761 |
|
---|
762 | if(PointCount < 2){
|
---|
763 | return;
|
---|
764 | }
|
---|
765 |
|
---|
766 | lcd_drawLine(pDrawProp, Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
|
---|
767 | while(--PointCount){
|
---|
768 | x = Points->X;
|
---|
769 | y = Points->Y;
|
---|
770 | Points++;
|
---|
771 | lcd_drawLine(pDrawProp, x, y, Points->X, Points->Y);
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|