1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Software License Agreement (BSD License)
10 Copyright (c) 2011, microBuilder SARL
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.
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.
35 /**************************************************************************/
38 #include "projectconfig.h"
41 #include "core/gpio/gpio.h"
42 #include "core/adc/adc.h"
43 #include "core/systick/systick.h"
45 #include "drivers/lcd/tft/lcd.h"
46 #include "drivers/lcd/tft/drawing.h"
47 #include "drivers/lcd/tft/touchscreen.h"
48 #include "drivers/lcd/tft/fonts/dejavusans9.h"
49 #include "drivers/lcd/tft/fonts/dejavusansbold9.h"
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 };
54 bool adcEnabled
= true;
55 bool digEnabled
= false;
57 /**************************************************************************/
59 Renders the frame around the data grid
61 /**************************************************************************/
62 void renderLCDFrame(void)
65 drawFill(COLOR_DARKGRAY
);
67 // Render V references
68 drawString(245, 27, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "3.5V");
69 drawString(244, 26, COLOR_WHITE
, &dejaVuSansBold9ptFontInfo
, "3.5V");
70 drawString(245, 195, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "0.0V");
71 drawString(244, 194, COLOR_WHITE
, &dejaVuSansBold9ptFontInfo
, "0.0V");
74 drawString( 10, 10, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "~100ms/Div");
75 drawString( 9, 9, COLOR_WHITE
, &dejaVuSansBold9ptFontInfo
, "~100ms/Div");
76 drawString( 95, 10, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "500mV/Div");
77 drawString( 94, 9, COLOR_WHITE
, &dejaVuSansBold9ptFontInfo
, "500mV/Div");
79 // Clear the ADC output level just in case
80 drawRectangleFilled(175, 5, 250, 18, COLOR_DARKGRAY
);
82 // Render the channel text
83 drawString( 25, 220, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "P1.4 (Analog)");
84 drawString( 24, 219, adcEnabled
? COLOR_YELLOW
: COLOR_MEDIUMGRAY
, &dejaVuSansBold9ptFontInfo
, "P1.4 (Analog)");
85 drawString(135, 220, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "P2.0 (Digital)");
86 drawString(134, 219, digEnabled
? COLOR_GREEN
: COLOR_MEDIUMGRAY
, &dejaVuSansBold9ptFontInfo
, "P2.0 (Digital)");
89 drawString(245, 80, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, "Warning:");
90 drawString(244, 79, COLOR_WHITE
, &dejaVuSansBold9ptFontInfo
, "Warning:");
91 drawString(244, 95, COLOR_WHITE
, &dejaVuSans9ptFontInfo
, "ADC input");
92 drawString(244, 110, COLOR_WHITE
, &dejaVuSans9ptFontInfo
, "is not 5.0V");
93 drawString(244, 125, COLOR_WHITE
, &dejaVuSans9ptFontInfo
, "tolerant!");
96 /**************************************************************************/
98 Converts the supplied 8-bit value to an approximate pixel height in
101 /**************************************************************************/
102 uint16_t adcValToPixel(uint8_t value
)
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
110 pixel
= 200 - (value
* 1000 / 1545);
114 /**************************************************************************/
116 Redraws the grid and renders the data points on the LCD
118 /**************************************************************************/
119 void renderLCDGrid(void)
121 if ((!adcEnabled
) && (!digEnabled
))
127 drawRectangle(9, 24, 236, 201, COLOR_LIGHTGRAY
);
128 drawRectangleFilled(10, 25, 235, 200, COLOR_BLACK
);
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
);
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
);
148 // Render the individual data points
150 for (counter
= 0; counter
< 9; counter
++)
152 // Draw historical data
153 uint32_t arrayPosition
= 9 - counter
;
155 // Draw analog data points (Yellow)
158 drawLine(10 + counter
* 25, adcValToPixel(adcBuffer
[arrayPosition
]), 10 + (counter
+ 1) * 25, adcValToPixel(adcBuffer
[arrayPosition
- 1]), COLOR_YELLOW
);
161 // Draw digital data points (Green)
164 drawLine(10 + counter
* 25, adcValToPixel(digBuffer
[arrayPosition
]), 10 + (counter
+ 1) * 25, adcValToPixel(digBuffer
[arrayPosition
- 1]), COLOR_GREEN
);
168 // Render ADC value in text if present
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 drawString(180, 10, COLOR_BLACK
, &dejaVuSansBold9ptFontInfo
, text
);
178 drawString(179, 9, COLOR_YELLOW
, &dejaVuSansBold9ptFontInfo
, text
);
182 /**************************************************************************/
184 Shifts the buffer contents right one byte, and inserts the latest
185 value at the beginning.
187 /**************************************************************************/
188 void addToBuffer(uint8_t *buffer
, uint8_t value
)
191 for (counter
= 9; counter
> 0; counter
--)
193 buffer
[counter
] = buffer
[counter
- 1];
196 // Append the latest value
200 /**************************************************************************/
202 Main program entry point. After reset, normal code execution will
205 /**************************************************************************/
208 #if !defined CFG_TFTLCD
209 #error "CFG_TFTLCD must be enabled in projectconfig.h for this test"
211 #if defined CFG_INTERFACE
212 #error "CFG_INTERFACE must be disabled in projectconfig.h for this test (to save space)"
215 // Configure cpu and mandatory peripherals
218 /* Set P2.0 to GPIO input (just in case) */
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
);
228 // Rotate the screen and render the area around the data grid
229 lcdSetOrientation(LCD_ORIENTATION_LANDSCAPE
);
237 // Wait up to 5ms for a touch event
238 tsTouchError_t error
= tsWaitForEvent(&touch
, 5);
241 if (touch
.x
> 25 && touch
.x
< 100 && touch
.y
> 210)
243 // Analog switch selected
244 adcEnabled
= adcEnabled
? false : true;
246 if (touch
.x
> 125 && touch
.x
< 200 && touch
.y
> 210)
248 // Digital switch selected
249 digEnabled
= digEnabled
? false : true;
257 addToBuffer(adcBuffer
, adcRead(5) / 4); // 10-bit value converted to 8-bits
259 addToBuffer(digBuffer
, gpioGetValue(2, 0) ? 0xFF : 0x00);
261 // Update the LCD is required
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