-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchecker.hpp
494 lines (417 loc) · 12.2 KB
/
checker.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#pragma once
//=====================================================================//
/*! @file
@brief チェッカー・クラス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/R8C/blob/master/LICENSE
*/
//=====================================================================//
#include "common/renesas.hpp"
#include "common/fifo.hpp"
#include "common/uart_io.hpp"
#include "common/format.hpp"
#include "common/trb_io.hpp"
#include "common/adc_io.hpp"
#include "common/spi_io.hpp"
#include "chip/ST7565.hpp"
#include "common/monograph.hpp"
#include "common/font6x12.hpp"
#include "common/fixed_string.hpp"
namespace app {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief タイマーB、割り込みタスク
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class trb_task {
uint32_t time_;
uint8_t val_;
uint8_t lvl_;
uint8_t pos_;
uint8_t neg_;
public:
enum class type : uint8_t {
SW_A = 0x20, ///< SW-A
SW_B = 0x10 ///< SW-B
};
trb_task() : time_(0), val_(0), lvl_(0), pos_(0), neg_(0) { }
void init() const {
utils::PORT_MAP(utils::port_map::P34::PORT);
utils::PORT_MAP(utils::port_map::P35::PORT);
device::PD3.B4 = 0;
device::PD3.B5 = 0;
device::PUR3.B4 = 1; ///< プルアップ
device::PUR3.B5 = 1; ///< プルアップ
}
void operator() () {
++time_;
val_ = ~device::P3();
}
void service()
{
pos_ = ~lvl_ & val_; ///< 立ち上がりエッジ検出
neg_ = lvl_ & ~val_; ///< 立ち下がりエッジ検出
lvl_ = val_;
}
bool level(type t) const { return lvl_ & static_cast<uint8_t>(t); }
bool positive(type t) const { return pos_ & static_cast<uint8_t>(t); }
bool negative(type t) const { return neg_ & static_cast<uint8_t>(t); }
void set_time(uint32_t v) { time_ = v; }
uint32_t get_time() const { return time_; }
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief チェッカー・クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
class checker {
public:
typedef device::trb_io<trb_task, uint8_t> timer_b;
timer_b timer_b_;
#ifdef UART
typedef utils::fifo<uint8_t, 16> buffer;
typedef device::uart_io<device::UART0, buffer, buffer> uart;
uart uart_;
#endif
private:
typedef device::adc_io<utils::null_task> ADC;
ADC adc_;
// LCD SCL: P4_2(1)
typedef device::PORT<device::PORT4, device::bitpos::B2> SPI_SCL;
// LCD SDA: P4_5(12)
typedef device::PORT<device::PORT4, device::bitpos::B5> SPI_SDA;
// MISO, MOSI, SCK
typedef device::spi_io<device::NULL_PORT, SPI_SDA, SPI_SCL, device::soft_spi_mode::CK10> SPI;
SPI spi_;
// LCD /CS: P3_7(2)
typedef device::PORT<device::PORT3, device::bitpos::B7> LCD_SEL;
// LCD A0: P3_3(11)
typedef device::PORT<device::PORT3, device::bitpos::B3> LCD_A0;
// LCD /RES: P4_7(4)
typedef device::PORT<device::PORT4, device::bitpos::B7> LCD_RES;
typedef chip::ST7565<SPI, LCD_SEL, LCD_A0, LCD_RES> LCD;
LCD lcd_;
class PLOT {
public:
typedef int16_t value_type;
static const int16_t WIDTH = 128;
static const int16_t HEIGHT = 24;
static const int8_t PAGE_NUM = 2;
private:
uint8_t fb_[WIDTH * HEIGHT / 8];
public:
void clear(uint8_t v = 0)
{
for(uint16_t i = 0; i < (WIDTH * HEIGHT / 8); ++i) {
fb_[i] = v;
}
}
uint8_t* fb() { return fb_; }
void operator() (value_type x, value_type y, bool val)
{
if(x < 0 || x >= WIDTH) return;
if(y < 0 || y >= HEIGHT) return;
if(val) { fb_[(x >> 3) * y] |= 1 << (x & 7); }
else { fb_[(x >> 3) * y] &= ~(1 << (x & 7)); }
}
};
typedef graphics::font6x12 afont;
graphics::monograph<PLOT, afont> bitmap_;
graphics::kfont_null kfont_;
uint8_t loop_;
uint8_t page_;
float volt_;
float current_;
float watt_;
char str_[32];
enum class TASK : uint8_t {
MAIN, ///< 電圧、電流、表示
WATT_M, ///< 時間、電力(分)、表示
WATT_H, ///< 時間、電力(時)、表示
GRAPH, ///< グラフ表示
USB_REF, ///< USB D-, D+ 差動信号電圧表示
SETUP, ///< 設定
limit ///< 最大値
};
TASK task_;
uint8_t log_;
uint8_t log_itv_;
uint8_t gain_idx_;
uint8_t interval_;
float usb_m_;
float usb_p_;
#ifdef UART
uint8_t list_cnt_;
#endif
uint8_t buff_[128];
public:
//-------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-------------------------------------------------------------//
checker() : lcd_(spi_), bitmap_(kfont_), loop_(0), page_(0),
volt_(0.0f), current_(0.0f), watt_(0.0f),
task_(TASK::MAIN),
log_(0), log_itv_(0), gain_idx_(0), interval_(12),
usb_m_(0.0f), usb_p_(0.0f)
#ifdef UART
, list_cnt_(0)
#endif
{ }
//-------------------------------------------------------------//
/*!
@brief 初期化
*/
//-------------------------------------------------------------//
void init()
{
// タイマーB初期化
{
timer_b::task_.init();
uint8_t ir_level = 2;
timer_b_.start(50, ir_level);
}
#ifdef UART
// UART の設定 (P1_4: TXD0[out], P1_5: RXD0[in])
// ※シリアルライターでは、RXD 端子は、P1_6 となっているので注意!
{
utils::PORT_MAP(utils::port_map::P14::TXD0);
utils::PORT_MAP(utils::port_map::P15::RXD0);
uint8_t ir_level = 1;
uart_.start(57600, ir_level);
}
#endif
// ADC の設定
{
utils::PORT_MAP(utils::port_map::P10::AN0);
utils::PORT_MAP(utils::port_map::P11::AN1);
utils::PORT_MAP(utils::port_map::P12::AN2);
utils::PORT_MAP(utils::port_map::P13::AN3);
}
// SPI を開始
{
spi_.start(10000000);
}
// LCD を開始
{
// contrast, reverse=yes, BIAS9=false(BIAS7), Voltage-Ratio: 3
lcd_.start(0x1C, true, false, 3);
bitmap_.clear(0);
/// bitmap_.enable_2x();
}
#ifdef UART
utils::format("Start USB Checker\n");
#endif
}
//-------------------------------------------------------------//
/*!
@brief 電圧、電流表示
*/
//-------------------------------------------------------------//
void vc()
{
// 400mV/A * 3
if(page_ == 0) {
utils::sformat("%3.2fV", str_, sizeof(str_)) % volt_;
bitmap_.draw_text(0, 0, str_);
} else {
utils::sformat("%3.2fA", str_, sizeof(str_)) % current_;
bitmap_.draw_text(0, 0, str_);
}
}
//-------------------------------------------------------------//
/*!
@brief 経過時間、電力
@param[in] ch 表示単位
@param[in] wdiv 積算電力の母数
*/
//-------------------------------------------------------------//
void watt(const char* form, float watt)
{
if(page_ == 0) {
auto s = timer_b::task_.get_time() / 50;
auto m = s / 60;
auto h = m / 60;
utils::sformat("%02d:%02d:%02d", str_, sizeof(str_)) % (h % 24) % (m % 60) % (s % 60);
bitmap_.draw_text(0, 0, str_);
} else {
utils::sformat(form, str_, sizeof(str_)) % watt;
bitmap_.draw_text(0, 0, str_);
}
}
//-------------------------------------------------------------//
/*!
@brief グラフ表示
@param[in] o 表示オフセット
@param[in] w 表示範囲
*/
//-------------------------------------------------------------//
void graph(uint8_t o = 0, uint8_t w = 127)
{
static const int8_t gain_tbl[] = { 1, 2, 3, 4, 6, 8, 12, 16 };
int16_t gain = gain_tbl[gain_idx_];
if(page_ == 0) {
int16_t v = w - gain * w / 16;
bitmap_.line(o, 0, o + v, 0, true);
bitmap_.line(o, 1, o + v, 1, true);
}
uint8_t pos = log_ - w - 1;
pos &= 127;
int16_t v0 = static_cast<int16_t>(buff_[pos]);
v0 *= 3;
v0 /= gain;
for(uint8_t x = 0; x < w; ++x) {
++pos;
pos &= 127;
int16_t y0 = 47 - v0;
int16_t v1 = static_cast<int16_t>(buff_[pos]);
v1 *= 3;
v1 /= gain;
int16_t y1 = 47 - v1;
if(page_) {
y0 -= 24;
y1 -= 24;
}
int16_t x0 = static_cast<int16_t>(x) + o;
int16_t x1 = x0 + 1;
bitmap_.line(x0, y0, x1, y1, true);
v0 = v1;
}
}
//-------------------------------------------------------------//
/*!
@brief USB 信号電圧表示
*/
//-------------------------------------------------------------//
void usb_ref()
{
if(page_ == 0) {
utils::sformat("-D: %3.2fV", str_, sizeof(str_)) % usb_m_;
bitmap_.draw_text(0, 0, str_);
} else {
utils::sformat("+D: %3.2fV", str_, sizeof(str_)) % usb_p_;
bitmap_.draw_text(0, 0, str_);
}
}
//-------------------------------------------------------------//
/*!
@brief 設定
*/
//-------------------------------------------------------------//
void setup()
{
if(page_ == 0) {
} else {
}
}
//-------------------------------------------------------------//
/*!
@brief サービス
*/
//-------------------------------------------------------------//
void service()
{
timer_b_.sync();
timer_b_.task_.service();
adc_.start(ADC::CH_TYPE::CH0_CH1, ADC::CH_GROUP::AN0_AN1, false);
adc_.scan(); // AN0, AN1 A/D scan start
if(timer_b::task_.positive(timer_b::task_type::type::SW_A)) {
auto n = static_cast<uint8_t>(task_) + 1;
if(n >= static_cast<uint8_t>(TASK::limit)) n = 0;
task_ = static_cast<TASK>(n);
}
switch(task_) {
case TASK::WATT_M:
case TASK::WATT_H:
if(timer_b::task_.positive(timer_b::task_type::type::SW_B)) {
timer_b::task_.set_time(0);
watt_ = 0;
}
break;
case TASK::MAIN:
case TASK::GRAPH:
if(timer_b::task_.positive(timer_b::task_type::type::SW_B)) {
++gain_idx_;
if(gain_idx_ >= 8) gain_idx_ = 0; // 0 to 7
}
break;
case TASK::SETUP:
if(timer_b::task_.positive(timer_b::task_type::type::SW_B)) {
}
break;
default:
break;
}
adc_.sync(); // A/D scan sync
{
uint32_t i = adc_.get_value(0);
uint32_t v = adc_.get_value(1);
adc_.start(ADC::CH_TYPE::CH0_CH1, ADC::CH_GROUP::AN2_AN3, false);
adc_.scan(); // AN2, AN3 A/D scan start
if(i <= 3) i = 0; // noise bias
if(log_itv_ == 0) {
buff_[log_] = (i * v) >> 12;
++log_;
log_ &= 127;
log_itv_ = interval_;
} else {
--log_itv_;
}
current_ = static_cast<float>(i) / 1023.0f * 3.3f / (0.4f * 3.0f);
volt_ = static_cast<float>(v) / 1023.0f * 3.3f * 6.0f;
watt_ += volt_ * current_ / 50.0f;
}
{
adc_.sync(); // A/D scan sync
usb_m_ = static_cast<float>(adc_.get_value(0)) / 1023.0f * 3.3f;
usb_p_ = static_cast<float>(adc_.get_value(1)) / 1023.0f * 3.3f;
}
#ifdef UART
++list_cnt_;
if(list_cnt_ >= 50) {
list_cnt_ = 0;
utils::format("Vol: %3.2f [V], Cur: %3.2f [A]\n") % volt_ % current_;
utils::format("Watt: %8.5f [Wh]\n") % (watt_ / 3600.0f);
}
#endif
if(loop_ == 0) {
bitmap_.clear(0);
} else if(loop_ == 1) {
switch(task_) {
case TASK::MAIN:
vc();
graph(64, 64 - 1);
break;
case TASK::WATT_M:
watt("%8.3fWm", watt_ / 60.0f);
break;
case TASK::WATT_H:
watt("%8.5fWh", watt_ / 3600.0f);
break;
case TASK::GRAPH:
graph();
break;
case TASK::USB_REF:
usb_ref();
break;
case TASK::SETUP:
setup();
break;
default:
break;
}
} else if(loop_ == 2) {
lcd_.copy(bitmap_.at_plot().fb(), PLOT::PAGE_NUM, page_ * 3);
++page_;
page_ &= 1;
}
++loop_;
if(loop_ >= 3) {
loop_ = 0;
}
}
};
}