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桁に変更しました。
meter.cpp@4:0607a2a46238, 2018-05-18 (annotated)
- Committer:
- Rhyme
- Date:
- Fri May 18 01:25:32 2018 +0000
- Revision:
- 4:0607a2a46238
- Parent:
- 3:bf8761c8eb17
Changed the display value from 0.00 to 0.000
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:d01def9cf41e | 1 | #include "mbed.h" |
Rhyme | 0:d01def9cf41e | 2 | #include <math.h> |
Rhyme | 0:d01def9cf41e | 3 | #include <string.h> |
Rhyme | 0:d01def9cf41e | 4 | #include "ILI9341.h" |
Rhyme | 0:d01def9cf41e | 5 | #include "Arial28x28.h" |
Rhyme | 0:d01def9cf41e | 6 | #include "meter.h" |
Rhyme | 0:d01def9cf41e | 7 | #include "main.h" |
Rhyme | 0:d01def9cf41e | 8 | |
Rhyme | 0:d01def9cf41e | 9 | #ifndef M_PI |
Rhyme | 0:d01def9cf41e | 10 | #define M_PI 3.141593 |
Rhyme | 0:d01def9cf41e | 11 | #endif |
Rhyme | 0:d01def9cf41e | 12 | |
Rhyme | 0:d01def9cf41e | 13 | extern ILI9341 *tft ; |
Rhyme | 0:d01def9cf41e | 14 | extern meter *tacho ; |
Rhyme | 3:bf8761c8eb17 | 15 | double SIN[451] ; /* sin table for 45 to 90 degrees in 0.1 deg step */ |
Rhyme | 3:bf8761c8eb17 | 16 | double COS[451] ; /* cos table for 45 to 90 degrees in 0.1 deg step */ |
Rhyme | 0:d01def9cf41e | 17 | |
Rhyme | 0:d01def9cf41e | 18 | static void fillTriTable(void) |
Rhyme | 0:d01def9cf41e | 19 | { |
Rhyme | 0:d01def9cf41e | 20 | double theta ; |
Rhyme | 0:d01def9cf41e | 21 | int i ; |
Rhyme | 1:d7f2aa328962 | 22 | for (i = 0 ; i <= 450 ; i++ ) { |
Rhyme | 0:d01def9cf41e | 23 | theta = M_PI * (((double)(i + 450))/1800.0) ; |
Rhyme | 0:d01def9cf41e | 24 | SIN[i] = sin( theta ) ; |
Rhyme | 0:d01def9cf41e | 25 | COS[i] = cos( theta ) ; |
Rhyme | 0:d01def9cf41e | 26 | } |
Rhyme | 0:d01def9cf41e | 27 | } |
Rhyme | 0:d01def9cf41e | 28 | |
Rhyme | 0:d01def9cf41e | 29 | meter::meter(int x, int y, int width, int height, float min, float max) |
Rhyme | 0:d01def9cf41e | 30 | { |
Rhyme | 0:d01def9cf41e | 31 | _x = x ; |
Rhyme | 0:d01def9cf41e | 32 | _y = y ; |
Rhyme | 0:d01def9cf41e | 33 | _w = width ; |
Rhyme | 0:d01def9cf41e | 34 | _h = height ; |
Rhyme | 0:d01def9cf41e | 35 | _min = min ; |
Rhyme | 0:d01def9cf41e | 36 | _max = max ; |
Rhyme | 0:d01def9cf41e | 37 | _center_x = _x + _w / 2 ; |
Rhyme | 0:d01def9cf41e | 38 | _center_y = _y + (2 * _h) / 3 ; |
Rhyme | 0:d01def9cf41e | 39 | fillTriTable() ; |
Rhyme | 0:d01def9cf41e | 40 | } |
Rhyme | 0:d01def9cf41e | 41 | |
Rhyme | 0:d01def9cf41e | 42 | meter::~meter(void) |
Rhyme | 0:d01def9cf41e | 43 | { |
Rhyme | 0:d01def9cf41e | 44 | } |
Rhyme | 0:d01def9cf41e | 45 | |
Rhyme | 0:d01def9cf41e | 46 | void meter::drawFrame(void) |
Rhyme | 0:d01def9cf41e | 47 | { |
Rhyme | 0:d01def9cf41e | 48 | tft->BusEnable(true) ; |
Rhyme | 0:d01def9cf41e | 49 | tft->fillrect(_x, _y, _x+ _w - 1, _y+(2 * _h)/3 - 1, White) ; |
Rhyme | 0:d01def9cf41e | 50 | tft->fillrect(_x, _y+(2 * _h)/3 - 1, _x + _w - 1, _y + _h - 1, Black) ; |
Rhyme | 0:d01def9cf41e | 51 | tft->rect(_x, _y, _x + _w - 1, _y + _h - 1, Green) ; |
Rhyme | 0:d01def9cf41e | 52 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 53 | } |
Rhyme | 0:d01def9cf41e | 54 | |
Rhyme | 0:d01def9cf41e | 55 | void meter::drawScale(void) |
Rhyme | 0:d01def9cf41e | 56 | { |
Rhyme | 0:d01def9cf41e | 57 | int x1, x2, y1, y2 ; |
Rhyme | 0:d01def9cf41e | 58 | double radius ; |
Rhyme | 0:d01def9cf41e | 59 | double append ; |
Rhyme | 0:d01def9cf41e | 60 | radius = _w / 2.0 ; |
Rhyme | 0:d01def9cf41e | 61 | tft->BusEnable(true) ; |
Rhyme | 0:d01def9cf41e | 62 | for (int i = 0 ; i < 450 ; i += 45 ) { |
Rhyme | 0:d01def9cf41e | 63 | x1 = (radius * COS[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 64 | y1 = (radius * SIN[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 65 | if ((i % 10) == 5) { |
Rhyme | 0:d01def9cf41e | 66 | append = 5 ; |
Rhyme | 0:d01def9cf41e | 67 | } else { |
Rhyme | 0:d01def9cf41e | 68 | append = 10 ; |
Rhyme | 0:d01def9cf41e | 69 | } |
Rhyme | 0:d01def9cf41e | 70 | x2 = ((radius + append) * COS[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 71 | y2 = ((radius + append) * SIN[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 72 | tft->line(_center_x + x1, _center_y - y1, _center_x + x2, _center_y - y2, Black) ; |
Rhyme | 0:d01def9cf41e | 73 | tft->line(_center_x - x1, _center_y - y1, _center_x - x2, _center_y - y2, Black) ; |
Rhyme | 0:d01def9cf41e | 74 | } |
Rhyme | 0:d01def9cf41e | 75 | tft->line(_center_x, _center_y - radius, _center_x, _center_y - (radius + 10), Black) ; |
Rhyme | 0:d01def9cf41e | 76 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 77 | } |
Rhyme | 0:d01def9cf41e | 78 | |
Rhyme | 0:d01def9cf41e | 79 | void meter::drawHand(float value) |
Rhyme | 0:d01def9cf41e | 80 | { |
Rhyme | 0:d01def9cf41e | 81 | static int prev_x1 = 0, prev_x2 = 0, prev_y1 = 0, prev_y2 = 0 ; |
Rhyme | 0:d01def9cf41e | 82 | int x1, x2, y1, y2 ; |
Rhyme | 0:d01def9cf41e | 83 | double theta ; |
Rhyme | 0:d01def9cf41e | 84 | double radius = _w / 20.0 ; |
Rhyme | 0:d01def9cf41e | 85 | double append = 8.0 * (_w / 20.0) ; |
Rhyme | 0:d01def9cf41e | 86 | int i ; |
Rhyme | 0:d01def9cf41e | 87 | |
Rhyme | 0:d01def9cf41e | 88 | theta = 90.0 - (90.0 * (value / _max)) ; |
Rhyme | 1:d7f2aa328962 | 89 | if (theta < 0.0) { theta = 0.0 ; } |
Rhyme | 1:d7f2aa328962 | 90 | if (theta > 90.0) { theta = 90.0 ; } |
Rhyme | 0:d01def9cf41e | 91 | if (theta > 45.0) { |
Rhyme | 3:bf8761c8eb17 | 92 | i = (10.0 * (90.0 - theta) + 0.5) ; |
Rhyme | 3:bf8761c8eb17 | 93 | x1 = _center_x - (radius * COS[i] + 0.5) ; |
Rhyme | 3:bf8761c8eb17 | 94 | x2 = _center_x - ((radius + append) * COS[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 95 | } else { |
Rhyme | 3:bf8761c8eb17 | 96 | i = (10.0 * theta + 0.5) ; |
Rhyme | 2:204e60dbd73d | 97 | x1 = _center_x + (radius * COS[i] + 0.5) ; |
Rhyme | 2:204e60dbd73d | 98 | x2 = _center_x + ((radius + append) * COS[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 99 | } |
Rhyme | 1:d7f2aa328962 | 100 | |
Rhyme | 2:204e60dbd73d | 101 | y1 = _center_y - (radius * SIN[i] + 0.5) ; |
Rhyme | 2:204e60dbd73d | 102 | y2 = _center_y - ((radius + append) * SIN[i] + 0.5) ; |
Rhyme | 0:d01def9cf41e | 103 | tft->BusEnable(true) ; |
Rhyme | 3:bf8761c8eb17 | 104 | tft->line(prev_x1, prev_y1, prev_x2, prev_y2, White) ; /* erase prev */ |
Rhyme | 3:bf8761c8eb17 | 105 | tft->line(x1, y1, x2, y2, Black) ; /* draw current */ |
Rhyme | 0:d01def9cf41e | 106 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 107 | prev_x1 = x1 ; |
Rhyme | 0:d01def9cf41e | 108 | prev_x2 = x2 ; |
Rhyme | 0:d01def9cf41e | 109 | prev_y1 = y1 ; |
Rhyme | 0:d01def9cf41e | 110 | prev_y2 = y2 ; |
Rhyme | 0:d01def9cf41e | 111 | } |
Rhyme | 0:d01def9cf41e | 112 | |
Rhyme | 0:d01def9cf41e | 113 | void meter::draw(float value) |
Rhyme | 0:d01def9cf41e | 114 | { |
Rhyme | 0:d01def9cf41e | 115 | tft->BusEnable(true) ; |
Rhyme | 3:bf8761c8eb17 | 116 | drawFrame() ; |
Rhyme | 3:bf8761c8eb17 | 117 | drawScale() ; |
Rhyme | 3:bf8761c8eb17 | 118 | drawHand(value) ; |
Rhyme | 3:bf8761c8eb17 | 119 | drawValue(value) ; |
Rhyme | 0:d01def9cf41e | 120 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 121 | } |
Rhyme | 0:d01def9cf41e | 122 | |
Rhyme | 0:d01def9cf41e | 123 | void meter::drawValue(float value) |
Rhyme | 0:d01def9cf41e | 124 | { |
Rhyme | 0:d01def9cf41e | 125 | char str[32] ; |
Rhyme | 0:d01def9cf41e | 126 | int v1, v2 ; |
Rhyme | 0:d01def9cf41e | 127 | v1 = (int)value ; |
Rhyme | 4:0607a2a46238 | 128 | v2 = ((int)(1000.0 * value) % 1000) ; |
Rhyme | 4:0607a2a46238 | 129 | sprintf(str, "%d.%03d V", v1, v2) ; |
Rhyme | 0:d01def9cf41e | 130 | tft->BusEnable(true) ; |
Rhyme | 0:d01def9cf41e | 131 | tft->locate(80, 190) ; |
Rhyme | 0:d01def9cf41e | 132 | tft->printf(str) ; |
Rhyme | 0:d01def9cf41e | 133 | wait(0.01) ; |
Rhyme | 0:d01def9cf41e | 134 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 135 | } |
Rhyme | 0:d01def9cf41e | 136 | |
Rhyme | 0:d01def9cf41e | 137 | void meter::update(float value) |
Rhyme | 0:d01def9cf41e | 138 | { |
Rhyme | 3:bf8761c8eb17 | 139 | tft->BusEnable(true) ; |
Rhyme | 3:bf8761c8eb17 | 140 | drawHand(value) ; |
Rhyme | 3:bf8761c8eb17 | 141 | drawValue(value) ; |
Rhyme | 3:bf8761c8eb17 | 142 | tft->BusEnable(false) ; |
Rhyme | 0:d01def9cf41e | 143 | } |