API changes for v1.0.0
[hackover2013-badge-firmware.git] / tools / examples / lcd / tft / oscilloscope / 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 <stdio.h>
37
38 #include "projectconfig.h"
39 #include "sysinit.h"
40
41 #include "core/gpio/gpio.h"
42 #include "core/adc/adc.h"
43 #include "core/systick/systick.h"
44
45 #include "drivers/displays/tft/lcd.h"
46 #include "drivers/displays/tft/drawing.h"
47 #include "drivers/displays/tft/touchscreen.h"
48 #include "drivers/displays/tft/fonts/dejavusans9.h"
49 #include "drivers/displays/tft/fonts/dejavusansbold9.h"
50
51 static uint8_t adcBuffer[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
52 static uint8_t digBuffer[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
53
54 bool adcEnabled = true;
55 bool digEnabled = false;
56
57 /**************************************************************************/
58 /*!
59 Renders the frame around the data grid
60 */
61 /**************************************************************************/
62 void renderLCDFrame(void)
63 {
64 // Clear the screen
65 drawFill(COLOR_DARKGRAY);
66
67 // Render V references
68 fontsDrawString(245, 27, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "3.5V");
69 fontsDrawString(244, 26, COLOR_WHITE, &dejaVuSansBold9ptFontInfo, "3.5V");
70 fontsDrawString(245, 195, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "0.0V");
71 fontsDrawString(244, 194, COLOR_WHITE, &dejaVuSansBold9ptFontInfo, "0.0V");
72
73 // Div settings
74 fontsDrawString( 10, 10, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "~100ms/Div");
75 fontsDrawString( 9, 9, COLOR_WHITE, &dejaVuSansBold9ptFontInfo, "~100ms/Div");
76 fontsDrawString( 95, 10, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "500mV/Div");
77 fontsDrawString( 94, 9, COLOR_WHITE, &dejaVuSansBold9ptFontInfo, "500mV/Div");
78
79 // Clear the ADC output level just in case
80 drawRectangleFilled(175, 5, 250, 18, COLOR_DARKGRAY);
81
82 // Render the channel text
83 fontsDrawString( 25, 220, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "P1.4 (Analog)");
84 fontsDrawString( 24, 219, adcEnabled ? COLOR_YELLOW : COLOR_MEDIUMGRAY, &dejaVuSansBold9ptFontInfo, "P1.4 (Analog)");
85 fontsDrawString(135, 220, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "P2.0 (Digital)");
86 fontsDrawString(134, 219, digEnabled ? COLOR_GREEN : COLOR_MEDIUMGRAY, &dejaVuSansBold9ptFontInfo, "P2.0 (Digital)");
87
88 // ADC Warning
89 fontsDrawString(245, 80, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, "Warning:");
90 fontsDrawString(244, 79, COLOR_WHITE, &dejaVuSansBold9ptFontInfo, "Warning:");
91 fontsDrawString(244, 95, COLOR_WHITE, &dejaVuSans9ptFontInfo, "ADC input");
92 fontsDrawString(244, 110, COLOR_WHITE, &dejaVuSans9ptFontInfo, "is not 5.0V");
93 fontsDrawString(244, 125, COLOR_WHITE, &dejaVuSans9ptFontInfo, "tolerant!");
94 }
95
96 /**************************************************************************/
97 /*!
98 Converts the supplied 8-bit value to an approximate pixel height in
99 the data grid
100 */
101 /**************************************************************************/
102 uint16_t adcValToPixel(uint8_t value)
103 {
104 // Since the chart is 175 pixels high and 3.3V is at
105 // approximately 165 pixels height, we need to
106 // divide the 8 bit ADC readings by 1.545 (255/165)
107 // using fixed point math, and then substract
108 // the number from the bottom of the frame
109 uint16_t pixel = 0;
110 pixel = 200 - (value * 1000 / 1545);
111 return pixel;
112 }
113
114 /**************************************************************************/
115 /*!
116 Redraws the grid and renders the data points on the LCD
117 */
118 /**************************************************************************/
119 void renderLCDGrid(void)
120 {
121 if ((!adcEnabled) && (!digEnabled))
122 {
123 return;
124 }
125
126 // Redraw the grid
127 drawRectangle(9, 24, 236, 201, COLOR_LIGHTGRAY);
128 drawRectangleFilled(10, 25, 235, 200, COLOR_BLACK);
129
130 // Horizontal lines
131 drawLine(10, 50, 235, 50, COLOR_DARKERGRAY);
132 drawLine(10, 75, 235, 75, COLOR_DARKERGRAY);
133 drawLine(10, 100, 235, 100, COLOR_DARKERGRAY);
134 drawLine(10, 125, 235, 125, COLOR_DARKERGRAY);
135 drawLine(10, 150, 235, 150, COLOR_DARKERGRAY);
136 drawLine(10, 175, 235, 175, COLOR_DARKERGRAY);
137
138 // Vertical lines
139 drawLine( 35, 25, 35, 200, COLOR_DARKERGRAY);
140 drawLine( 60, 25, 60, 200, COLOR_DARKERGRAY);
141 drawLine( 85, 25, 85, 200, COLOR_DARKERGRAY);
142 drawLine(110, 25, 110, 200, COLOR_DARKERGRAY);
143 drawLine(135, 25, 135, 200, COLOR_DARKERGRAY);
144 drawLine(160, 25, 160, 200, COLOR_DARKERGRAY);
145 drawLine(185, 25, 185, 200, COLOR_DARKERGRAY);
146 drawLine(210, 25, 210, 200, COLOR_DARKERGRAY);
147
148 // Render the individual data points
149 uint32_t counter;
150 for (counter = 0; counter < 9; counter++)
151 {
152 // Draw historical data
153 uint32_t arrayPosition = 9 - counter;
154
155 // Draw analog data points (Yellow)
156 if (adcEnabled)
157 {
158 drawLine(10 + counter * 25, adcValToPixel(adcBuffer[arrayPosition]), 10 + (counter + 1) * 25, adcValToPixel(adcBuffer[arrayPosition - 1]), COLOR_YELLOW);
159 }
160
161 // Draw digital data points (Green)
162 if (digEnabled)
163 {
164 drawLine(10 + counter * 25, adcValToPixel(digBuffer[arrayPosition]), 10 + (counter + 1) * 25, adcValToPixel(digBuffer[arrayPosition - 1]), COLOR_GREEN);
165 }
166 }
167
168 // Render ADC value in text if present
169 if (adcEnabled)
170 {
171 char text[10];
172 // Assuming 3.3V supply and 8-bit ADC values, 1 unit = 12.89mV (3300/256)
173 sprintf(text, "%u.%u V", ((adcBuffer[0] * 1289) / 100) / 1000, ((adcBuffer[0] * 1294) / 100) % 1000);
174 // Clear the previous text
175 drawRectangleFilled(175, 5, 250, 18, COLOR_DARKGRAY);
176 // Render the latest value in mV
177 fontsDrawString(180, 10, COLOR_BLACK, &dejaVuSansBold9ptFontInfo, text);
178 fontsDrawString(179, 9, COLOR_YELLOW, &dejaVuSansBold9ptFontInfo, text);
179 }
180 }
181
182 /**************************************************************************/
183 /*!
184 Shifts the buffer contents right one byte, and inserts the latest
185 value at the beginning.
186 */
187 /**************************************************************************/
188 void addToBuffer(uint8_t *buffer, uint8_t value)
189 {
190 uint32_t counter;
191 for (counter = 9; counter > 0; counter--)
192 {
193 buffer[counter] = buffer[counter - 1];
194 }
195
196 // Append the latest value
197 buffer[0] = value;
198 }
199
200 /**************************************************************************/
201 /*!
202 Main program entry point. After reset, normal code execution will
203 begin here.
204 */
205 /**************************************************************************/
206 int main(void)
207 {
208 #if !defined CFG_TFTLCD
209 #error "CFG_TFTLCD must be enabled in projectconfig.h for this test"
210 #endif
211 #if defined CFG_INTERFACE
212 #error "CFG_INTERFACE must be disabled in projectconfig.h for this test (to save space)"
213 #endif
214
215 // Configure cpu and mandatory peripherals
216 systemInit();
217
218 /* Set P2.0 to GPIO input (just in case) */
219 gpioSetDir(2, 0, 0);
220
221 /* Set P1.4/AD5 to analog input (only AD0..3 are configured by adcInit) */
222 IOCON_PIO1_4 &= ~(IOCON_PIO1_4_ADMODE_MASK |
223 IOCON_PIO1_4_FUNC_MASK |
224 IOCON_PIO1_4_MODE_MASK);
225 IOCON_PIO1_4 |= (IOCON_PIO1_4_FUNC_AD5 &
226 IOCON_PIO1_4_ADMODE_ANALOG);
227
228 // Rotate the screen and render the area around the data grid
229 lcdSetOrientation(LCD_ORIENTATION_LANDSCAPE);
230 renderLCDFrame();
231
232 tsTouchData_t touch;
233
234 // Start reading
235 while (1)
236 {
237 // Wait up to 5ms for a touch event
238 tsTouchError_t error = tsWaitForEvent(&touch, 5);
239 if (!error)
240 {
241 if (touch.x > 25 && touch.x < 100 && touch.y > 210)
242 {
243 // Analog switch selected
244 adcEnabled = adcEnabled ? false : true;
245 }
246 if (touch.x > 125 && touch.x < 200 && touch.y > 210)
247 {
248 // Digital switch selected
249 digEnabled = digEnabled ? false : true;
250 }
251 // Refresh the frame
252 renderLCDFrame();
253 }
254
255 // Read pins
256 if (adcEnabled)
257 addToBuffer(adcBuffer, adcRead(5) / 4); // 10-bit value converted to 8-bits
258 if (digEnabled)
259 addToBuffer(digBuffer, gpioGetValue(2, 0) ? 0xFF : 0x00);
260
261 // Update the LCD is required
262 renderLCDGrid();
263
264 // Note, this will actually mean the timing is ~100mS + the amount of
265 // time it took to get the readings and update the LCD
266 // A timer interrupt could be used to get much more accurate results,
267 // filling the buffer inside the IRQ and rendering the screen updates
268 // every x milliseconds
269 systickDelay(100);
270 }
271
272 return 0;
273 }
This page took 0.084425 seconds and 5 git commands to generate.