First commit
[hackover2013-badge-firmware.git] / tools / examples / lcd / tft / drawing_basic / main.c
1 /**************************************************************************/
2 /*!
3 @file main.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2011, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "projectconfig.h"
41 #include "sysinit.h"
42
43 #include "core/gpio/gpio.h"
44 #include "core/systick/systick.h"
45
46 #include "drivers/displays/tft/lcd.h"
47 #include "drivers/displays/tft/drawing.h"
48 #include "drivers/displays/tft/touchscreen.h"
49 #include "drivers/displays/tft/dialogues/alphanumeric.h"
50
51 #include "drivers/displays/tft/fonts/dejavusans9.h"
52 #include "drivers/displays/tft/fonts/dejavusansbold9.h"
53 #include "drivers/displays/tft/fonts/dejavusansmono8.h"
54
55 /**************************************************************************/
56 /*!
57 Main program entry point. After reset, normal code execution will
58 begin here.
59 */
60 /**************************************************************************/
61 int main(void)
62 {
63 // Configure cpu and mandatory peripherals
64 systemInit();
65
66 #if !defined CFG_TFTLCD
67 #error "CFG_TFTLCD must be enabled in projectconfig.h for this test"
68 #endif
69 #if defined CFG_INTERFACE
70 #error "CFG_INTERFACE must be disabled in projectconfig.h for this test (to save space)"
71 #endif
72
73 // Clear the screen
74 // ---------------------------------------------------------------------
75 drawFill(COLOR_WHITE);
76
77 // Render some text using DejaVu Sans 9 and Sans Mono 8
78 // ---------------------------------------------------------------------
79 fontsDrawString(5, 10, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "DejaVu Sans 9 Bold");
80 fontsDrawString(5, 30, COLOR_BLACK, &dejaVuSans9ptFontInfo, "DejaVu Sans 9");
81 fontsDrawString(5, 50, COLOR_BLACK, &dejaVuSansMono8ptFontInfo, "DejaVu Sans Mono 8");
82
83 // Change the LCD orientation to render text horizontally
84 // ---------------------------------------------------------------------
85 lcdProperties_t lcdProperties = lcdGetProperties();
86 // Check if the screen orientation can be changed
87 if (lcdProperties.orientation)
88 {
89 // Change the orientation
90 lcdSetOrientation(lcdGetOrientation() == LCD_ORIENTATION_PORTRAIT ?
91 LCD_ORIENTATION_LANDSCAPE : LCD_ORIENTATION_PORTRAIT);
92 // Render some text in the new orientation
93 fontsDrawString(5, 10, COLOR_BLACK, &dejaVuSans9ptFontInfo, "DejaVu Sans 9 (Rotated)");
94 // Change the orientation back
95 lcdSetOrientation(lcdGetOrientation() == LCD_ORIENTATION_PORTRAIT ?
96 LCD_ORIENTATION_LANDSCAPE : LCD_ORIENTATION_PORTRAIT);
97 }
98
99 // Draw some primitive shapes
100 // ---------------------------------------------------------------------
101 drawLine(5, 65, 200, 65, COLOR_RED);
102 drawLine(5, 67, 200, 67, COLOR_GREEN);
103 drawLine(5, 69, 200, 69, COLOR_BLUE);
104 drawCircleFilled(30, 105, 25, COLOR_BLACK);
105 drawCircleFilled(30, 105, 23, drawRGB24toRGB565(0x33, 0x00, 0x00));
106 drawCircleFilled(30, 105, 19, drawRGB24toRGB565(0x66, 0x00, 0x00));
107 drawCircleFilled(30, 105, 15, drawRGB24toRGB565(0x99, 0x00, 0x00));
108 drawCircleFilled(30, 105, 11, drawRGB24toRGB565(0xCC, 0x00, 0x00));
109 drawCircleFilled(30, 105, 7, drawRGB24toRGB565(0xFF, 0x00, 0x00));
110 drawRectangleFilled( 80, 80, 180, 125, COLOR_DARKERGRAY);
111 drawRectangleFilled( 85, 85, 175, 120, COLOR_DARKGRAY);
112 drawRectangleFilled( 90, 90, 170, 115, COLOR_MEDIUMGRAY);
113 drawRectangleFilled( 95, 95, 165, 110, COLOR_LIGHTGRAY);
114 drawRectangleFilled(100, 100, 160, 105, COLOR_PALEGRAY);
115
116 // Draw some compound shapes
117 // ---------------------------------------------------------------------
118 drawProgressBar(70, 140, 75, 12, COLOR_BLACK, COLOR_MEDIUMGRAY, 78);
119 fontsDrawString(5, 144, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "Progress");
120 fontsDrawString(155, 144, COLOR_BLACK, &dejaVuSans9ptFontInfo, "78%");
121 drawRectangleFilled(0, 175, 239, 210, COLOR_DARKERGRAY);
122 drawButton(20, 180, 200, 25, &dejaVuSans9ptFontInfo, "Click For Text Entry", false);
123
124 // Wait for a valid touch event
125 // ---------------------------------------------------------------------
126 tsTouchData_t data;
127 tsTouchError_t error = -1;
128 bool success = false;
129 while(!success)
130 {
131 // Wait forever for a valid touch event to occur (ignoring faulty readings)
132 while (error)
133 {
134 // Only exit this loop when '0' is returned
135 error = tsWaitForEvent(&data, 0);
136 // printf("Error: %d X: %u Y: %u \r\n", error, data.x, data.y);
137 }
138
139 // Reset error to an error state in case we got a valid reading, but it's not
140 // within the expected range
141 error = -1;
142
143 // Check if the captured touch event is within the specified X/Y range
144 if (data.x > 20 && data.x < 220 && data.y > 180 && data.y < 205)
145 {
146 // Wait a few milliseconds then display the text input dialogue
147 systickDelay(100);
148 char* results = alphaShowDialogue();
149 // At this point, results contains the text from the dialogue ...
150 // clear the screen and show the results
151 drawFill(COLOR_WHITE);
152 fontsDrawString(10, 10, COLOR_BLACK, &dejaVuSans9ptFontInfo, "You Entered:");
153 fontsDrawString(10, 30, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, results);
154 fontsDrawString(10, 155, COLOR_BLACK, &dejaVuSans9ptFontInfo, "Thanks ... starting blinky!");
155 // Setting success to true allow the code to move in to blinky
156 success = true;
157 }
158 }
159
160 uint32_t currentSecond, lastSecond;
161 currentSecond = lastSecond = 0;
162
163 while (1)
164 {
165 // Toggle LED once per second ... rollover = 136 years :)
166 currentSecond = systickGetSecondsActive();
167 if (currentSecond != lastSecond)
168 {
169 lastSecond = currentSecond;
170 if (gpioGetValue(CFG_LED_PORT, CFG_LED_PIN) == CFG_LED_OFF)
171 {
172 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);
173 }
174 else
175 {
176 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
177 }
178 }
179 }
180
181 return 0;
182 }
This page took 0.06008 seconds and 5 git commands to generate.