A simple meter using Adafruit 2.8 TFT with touch v2

Dependencies:   SPI_STMPE610 UniGraphic mbed vt100

/media/uploads/Rhyme/tester_s.jpg

A dumb simple voltage tester using ADC.
With FRDM-KL25Z, you can measure about 0V to 3.28V.
Nothing to drop your jaw nor holding your breath
but I wanted one, so I wrote one ;-)

とても単純はADCを使用した電圧テスターです。
FRDM-KL25Z を使用した場合、0V ~ 3.28V くらいが測定範囲です。
特に目新しいことも、驚くこともありませんが、
自分が欲しいので書いてみました (^ - ^)

On 18-May-2018 
I changed the number display from 0.00 to 0.000
so that I can measure the change of a small loadcell.

2018年5月18日 
ロードセル (重量センサ)の出力値の変化を見たかったので
有効数字を小数点以下2桁から3桁に変更しました。

Files at this revision

API Documentation at this revision

Comitter:
Rhyme
Date:
Thu Jul 20 02:37:13 2017 +0000
Child:
1:d7f2aa328962
Commit message:
first working version

Changed in this revision

SPI_STMPE610.lib Show annotated file Show diff for this revision Revisions of this file
UniGraphic.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
meter.cpp Show annotated file Show diff for this revision Revisions of this file
meter.h Show annotated file Show diff for this revision Revisions of this file
vt100.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI_STMPE610.lib	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/Rhyme/code/SPI_STMPE610/#6740dfc84a8c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UniGraphic.lib	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/GraphicsDisplay/code/UniGraphic/#f87f06292637
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,72 @@
+#include "mbed.h"
+#include "ILI9341.h"
+#include "Arial12x12.h"
+#include "Arial24x23.h"
+#include "Arial28x28.h"
+#include "Arial43x48_numb.h"
+#include "SPI_STMPE610.h"
+#include "vt100.h"
+#include "meter.h"
+#include "main.h"
+
+vt100        *tty = 0 ;
+ILI9341      *tft = 0 ;
+SPI_STMPE610 *tsc = 0 ;
+meter        *tacho = 0 ;
+
+DigitalOut myled(LED1, 1);
+DigitalOut *backlight = 0 ;
+AnalogIn   *vin = 0 ;
+
+float min_value = 0.0 ;
+float max_value = 3.3 ;
+
+void initTFT(void)
+{
+    //Configure the display driver
+    tft->BusEnable(true) ;
+    tft->FastWindow(true) ;
+    tft->background(Black);
+    tft->foreground(White);
+    wait(0.01) ;
+    tft->cls();
+    tft->BusEnable(false) ;
+    backlight = new DigitalOut(PIN_BL_TFT, 1) ;
+}
+
+void init_hardware(void)
+{
+    tty = new vt100() ;
+    tty->cls() ;
+    tft = new ILI9341(SPI_8, 10000000, 
+    PIN_MOSI, PIN_MISO,  PIN_SCLK, 
+    PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
+    initTFT() ;
+    tft->set_font((unsigned char*) Arial28x28);
+    tft->foreground(White) ;
+//    tsc = new SPI_STMPE610(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
+    tacho = new meter(5, 5, 230, 230, 0.0, 3.3) ;
+    vin = new AnalogIn(PIN_ADC_CH0) ;
+}
+
+int main() {
+    float value = 0.0 ;
+    float prev_value = 0.0 ;
+    int intvalue = 0 ;
+    init_hardware() ;
+    *backlight = 1 ;
+
+    tacho->drawFrame() ;
+    tacho->drawScale() ;
+    while(1) {
+        value = 3.28 * vin->read() ;
+        intvalue = (int)(100.0 * value + 0.5) ;
+        value = (double)intvalue / 100.0 ;
+        if (value != prev_value) {
+            tacho->drawHand(value) ;
+            tacho->drawValue(value) ;
+            prev_value = value ;
+        }
+        wait(0.2) ;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,167 @@
+/** mbed oscilloscope my implementation of a oscillo scope
+ * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef _MAIN_H_
+#define _MAIN_H_ included
+
+#if defined (TARGET_KL25Z) 
+#define PIN_SCLK        PTD1
+#define PIN_MISO        PTD3 
+#define PIN_MOSI        PTD2
+#define PIN_CS_TFT      PTD0 
+#define PIN_DC_TFT      PTD5 
+#define PIN_CS_TSC      PTA13
+#define PIN_BL_TFT      PTA12
+#define PIN_CS_SD       PTA4 
+#define PIN_TSC_INTR    PTC9 /* place holder */
+#define PIN_RESET_TFT   PTB10 /* place holder */
+#define PIN_ADC_CH0     PTB0
+#define PIN_ADC_CH1     PTB2
+
+#elif defined (TARGET_KL46Z)
+#define PIN_SCLK        PTD5 
+#define PIN_MISO        PTD7 
+#define PIN_MOSI        PTD6
+#define PIN_CS_TFT      PTD4 
+#define PIN_DC_TFT      PTD2 
+#define PIN_CS_TSC      PTA13
+#define PIN_BL_TFT      PTC9 
+#define PIN_CS_SD       PTA4 
+#define PIN_TSC_INTR    PTC7 /* place holder */
+#define PIN_RESET_TFT   PTC6 /* place holder */
+#define PIN_ADC_CH0     PTB0
+#define PIN_ADC_CH1     PTB2
+
+#elif defined (TARGET_K64F)
+#define PIN_SCLK        PTD1
+#define PIN_MISO        PTD3
+#define PIN_MOSI        PTD2
+#define PIN_CS_TFT      PTD0
+#define PIN_DC_TFT      PTC4
+// for board rev E or later
+#define PIN_CS_TSC      PTC12
+// for earlier boards use following line
+// #define PIN_CS_TSC      PTA0
+#define PIN_BL_TFT      PTC3
+#define PIN_CS_SD       PTB23
+#define PIN_TSC_INTR    PTC0 /* place holder */
+#define PIN_RESET_TFT   PTC9 /* place holder */
+#define PIN_ADC_CH0     PTB2
+#define PIN_ADC_CH1     PTB10
+
+#elif defined (TARGET_K22F)
+#define PIN_SCLK        PTD5
+#define PIN_MISO        PTD7
+#define PIN_MOSI        PTD6
+#define PIN_CS_TFT      PTD4
+#define PIN_DC_TFT      PTA1
+#define PIN_CS_TSC      PTB19
+#define PIN_BL_TFT      PTC6 
+#define PIN_CS_SD       PTA4 
+#define PIN_TSC_INTR    PTC7 /* place holder */
+#define PIN_RESET_TFT   PTC9 /* place holder */
+#define PIN_ADC_CH0     PTB0
+#define PIN_ADC_CH1     PTC1
+
+#elif defined (TARGET_NUCLEO_F411RE)
+#define PIN_SCLK        PA_5
+#define PIN_MISO        PA_6
+#define PIN_MOSI        PA_7
+#define PIN_CS_TFT      PB_6
+#define PIN_DC_TFT      PC_7
+#define PIN_CS_TSC      PA_9
+#define PIN_BL_TFT      PA_8
+#define PIN_CS_SD       PB_5
+#define PIN_TSC_INTR    PA_8 /* place holder */
+#define PIN_RESET_TFT   PA_13 /* place holder */
+#define PIN_ADC_CH0     PA_0
+#define PIN_ADC_CH1     PA_4
+
+#elif defined (TARGET_K20D50M)
+#define PIN_SCLK        PTD1
+#define PIN_MISO        PTD3
+#define PIN_MOSI        PTD2
+#define PIN_CS_TFT      PTC2
+#define PIN_DC_TFT      PTA2 
+#define PIN_CS_TSC      PTA12
+#define PIN_BL_TFT      PTC4 
+#define PIN_CS_SD       PTC8 
+#define PIN_TSC_INTR    PTA4 /* place holder */
+#define PIN_RESET_TFT   PTC7 /* place holder */
+#define PIN_ADC_CH0     PTC0
+#define PIN_ADC_CH1     PTD6
+
+#elif defined (TARGET_RZ_A1H)
+#define PIN_SCLK        P10_12
+#define PIN_MISO        P10_15
+#define PIN_MOSI        P10_14
+#define PIN_CS_TFT      P10_13
+#define PIN_DC_TFT      P8_14 
+#define PIN_CS_TSC      P8_15
+#define PIN_BL_TFT      P8_11
+#define PIN_CS_SD       P4_5 
+#define PIN_TSC_INTR    P2_9 /* place holder */
+#define PIN_RESET_TFT   P2_10 /* place holder */
+#define PIN_ADC_CH0     P1_8
+#define PIN_ADC_CH1     P1_10
+#elif defined (TARGET_MAX32600MBED)
+/* I2C */
+#define PIN_SCL  P2_7
+#define PIN_SDA  P2_6
+/* SPI */
+#define PIN_SCLK  P2_0
+#define PIN_MISO P2_2
+#define PIN_MOSI P2_1
+#define PIN_CS0  P2_3
+/* Interrupt */
+#define PIN_INT0 P2_4
+#define PIN_INT1 P2_5
+#define PIN_INT2 P1_7
+#define PIN_INT3 P1_6
+#define PIN_INT4 P1_5
+#define PIN_INT5 P1_4
+/* Analog In */
+#define PIN_AN0  AIN_0P
+#define PIN_AN1  AIN_1P
+#define PIN_AN2  AIN_2P
+#define PIN_AN3  AIN_3P
+#define PIN_AN4  AIN_4P
+#define PIN_AN5  AIN_5P
+#define BOARD_NAME "MAX32600MBED"
+
+#define PIN_CS_TFT      P2_3 // D10 PTD0 
+#define PIN_DC_TFT      P2_4 // D9  PTD5 
+#define PIN_CS_TSC      P2_5 // D8  PTA13
+#define PIN_BL_TFT      P1_7 // D7   PTC9 
+#define PIN_CS_SD       P1_4 // D4   PTA4 
+#define PIN_TSC_INTR    P5_4 // PTC9 /* place holder */
+#define PIN_RESET_TFT   P5_5 /// PTB10 /* place holder */
+#define PIN_ADC_CH0     AIN_0P // A0 PTB0
+#define PIN_ADC_CH1     AIN_2P // A2 PTB2
+
+#else
+ #error TARGET NOT DEFINED
+#define PIN_SCLK        D13 
+#define PIN_MISO        D12 
+#define PIN_MOSI        D11
+#define PIN_CS_TFT      D10 
+#define PIN_DC_TFT      D9 
+#define PIN_CS_TSC      D8
+#define PIN_BL_TFT      D7 
+#define PIN_CS_SD       D4 
+#define PIN_TSC_INTR    D5-inside /* place holder */
+#define PIN_RESET_TFT   D4-inside /* place holder */
+#define PIN_ADC_CH0     A0
+#define PIN_ADC_CH1     A2
+
+#endif
+
+#endif /* _MAIN_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/22da6e220af6
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/meter.cpp	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,149 @@
+#include "mbed.h"
+#include <math.h>
+#include <string.h>
+#include "ILI9341.h"
+#include "Arial12x12.h"
+#include "Arial24x23.h"
+#include "Arial28x28.h"
+#include "Arial43x48_numb.h"
+#include "SPI_STMPE610.h"
+#include "meter.h"
+#include "vt100.h"
+#include "main.h"
+
+#ifndef M_PI
+#define M_PI 3.141593
+#endif
+
+extern vt100        *tty ;
+extern ILI9341      *tft ;
+extern SPI_STMPE610 *tsc ;
+extern meter        *tacho ;
+double SIN[451] ;
+double COS[451] ;
+
+static void fillTriTable(void)
+{
+    double theta ;
+    int i ;
+    for (i = 1 ; i < 450 ; i++ ) {
+        theta = M_PI * (((double)(i + 450))/1800.0) ;
+        SIN[i] = sin( theta ) ;
+        COS[i] = cos( theta ) ;
+    }
+    SIN[0] = 0.0 ;
+    COS[0] = 1.0 ;
+    SIN[450] = 1.0 ;
+    COS[450] = 0.0 ;
+}
+
+meter::meter(int x, int y, int width, int height, float min, float max) 
+{
+    _x = x ;
+    _y = y ;
+    _w = width ;
+    _h = height ;
+    _value = min ;
+    _min = min ;
+    _max = max ;
+    _center_x = _x + _w / 2 ;
+    _center_y = _y + (2 * _h) / 3 ;
+    fillTriTable() ;
+}
+
+meter::~meter(void) 
+{
+}
+
+void meter::drawFrame(void)
+{
+    tft->BusEnable(true) ;
+    tft->fillrect(_x, _y, _x+ _w - 1, _y+(2 * _h)/3 - 1, White) ;
+    tft->fillrect(_x, _y+(2 * _h)/3 - 1, _x + _w - 1, _y + _h - 1, Black) ;
+    tft->rect(_x, _y, _x + _w - 1, _y + _h - 1, Green) ;
+    tft->BusEnable(false) ;
+}
+
+void meter::drawScale(void)
+{
+    int x1, x2, y1, y2 ;
+    double radius ;
+    double append ;
+    radius = _w / 2.0 ;
+    tft->BusEnable(true) ;
+    for (int i = 0 ; i < 450 ; i += 45 ) {
+        x1 = (radius * COS[i] + 0.5) ;
+        y1 = (radius * SIN[i] + 0.5) ;
+        if ((i % 10) == 5) {
+            append = 5 ;
+        } else {
+            append = 10 ;
+        }
+        x2 = ((radius + append) * COS[i] + 0.5) ;
+        y2 = ((radius + append) * SIN[i] + 0.5) ;
+        tft->line(_center_x + x1, _center_y - y1, _center_x + x2, _center_y - y2, Black) ;
+        tft->line(_center_x - x1, _center_y - y1, _center_x - x2, _center_y - y2, Black) ;
+    }
+    tft->line(_center_x, _center_y - radius, _center_x, _center_y - (radius + 10), Black) ;
+    tft->BusEnable(false) ;
+}
+
+void meter::drawHand(float value)
+{
+    static int prev_x1 = 0, prev_x2 = 0, prev_y1 = 0, prev_y2 = 0 ;
+    int x1, x2, y1, y2 ;
+    double theta ;
+    double radius = _w / 20.0 ;
+    double append = 8.0 * (_w / 20.0)  ;
+    int i ;
+    
+    theta = 90.0 - (90.0 * (value / _max)) ;
+    if (theta > 45.0) {
+        i = (10.0 * (90.0 - theta + 45.0) + 0.5) ; 
+        x1 = _center_x -(radius * COS[i-450] + 0.5) ;
+        x2 = _center_x -((radius + append) * COS[i-450] + 0.5) ;
+    } else {
+        i = (10.0 * (theta + 45.0) + 0.5) ;
+        x1 = _center_x + (radius * COS[i-450] + 0.5) ;
+        x2 = _center_x + ((radius + append) * COS[i-450] + 0.5) ;
+    }
+    y1 = _center_y - (radius * SIN[i-450] + 0.5) ;
+    y2 = _center_y - ((radius + append) * SIN[i-450] + 0.5) ;
+    tft->BusEnable(true) ;
+    tft->line(prev_x1, prev_y1, prev_x2, prev_y2, White) ;
+    tft->line(x1, y1, x2, y2, Black) ;
+    tft->BusEnable(false) ;
+    prev_x1 = x1 ;
+    prev_x2 = x2 ;
+    prev_y1 = y1 ;
+    prev_y2 = y2 ;
+}
+
+void meter::draw(void) 
+{
+}
+
+void meter::draw(float value) 
+{
+    tft->BusEnable(true) ;
+    drawHand(_value) ;
+    tft->BusEnable(false) ;
+}
+
+void meter::drawValue(float value)
+{
+    char str[32] ;
+    int v1, v2 ;
+    v1 = (int)value ;
+    v2 = ((int)(100.0 * value) % 100) ;
+    sprintf(str, "%d.%02d V", v1, v2) ;
+    tft->BusEnable(true) ;
+    tft->locate(80, 190) ;
+    tft->printf(str) ;
+    wait(0.01) ;
+    tft->BusEnable(false) ;
+}
+
+void meter::update(float value) 
+{
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/meter.h	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,29 @@
+#ifndef _METER_H_
+#define _METER_H_
+
+class meter {
+public:
+meter(int x, int y, int width, int height, float min, float max) ;
+~meter(void) ;
+void drawFrame(void) ;
+void drawScale(void) ;
+void drawHand(float value) ;
+void drawValue(float value) ;
+void draw(void) ;
+void draw(float value) ;
+void update(float value) ;
+
+private:
+int _x ;
+int _y ;
+int _w ;
+int _h ;
+int _center_x ;
+int _center_y ;
+float _value ;
+float _min ;
+float _max ;
+
+} ;
+
+#endif /* _METER_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vt100.lib	Thu Jul 20 02:37:13 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/Rhyme/code/vt100/#b7229a9eae1c