From 3c1641536c10b672b336aefa1fdc3b354a37d240 Mon Sep 17 00:00:00 2001 From: Yu Nejigane Date: Fri, 1 Feb 2013 02:28:55 +0900 Subject: [PATCH] Added ChartView --- ChartView.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ ChartView.h | 25 ++++++++++++++++++ SliderView.cpp | 2 +- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 ChartView.cpp create mode 100644 ChartView.h diff --git a/ChartView.cpp b/ChartView.cpp new file mode 100644 index 0000000..3ff08d4 --- /dev/null +++ b/ChartView.cpp @@ -0,0 +1,69 @@ +#include "ChartView.h" + +namespace minimal { + +ChartView::ChartView(const char* name, int16_t min, int16_t max) + : View(name), min_(min), max_(max), value_(0) { + if (min_ > max_) { + int16_t tmp = min_; + min_ = max_; + max_ = tmp; + } + memset(history_, 0, sizeof(history_)); +} + +void ChartView::drawChart() { + uint8_t buffer[100] = {0}; + + for (uint8_t i = 0; i < 4; ++i) { + uint8_t offset = (3 - i) * 8; + memset(buffer, 0, sizeof(buffer)); + for (uint8_t j = 0; j < 100; ++j) { + if (history_[j] + 4 > offset) { + uint8_t diff = history_[j] + 4 - offset; + if (diff >= 8) { + buffer[j] = 255; + } else { + for (uint8_t k = 0; k < diff; ++k) buffer[j] = (buffer[j] >> 1) | 128; + } + } + } + if (i == 3) { + for (uint8_t j = 0; j < 100; ++j) buffer[j] &= 15; + } + AD12864SPI::write(i + 2, 14, buffer, 100); + } +} + +void ChartView::drawValue() { + char number[7]; + sprintf(number, "%6d", value_); + uint8_t buffer[Font::GLYPH_WIDTH * 6] = {0}; + uint8_t bufferLength = Font::GLYPH_WIDTH * 6; + Font::getData(number, buffer, bufferLength, true); + AD12864SPI::write(6, 20, buffer, bufferLength); + memset(buffer, 0, sizeof(buffer)); + Font::getData(number, buffer, bufferLength, false); + AD12864SPI::write(7, 20, buffer, bufferLength); +} + +void ChartView::update(int16_t value) { + value_ = value; + if (value_ < min_) value_ = min_; + if (value_ > max_) value_ = max_; + + for (uint8_t i = 0; i < 99; ++i) history_[i] = history_[i + 1]; + + uint16_t a = value_ - min_; + uint16_t b = max_ - min_; + history_[99] = b == 0 ? 24 : a * 24 / b; +} + +void ChartView::draw() { + View::draw(); + drawOkButton(true); + drawChart(); + drawValue(); +} + +} \ No newline at end of file diff --git a/ChartView.h b/ChartView.h new file mode 100644 index 0000000..daabdb8 --- /dev/null +++ b/ChartView.h @@ -0,0 +1,25 @@ +#ifndef __MINIMAL_CHARTVIEW_H__ +#define __MINIMAL_CHARTVIEW_H__ + +#include "View.h" + +namespace minimal { + +class ChartView : public View { +protected: + uint8_t history_[100]; + int16_t min_; + int16_t max_; + int16_t value_; + void drawValue(); + void drawChart(); + +public: + ChartView(const char* name, int16_t min = 0, int16_t max = 1023); + virtual void draw(); + void update(int16_t value); +}; + +} + +#endif diff --git a/SliderView.cpp b/SliderView.cpp index 8cb4fce..d1672f9 100644 --- a/SliderView.cpp +++ b/SliderView.cpp @@ -34,7 +34,7 @@ void SliderView::drawValue() { void SliderView::drawSlider() { uint16_t a = value_ - min_; uint16_t b = max_ - min_; - uint16_t barLength = a * 100 / b; + uint16_t barLength = b == 0 ? 100 : a * 100 / b; uint8_t buffer[AD12864SPI::COLUMN_SIZE] = {0}; buffer[12] = 254;