Clean up temperature sensor rendering.

This commit is contained in:
Jared Boone 2015-12-17 20:36:58 -08:00
parent 188f692195
commit 1146d1fa06
2 changed files with 73 additions and 20 deletions

View File

@ -63,40 +63,77 @@ void TemperatureWidget::paint(Painter& painter) {
const auto logger = portapack::temperature_logger;
const auto rect = screen_rect();
const Color color_background { 0, 0, 64 };
const Color color_foreground = Color::green();
const Color color_reticle { 128, 128, 128 };
const Dim bar_width = 1;
const Dim margin_x = (rect.width() - (logger.capacity() * bar_width)) / 2;
const Coord rightmost_x = rect.right() - margin_x;
const Dim graph_width = logger.capacity() * bar_width;
const Dim graph_height = rect.height() - 16;
const Rect graph_rect {
rect.left() + (rect.width() - graph_width) / 2, rect.top() + 8,
graph_width, rect.height()
};
const Rect frame_rect {
graph_rect.left() - 1, graph_rect.top() - 1,
graph_rect.width() + 2, graph_rect.height() + 2
};
painter.draw_rectangle(frame_rect, color_reticle);
painter.fill_rectangle(graph_rect, color_background);
const auto history = logger.history();
for(size_t i=0; i<history.size(); i++) {
const Coord x = graph_rect.right() - (history.size() - i) * bar_width;
const auto sample = history[i];
const Dim bar_height = sample * 4;
const Coord x = rightmost_x - (history.size() - i) * bar_width;
const Coord y = rect.bottom() - bar_height;
const Rect bar_rect { x, y, bar_width, bar_height };
painter.fill_rectangle(bar_rect, Color::green());
const auto temp = temperature(sample);
const auto y = screen_y(temp, graph_rect);
const Dim bar_height = graph_rect.bottom() - y;
painter.fill_rectangle({ x, y, bar_width, bar_height }, color_foreground);
}
if( !history.empty() ) {
const int32_t temp = temperature(history.back());
const size_t temp_len = 3;
painter.draw_string(
{ static_cast<Coord>(rect.right() - (temp_len * 8)), rect.top() },
style(),
to_string_dec_int(temp, temp_len)
);
const auto sample = history.back();
const auto temp = temperature(sample);
const auto last_y = screen_y(temp, graph_rect);
const Coord x = graph_rect.right() + 8;
const Coord y = last_y - 8;
painter.draw_string({ x, y }, style(), temperature_str(temp));
}
const auto display_temp_max = display_temp_min + (graph_rect.height() / display_temp_scale);
for(auto temp=display_temp_min; temp<=display_temp_max; temp+=10) {
const int32_t tick_length = 6;
const auto tick_x = graph_rect.left() - tick_length;
const auto tick_y = screen_y(temp, graph_rect);
painter.fill_rectangle({ tick_x, tick_y, tick_length, 1 }, color_reticle);
const auto text_x = graph_rect.left() - temp_len * 8 - 8;
const auto text_y = tick_y - 8;
painter.draw_string({ text_x, text_y }, style(), temperature_str(temp));
}
}
int32_t TemperatureWidget::temperature(const int32_t sensor_value) {
return -45 + sensor_value * 5
TemperatureWidget::temperature_t TemperatureWidget::temperature(const sample_t sensor_value) const {
return -45 + sensor_value * 5;
}
std::string TemperatureWidget::temperature_str(const temperature_t temperature) const {
return to_string_dec_int(temperature, temp_len - 1) + "C";
}
Coord TemperatureWidget::screen_y(
const temperature_t temperature,
const Rect& rect
) const {
const Coord y_raw = rect.bottom() - ((temperature - display_temp_min) * display_temp_scale);
const auto y_limit = std::min(rect.bottom(), std::max(rect.top(), y_raw));
return y_limit;
}
/* TemperatureView *******************************************************/
TemperatureView::TemperatureView(NavigationView& nav) {
add_children({ {
&text_title,
&temperature_widget,
&button_done,
} });

View File

@ -93,7 +93,18 @@ public:
void paint(Painter& painter) override;
private:
int32_t temperature(const uint32_t sensor_value);
using sample_t = uint32_t;
using temperature_t = int32_t;
temperature_t temperature(const sample_t sensor_value) const;
Coord screen_y(const temperature_t temperature, const Rect& screen_rect) const;
std::string temperature_str(const temperature_t temperature) const;
static constexpr temperature_t display_temp_min = 0;
static constexpr temperature_t display_temp_scale = 3;
static constexpr Dim bar_width = 1;
static constexpr size_t temp_len = 3;
};
class TemperatureView : public View {
@ -103,12 +114,17 @@ public:
void focus() override;
private:
Text text_title {
{ 76, 16, 240, 16 },
"Temperature",
};
TemperatureWidget temperature_widget {
{ 0, 16, 240, 192 },
{ 0, 40, 240, 180 },
};
Button button_done {
{ 72, 256, 96, 24 },
{ 72, 264, 96, 24 },
"Done"
};
};