|
| 1 | +// -!- C++ -!- ////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// System : |
| 4 | +// Module : |
| 5 | +// Object Name : $RCSfile$ |
| 6 | +// Revision : $Revision$ |
| 7 | +// Date : $Date$ |
| 8 | +// Author : $Author$ |
| 9 | +// Created By : Robert Heller |
| 10 | +// Created : Mon Sep 9 09:39:50 2024 |
| 11 | +// Last Modified : <240909.1520> |
| 12 | +// |
| 13 | +// Description |
| 14 | +// |
| 15 | +// Notes |
| 16 | +// |
| 17 | +// History |
| 18 | +// |
| 19 | +///////////////////////////////////////////////////////////////////////////// |
| 20 | +// |
| 21 | +// Copyright (C) 2024 Robert Heller D/B/A Deepwoods Software |
| 22 | +// 51 Locke Hill Road |
| 23 | +// Wendell, MA 01379-9728 |
| 24 | +// |
| 25 | +// This program is free software; you can redistribute it and/or modify |
| 26 | +// it under the terms of the GNU General Public License as published by |
| 27 | +// the Free Software Foundation; either version 2 of the License, or |
| 28 | +// (at your option) any later version. |
| 29 | +// |
| 30 | +// This program is distributed in the hope that it will be useful, |
| 31 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | +// GNU General Public License for more details. |
| 34 | +// |
| 35 | +// You should have received a copy of the GNU General Public License |
| 36 | +// along with this program; if not, write to the Free Software |
| 37 | +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 38 | +// |
| 39 | +// |
| 40 | +// |
| 41 | +////////////////////////////////////////////////////////////////////////////// |
| 42 | + |
| 43 | +static const char rcsid[] = "@(#) : $Id$"; |
| 44 | + |
| 45 | +#include "openlcb/RefreshLoop.hxx" |
| 46 | +#include "Adafruit_HX8357.h" |
| 47 | +#include "Adafruit_GFX.h" |
| 48 | +#include "Adafruit_TSC2007.h" |
| 49 | +#include "hardware.hxx" |
| 50 | +#include "DisplayNetworkHealth.hxx" |
| 51 | +#include "NetworkHealthScan.hxx" |
| 52 | +#include "StringUtils.hxx" |
| 53 | + |
| 54 | +namespace Arduino |
| 55 | +{ |
| 56 | +static long map(long x, long in_min, long in_max, long out_min, long out_max) |
| 57 | +{ |
| 58 | + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
| 59 | +} |
| 60 | +} |
| 61 | + |
| 62 | +namespace DisplayNetworkHealth |
| 63 | +{ |
| 64 | + |
| 65 | +DisplayNetworkHealth::DisplayNetworkHealth(Adafruit_HX8357 *display, |
| 66 | + Adafruit_TSC2007 *touch, |
| 67 | + NetworkHealthScan *healthScan) |
| 68 | + : display_(display) |
| 69 | +, touch_(touch) |
| 70 | +, healthScan_(healthScan) |
| 71 | +, lastTotal_(0) |
| 72 | +, nodelist_(display,LISTBOX_X,LISTBOX_Y,LISTBOX_TSIZE,LISTBOX_ITEMS) |
| 73 | +{ |
| 74 | + rescan_.initButtonUL(display_,BUTTON1_X,BUTTONS_Y,BUTTONS_W,BUTTONS_H, |
| 75 | + HX8357_WHITE,HX8357_BLACK,HX8357_MAGENTA, |
| 76 | + BUTTON1,BUTTONS_TXSZ); |
| 77 | + resetDB_.initButtonUL(display_,BUTTON2_X,BUTTONS_Y,BUTTONS_W,BUTTONS_H, |
| 78 | + HX8357_WHITE,HX8357_BLACK,HX8357_MAGENTA, |
| 79 | + BUTTON2,BUTTONS_TXSZ); |
| 80 | + refreshDisplay_(); |
| 81 | +} |
| 82 | + |
| 83 | +void DisplayNetworkHealth::poll_33hz(openlcb::WriteHelper *helper, |
| 84 | + Notifiable *done) |
| 85 | +{ |
| 86 | + if (healthScan_->Total() != lastTotal_) |
| 87 | + { |
| 88 | + nodelist_.RedrawList(healthScan_->NodeDB_Begin(), |
| 89 | + healthScan_->NodeDB_End()); |
| 90 | + redrawTotals_(); |
| 91 | + } |
| 92 | + processButons_(); |
| 93 | + done->notify(); |
| 94 | +} |
| 95 | + |
| 96 | +void DisplayNetworkHealth::refreshDisplay_() |
| 97 | +{ |
| 98 | + display_->fillScreen(HX8357_BLACK); |
| 99 | + redrawHeading_(); |
| 100 | + nodelist_.RedrawList(healthScan_->NodeDB_Begin(), |
| 101 | + healthScan_->NodeDB_End()); |
| 102 | + redrawTotals_(); |
| 103 | + redrawButtons_(); |
| 104 | +} |
| 105 | + |
| 106 | +void DisplayNetworkHealth::redrawHeading_() |
| 107 | +{ |
| 108 | + display_->setCursor(HEADING_X,HEADING_Y); |
| 109 | + display_->setTextSize(HEADING_TXSZ); |
| 110 | + display_->setTextColor(HEADING_COLOR,HEADING_BG); |
| 111 | + display_->print(HEADING); |
| 112 | +} |
| 113 | + |
| 114 | +void DisplayNetworkHealth::redrawTotals_() |
| 115 | +{ |
| 116 | + char buffer[20]; |
| 117 | + display_->setCursor(TOTALS_X,TOTALS_Y); |
| 118 | + display_->setTextSize(TOTALS_TXSZ); |
| 119 | + display_->setTextColor(TOTALS_COLOR,TOTALS_BG); |
| 120 | + snprintf(buffer,sizeof(buffer),TOTALS_FORMAT, |
| 121 | + healthScan_->Total(),healthScan_->Found(), |
| 122 | + healthScan_->Missing(),healthScan_->Added()); |
| 123 | + display_->print(buffer); |
| 124 | +} |
| 125 | + |
| 126 | +void DisplayNetworkHealth::redrawButtons_() |
| 127 | +{ |
| 128 | + rescan_.drawButton(); |
| 129 | + resetDB_.drawButton(); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +void DisplayNetworkHealth::processButons_() |
| 134 | +{ |
| 135 | + TS_Point p = touch_->getPoint(); |
| 136 | + if (((p.x == 0) && (p.y == 0)) || (p.z < 10)) |
| 137 | + { |
| 138 | + // this is our way of tracking touch 'release'! |
| 139 | + p.x = p.y = p.z = -1; |
| 140 | + } |
| 141 | + // Scale from ~0->4000 to tft.width using the calibration #'s |
| 142 | + if (p.z != -1) |
| 143 | + { |
| 144 | + int py = Arduino::map(p.x, TSMax_x, TSMin_x, 0, display_->height()); |
| 145 | + int px = Arduino::map(p.y, TSMin_y, TSMax_y, 0, display_->width()); |
| 146 | + p.x = px; |
| 147 | + p.y = py; |
| 148 | + } |
| 149 | + if (rescan_.contains(p.x, p.y)) |
| 150 | + { |
| 151 | + rescan_.press(true); |
| 152 | + |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + rescan_.press(false); |
| 157 | + } |
| 158 | + if (resetDB_.contains(p.x, p.y)) |
| 159 | + { |
| 160 | + resetDB_.press(true); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + resetDB_.press(false); |
| 165 | + } |
| 166 | + if (rescan_.justReleased()) |
| 167 | + { |
| 168 | + rescan_.drawButton(false); |
| 169 | + healthScan_->ScanNetwork(); |
| 170 | + } |
| 171 | + if (rescan_.justPressed()) |
| 172 | + { |
| 173 | + rescan_.drawButton(true); |
| 174 | + return; |
| 175 | + } |
| 176 | + if (resetDB_.justReleased()) |
| 177 | + { |
| 178 | + resetDB_.drawButton(false); |
| 179 | + healthScan_->ResetNodeDB(); |
| 180 | + } |
| 181 | + if (resetDB_.justPressed()) |
| 182 | + { |
| 183 | + resetDB_.drawButton(true); |
| 184 | + return; |
| 185 | + } |
| 186 | + nodelist_.CheckButtons(p.x, p.y); |
| 187 | +} |
| 188 | + |
| 189 | +DisplayNetworkHealth::ListBox::ListBox(Adafruit_GFX *gfx,int16_t x, int16_t y, |
| 190 | + uint8_t textSize, uint8_t items) |
| 191 | + : gfx_(gfx) |
| 192 | +, x_(x) |
| 193 | +, y_(y) |
| 194 | +, textSize_(textSize) |
| 195 | +, items_(items) |
| 196 | +, itemOffset_(0) |
| 197 | +, havePrev_(false) |
| 198 | +, haveNext_(false) |
| 199 | +{ |
| 200 | + uint16_t w_2 = gfx_->width()/2; |
| 201 | + prev_.initButtonUL(gfx_,x_,y_+(items_*(textSize_*8)),w_2,(textSize_*8), |
| 202 | + HX8357_WHITE,HX8357_BLACK,HX8357_MAGENTA, |
| 203 | + "Prev",textSize_); |
| 204 | + next_.initButtonUL(gfx_,x_+w_2,y_+(items_*(textSize_*8)),w_2, |
| 205 | + (textSize_*8),HX8357_WHITE,HX8357_BLACK,HX8357_MAGENTA, |
| 206 | + "Next",textSize_); |
| 207 | +} |
| 208 | +void DisplayNetworkHealth::ListBox::RedrawList( |
| 209 | + NetworkHealthScan::NodeDB_ConstIterator begin, |
| 210 | + NetworkHealthScan::NodeDB_ConstIterator end) |
| 211 | +{ |
| 212 | + begin_ = begin; |
| 213 | + end_ = end; |
| 214 | + RedrawList(0); |
| 215 | +} |
| 216 | + |
| 217 | +void DisplayNetworkHealth::ListBox::RedrawList(uint8_t itemOffset) |
| 218 | +{ |
| 219 | + itemOffset_ = itemOffset; |
| 220 | + RedrawList(); |
| 221 | +} |
| 222 | + |
| 223 | +void DisplayNetworkHealth::ListBox::RedrawList() |
| 224 | +{ |
| 225 | + gfx_->fillRect(x_,y_,gfx_->width(),(textSize_*8)*(items_+1),HX8357_BLACK); |
| 226 | + int16_t y=y_; |
| 227 | + gfx_->setTextSize(textSize_); |
| 228 | + gfx_->setTextColor(HX8357_CYAN,HX8357_BLACK); |
| 229 | + auto it = begin_; |
| 230 | + std::advance(it, itemOffset_); |
| 231 | + havePrev_ = itemOffset_ > 0; |
| 232 | + haveNext_ = std::distance(it,end_) > items_; |
| 233 | + for (uint8_t i = 0; i < items_; i++) |
| 234 | + { |
| 235 | + if (it == end_) break; |
| 236 | + string nodeidstring = utils::node_id_to_string(it->second.node_id); |
| 237 | + gfx_->setCursor(x_,y); |
| 238 | + gfx_->print(nodeidstring.c_str()); |
| 239 | + y += textSize_*8; |
| 240 | + it++; |
| 241 | + } |
| 242 | + if (havePrev_) prev_.drawButton(); |
| 243 | + if (haveNext_) next_.drawButton(); |
| 244 | +} |
| 245 | + |
| 246 | +void DisplayNetworkHealth::ListBox::CheckButtons(int16_t x, int16_t y) |
| 247 | +{ |
| 248 | + if (havePrev_) |
| 249 | + { |
| 250 | + if (prev_.contains(x,y)) |
| 251 | + { |
| 252 | + prev_.press(true); |
| 253 | + } |
| 254 | + else |
| 255 | + { |
| 256 | + prev_.press(false); |
| 257 | + } |
| 258 | + if (prev_.justReleased()) |
| 259 | + { |
| 260 | + prev_.drawButton(false); |
| 261 | + if (itemOffset_ >= items_) |
| 262 | + { |
| 263 | + RedrawList(itemOffset_-items_); |
| 264 | + } |
| 265 | + } |
| 266 | + if (prev_.justPressed()) |
| 267 | + { |
| 268 | + prev_.drawButton(true); |
| 269 | + return; |
| 270 | + } |
| 271 | + } |
| 272 | + if (haveNext_) |
| 273 | + { |
| 274 | + if (next_.contains(x,y)) |
| 275 | + { |
| 276 | + next_.press(true); |
| 277 | + } |
| 278 | + else |
| 279 | + { |
| 280 | + next_.press(false); |
| 281 | + } |
| 282 | + if (next_.justReleased()) |
| 283 | + { |
| 284 | + next_.drawButton(false); |
| 285 | + if (std::distance(begin_,end_) > itemOffset_+items_) |
| 286 | + { |
| 287 | + RedrawList(itemOffset_+items_); |
| 288 | + } |
| 289 | + } |
| 290 | + if (next_.justPressed()) |
| 291 | + { |
| 292 | + next_.drawButton(true); |
| 293 | + return; |
| 294 | + } |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +} |
0 commit comments