2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_rssi.hpp"
|
|
|
|
|
2016-07-27 23:25:33 -04:00
|
|
|
#include "baseband_api.hpp"
|
2016-01-27 15:50:33 -05:00
|
|
|
#include "utility.hpp"
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
void RSSI::paint(Painter& painter) {
|
|
|
|
const auto r = screen_rect();
|
2016-01-27 14:48:22 -05:00
|
|
|
|
2016-01-27 15:50:33 -05:00
|
|
|
constexpr int rssi_sample_range = 256;
|
2016-01-27 14:48:22 -05:00
|
|
|
constexpr float rssi_voltage_min = 0.4;
|
|
|
|
constexpr float rssi_voltage_max = 2.2;
|
|
|
|
constexpr float adc_voltage_max = 3.3;
|
2016-01-27 15:50:33 -05:00
|
|
|
constexpr int raw_min = rssi_sample_range * rssi_voltage_min / adc_voltage_max;
|
|
|
|
constexpr int raw_max = rssi_sample_range * rssi_voltage_max / adc_voltage_max;
|
|
|
|
constexpr int raw_delta = raw_max - raw_min;
|
|
|
|
const range_t<int> x_avg_range { 0, r.width() - 1 };
|
|
|
|
const auto x_avg = x_avg_range.clip((avg_ - raw_min) * r.width() / raw_delta);
|
|
|
|
const range_t<int> x_min_range { 0, x_avg };
|
|
|
|
const auto x_min = x_min_range.clip((min_ - raw_min) * r.width() / raw_delta);
|
|
|
|
const range_t<int> x_max_range { x_avg + 1, r.width() };
|
|
|
|
const auto x_max = x_max_range.clip((max_ - raw_min) * r.width() / raw_delta);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
const Rect r0 { r.left(), r.top(), x_min, r.height() };
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
r0,
|
|
|
|
Color::blue()
|
|
|
|
);
|
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
const Rect r1 { r.left() + x_min, r.top(), x_avg - x_min, r.height() };
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
r1,
|
|
|
|
Color::red()
|
|
|
|
);
|
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
const Rect r2 { r.left() + x_avg, r.top(), 1, r.height() };
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
r2,
|
|
|
|
Color::white()
|
|
|
|
);
|
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
const Rect r3 { r.left() + x_avg + 1, r.top(), x_max - (x_avg + 1), r.height() };
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
r3,
|
|
|
|
Color::red()
|
|
|
|
);
|
|
|
|
|
2016-11-29 13:13:56 -05:00
|
|
|
const Rect r4 { r.left() + x_max, r.top(), r.width() - x_max, r.height() };
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.fill_rectangle(
|
|
|
|
r4,
|
|
|
|
Color::black()
|
|
|
|
);
|
2016-07-27 23:25:33 -04:00
|
|
|
|
|
|
|
if (pwmrssi_enabled) {
|
|
|
|
const range_t<int> pwmrssi_avg_range { 0, 96 };
|
|
|
|
const auto pwmrssi_avg = pwmrssi_avg_range.clip((avg_ - raw_min) * 96 / raw_delta);
|
|
|
|
baseband::set_pwmrssi(pwmrssi_avg, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSSI::set_pwmrssi(bool enabled) {
|
|
|
|
pwmrssi_enabled = enabled;
|
|
|
|
if (!enabled) baseband::set_pwmrssi(0, false);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RSSI::on_statistics_update(const RSSIStatistics& statistics) {
|
|
|
|
min_ = statistics.min;
|
|
|
|
avg_ = statistics.accumulator / statistics.count;
|
|
|
|
max_ = statistics.max;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|