AceRoutine  1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
LogBinTableRenderer.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2022 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include <Arduino.h>
26 #include <AceCommon.h> // printPad5To()
27 #include "LogBinTableRenderer.h"
28 
29 namespace ace_routine {
30 namespace internal {
31 
32 // The numbers for higher duration values deviate from powers of 2 because 2^10
33 // = 1024, which is not exactly 1000. The length of each string should be 6 or
34 // smaller, otherwise it will be truncated by
35 // LogBinTableRenderer::printHeaderTo().
36 static const char kBinLabel00[] PROGMEM = "<2us";
37 static const char kBinLabel01[] PROGMEM = "<4us";
38 static const char kBinLabel02[] PROGMEM = "<8us";
39 static const char kBinLabel03[] PROGMEM = "<16us";
40 static const char kBinLabel04[] PROGMEM = "<32us";
41 static const char kBinLabel05[] PROGMEM = "<64us";
42 static const char kBinLabel06[] PROGMEM = "<128us";
43 static const char kBinLabel07[] PROGMEM = "<256us";
44 static const char kBinLabel08[] PROGMEM = "<512us";
45 static const char kBinLabel09[] PROGMEM = "<1ms";
46 static const char kBinLabel10[] PROGMEM = "<2ms";
47 static const char kBinLabel11[] PROGMEM = "<4ms";
48 static const char kBinLabel12[] PROGMEM = "<8ms";
49 static const char kBinLabel13[] PROGMEM = "<16ms";
50 static const char kBinLabel14[] PROGMEM = "<33ms";
51 static const char kBinLabel15[] PROGMEM = "<66ms";
52 static const char kBinLabel16[] PROGMEM = "<131ms";
53 static const char kBinLabel17[] PROGMEM = "<262ms";
54 static const char kBinLabel18[] PROGMEM = "<524ms";
55 static const char kBinLabel19[] PROGMEM = "<1s";
56 static const char kBinLabel20[] PROGMEM = "<2s";
57 static const char kBinLabel21[] PROGMEM = "<4s";
58 static const char kBinLabel22[] PROGMEM = "<8s";
59 static const char kBinLabel23[] PROGMEM = "<17s";
60 static const char kBinLabel24[] PROGMEM = "<34s";
61 static const char kBinLabel25[] PROGMEM = "<67s";
62 static const char kBinLabel26[] PROGMEM = "<134s";
63 static const char kBinLabel27[] PROGMEM = "<268s";
64 static const char kBinLabel28[] PROGMEM = "<537s";
65 static const char kBinLabel29[] PROGMEM = "<1074s";
66 static const char kBinLabel30[] PROGMEM = "<2147s";
67 static const char kBinLabel31[] PROGMEM = "<4295s";
68 
69 const char* const kBinLabels[kNumBinLabels] PROGMEM = {
70  kBinLabel00,
71  kBinLabel01,
72  kBinLabel02,
73  kBinLabel03,
74  kBinLabel04,
75  kBinLabel05,
76  kBinLabel06,
77  kBinLabel07,
78  kBinLabel08,
79  kBinLabel09,
80  kBinLabel10,
81  kBinLabel11,
82  kBinLabel12,
83  kBinLabel13,
84  kBinLabel14,
85  kBinLabel15,
86  kBinLabel16,
87  kBinLabel17,
88  kBinLabel18,
89  kBinLabel19,
90  kBinLabel20,
91  kBinLabel21,
92  kBinLabel22,
93  kBinLabel23,
94  kBinLabel24,
95  kBinLabel25,
96  kBinLabel26,
97  kBinLabel27,
98  kBinLabel28,
99  kBinLabel29,
100  kBinLabel30,
101  kBinLabel31,
102 };
103 
104 void printHeaderTo(Print& printer, uint8_t startBin, uint8_t endBin) {
105  endBin = (endBin > kNumBinLabels) ? kNumBinLabels : endBin;
106  if (endBin <= startBin) return; // needed if startBin = endBin = 0
107 
108  for (uint8_t i = startBin; i < endBin - 1; i++) {
109  auto* label = (const char*) pgm_read_ptr(&internal::kBinLabels[i]);
110  printPStringTo(printer, label, 6);
111  }
112  printer.print(F(" >>"));
113 }
114 
115 void printPStringTo(Print& printer, const char* s, uint8_t boxSize) {
116  uint8_t length = strlen_P(s);
117  uint8_t printLength;
118  if (length < boxSize) {
119  for (uint8_t i = 0; i < (boxSize - length); i++) {
120  printer.print(' ');
121  }
122  printLength = length;
123  } else {
124  printLength = boxSize;
125  }
126 
127  for (uint8_t i = 0; i < printLength; i++) {
128  printer.print((char) pgm_read_byte(&s[i]));
129  }
130 }
131 
132 void printBinsTo(
133  Print& printer,
134  const uint16_t bins[],
135  uint8_t numBins,
136  uint8_t startBin,
137  uint8_t endBin) {
138 
139  endBin = (endBin > numBins) ? numBins : endBin;
140  for (uint8_t i = startBin; i < endBin; i++) {
141  uint16_t count = bins[i];
142  printer.print(' ');
143  ace_common::printPad5To(printer, count);
144  }
145 }
146 
147 }
148 }