A simple meter using Adafruit 2.8 TFT with touch v2
Dependencies: SPI_STMPE610 UniGraphic mbed vt100
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桁に変更しました。
Revision 3:bf8761c8eb17, committed 2017-07-21
- Comitter:
- Rhyme
- Date:
- Fri Jul 21 02:01:05 2017 +0000
- Parent:
- 2:204e60dbd73d
- Child:
- 4:0607a2a46238
- Commit message:
- Some source code clean-ups done; The first document added
Changed in this revision
--- a/main.cpp Fri Jul 21 01:24:53 2017 +0000
+++ b/main.cpp Fri Jul 21 02:01:05 2017 +0000
@@ -1,20 +1,19 @@
+/**
+ * main.cpp
+ */
#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"
+#define ADC_MAX_VALUE 3.28
+
vt100 *tty = 0 ;
ILI9341 *tft = 0 ;
-SPI_STMPE610 *tsc = 0 ;
meter *tacho = 0 ;
-DigitalOut myled(LED1, 1);
DigitalOut *backlight = 0 ;
AnalogIn *vin = 0 ;
@@ -39,34 +38,39 @@
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") ;
+ 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) ;
+ *backlight = 1 ;
+}
+
+double getRoundedValue(void)
+{
+ double value = 0.0 ;
+ int intvalue = 0 ;
+ value = ADC_MAX_VALUE * vin->read() ;
+ intvalue = (int)(100.0 * value + 0.5) ;
+ value = (double)intvalue / 100.0 ;
+ return( value ) ;
}
int main() {
float value = 0.0 ;
float prev_value = 0.0 ;
- int intvalue = 0 ;
+
init_hardware() ;
- *backlight = 1 ;
-
- tacho->drawFrame() ;
- tacho->drawScale() ;
+ tacho->draw(value) ;
+
while(1) {
- value = 3.28 * vin->read() ;
- intvalue = (int)(100.0 * value + 0.5) ;
- value = (double)intvalue / 100.0 ;
+ value = getRoundedValue() ;
if (value != prev_value) {
- tacho->drawHand(value) ;
- tacho->drawValue(value) ;
+ tacho->update(value) ;
prev_value = value ;
}
- wait(0.2) ;
+ wait(0.1) ;
}
}
--- a/meter.cpp Fri Jul 21 01:24:53 2017 +0000
+++ b/meter.cpp Fri Jul 21 02:01:05 2017 +0000
@@ -2,25 +2,18 @@
#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] ;
+double SIN[451] ; /* sin table for 45 to 90 degrees in 0.1 deg step */
+double COS[451] ; /* cos table for 45 to 90 degrees in 0.1 deg step */
static void fillTriTable(void)
{
@@ -39,7 +32,6 @@
_y = y ;
_w = width ;
_h = height ;
- _value = min ;
_min = min ;
_max = max ;
_center_x = _x + _w / 2 ;
@@ -97,11 +89,11 @@
if (theta < 0.0) { theta = 0.0 ; }
if (theta > 90.0) { theta = 90.0 ; }
if (theta > 45.0) {
- i = (10.0 * (90.0 - theta + 45.0) + 0.5) - 450 ;
- x1 = _center_x -(radius * COS[i] + 0.5) ;
- x2 = _center_x -((radius + append) * COS[i] + 0.5) ;
+ i = (10.0 * (90.0 - theta) + 0.5) ;
+ x1 = _center_x - (radius * COS[i] + 0.5) ;
+ x2 = _center_x - ((radius + append) * COS[i] + 0.5) ;
} else {
- i = (10.0 * (theta + 45.0) + 0.5) - 450 ;
+ i = (10.0 * theta + 0.5) ;
x1 = _center_x + (radius * COS[i] + 0.5) ;
x2 = _center_x + ((radius + append) * COS[i] + 0.5) ;
}
@@ -109,8 +101,8 @@
y1 = _center_y - (radius * SIN[i] + 0.5) ;
y2 = _center_y - ((radius + append) * SIN[i] + 0.5) ;
tft->BusEnable(true) ;
- tft->line(prev_x1, prev_y1, prev_x2, prev_y2, White) ;
- tft->line(x1, y1, x2, y2, Black) ;
+ tft->line(prev_x1, prev_y1, prev_x2, prev_y2, White) ; /* erase prev */
+ tft->line(x1, y1, x2, y2, Black) ; /* draw current */
tft->BusEnable(false) ;
prev_x1 = x1 ;
prev_x2 = x2 ;
@@ -118,14 +110,13 @@
prev_y2 = y2 ;
}
-void meter::draw(void)
-{
-}
-
void meter::draw(float value)
{
tft->BusEnable(true) ;
- drawHand(_value) ;
+ drawFrame() ;
+ drawScale() ;
+ drawHand(value) ;
+ drawValue(value) ;
tft->BusEnable(false) ;
}
@@ -145,4 +136,8 @@
void meter::update(float value)
{
+ tft->BusEnable(true) ;
+ drawHand(value) ;
+ drawValue(value) ;
+ tft->BusEnable(false) ;
}
\ No newline at end of file
--- a/meter.h Fri Jul 21 01:24:53 2017 +0000
+++ b/meter.h Fri Jul 21 02:01:05 2017 +0000
@@ -1,16 +1,59 @@
#ifndef _METER_H_
#define _METER_H_
+/**
+ * meter class
+ * A simple analog style meter
+ **/
class meter {
public:
+/**
+ * meter constructor
+ * @param x right-top position of meter
+ * @param y right-top position of meter
+ * @param width width of meter
+ * @param height height of meter
+ * @param min minimum value of meter
+ * @param max maximum value of meter
+ */
meter(int x, int y, int width, int height, float min, float max) ;
+
+/**
+ * meter destructor
+ */
~meter(void) ;
+
+/**
+ * drawFrame draw meter frame/canvas
+ */
void drawFrame(void) ;
+
+/**
+ * drawScale draw measuring mark
+ */
void drawScale(void) ;
+
+/**
+ * drawHand draw meter hand at value position
+ * @param value value to plot the hand
+ */
void drawHand(float value) ;
+
+/**
+ * drawValue draw textual value in the lower part of the meter
+ * @param value value to display (voltage assumed)
+ */
void drawValue(float value) ;
-void draw(void) ;
+
+/**
+ * draw draw full set of the meter
+ * @param value value to display and put hand
+ */
void draw(float value) ;
+
+/**
+ * update draw only hand and value text
+ */
void update(float value) ;
private:
@@ -20,7 +63,6 @@
int _h ;
int _center_x ;
int _center_y ;
-float _value ;
float _min ;
float _max ;