Numworks Epsilon  1.4.1
Graphing Calculator Operating System
battery_test_controller.cpp
Go to the documentation of this file.
2 #include "../constant.h"
3 #include "app.h"
4 extern "C" {
5 #include <assert.h>
6 }
7 #include <poincare.h>
8 
9 using namespace Poincare;
10 
11 namespace HardwareTest {
12 
13 BatteryTestController::BatteryTestController(Responder * parentResponder) :
14  ViewController(parentResponder),
15  m_view()
16 {
17 }
18 
20  return &m_view;
21 }
22 
24  if (event == Ion::Events::OK) {
25  if (strcmp(m_view.batteryStateTextView()->text(), k_batteryOKText) == 0) {
27  App * a = (App *)app();
28  modal->displayModalViewController(a->USBController(), 0.0f, 0.0f);
29  }
30  }
31  updateBatteryState(Ion::Battery::voltage(), Ion::Battery::isCharging());
32  return true;
33 }
34 
36  const char * text = Ion::Battery::voltage() < k_batteryThreshold ? k_batteryNeedChargingText : k_batteryOKText;
37  KDColor color = Ion::Battery::voltage() < k_batteryThreshold ? KDColorRed : KDColorGreen;
38  m_view.setColor(color);
39  m_view.batteryStateTextView()->setText(text);
40  updateBatteryState(Ion::Battery::voltage(), Ion::Battery::isCharging());
41 }
42 
43 void BatteryTestController::updateBatteryState(float batteryLevel, bool batteryCharging) {
44  char bufferLevel[ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
45  const char * legend = "Battery level: ";
46  int legendLength = strlen(legend);
47  strlcpy(bufferLevel, legend, legendLength+1);
48  PrintFloat::convertFloatToText<float>(batteryLevel, bufferLevel+legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
49  m_view.batteryLevelTextView()->setText(bufferLevel);
50 
51  char bufferCharging[ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
52  int numberOfChars = 0;
53  legend = "Battery charging: ";
54  legendLength = strlen(legend);
55  strlcpy(bufferCharging, legend, legendLength+1);
56  numberOfChars += legendLength;
57  legend = "no";
58  if (batteryCharging) {
59  legend = "yes";
60  }
61  legendLength = strlen(legend);
62  strlcpy(bufferCharging+numberOfChars, legend, legendLength+1);
63  numberOfChars += legendLength;
64  bufferCharging[numberOfChars] = 0;
65  m_view.batteryChargingTextView()->setText(bufferCharging);
66 }
67 
68 BatteryTestController::ContentView::ContentView() :
70  m_batteryStateView(KDText::FontSize::Large),
71  m_batteryLevelView(KDText::FontSize::Small),
72  m_batteryChargingView(KDText::FontSize::Small)
73 {
74 }
75 
76 BufferTextView * BatteryTestController::ContentView::batteryStateTextView() {
77  return &m_batteryStateView;
78 }
79 
80 BufferTextView * BatteryTestController::ContentView::batteryLevelTextView() {
81  return &m_batteryLevelView;
82 }
83 
84 BufferTextView * BatteryTestController::ContentView::batteryChargingTextView() {
85  return &m_batteryChargingView;
86 }
87 
90  m_batteryStateView.setBackgroundColor(color);
91  m_batteryLevelView.setBackgroundColor(color);
92  m_batteryChargingView.setBackgroundColor(color);
93 }
94 
95 void BatteryTestController::ContentView::layoutSubviews() {
96  m_batteryStateView.setFrame(KDRect(0, 0, Ion::Display::Width, Ion::Display::Height/2));
98  m_batteryLevelView.setFrame(KDRect(0, Ion::Display::Height-2*textSize.height(), Ion::Display::Width, textSize.height()));
99  m_batteryChargingView.setFrame(KDRect(0, Ion::Display::Height-textSize.height(), Ion::Display::Width, textSize.height()));
100 }
101 
102 int BatteryTestController::ContentView::numberOfSubviews() const {
103  return 3;
104 }
105 
106 View * BatteryTestController::ContentView::subviewAtIndex(int index) {
107  if (index == 0) {
108  return &m_batteryStateView;
109  }
110  if (index == 1) {
111  return &m_batteryLevelView;
112  }
113  return &m_batteryChargingView;
114 }
115 
116 }
117 
constexpr int Width
Definition: display.h:26
bool isCharging()
Definition: battery.cpp:16
Responder * parentResponder() const
Definition: responder.cpp:12
bool handleEvent(Ion::Events::Event event) override
size_t strlcpy(char *dst, const char *src, size_t len)
Definition: strlcpy.c:3
Definition: text.h:8
Definition: size.h:6
constexpr KDColor KDColorWhite
Definition: color.h:42
size_t strlen(const char *s)
Definition: strlen.c:3
static constexpr int LargeNumberOfSignificantDigits
Definition: constant.h:6
float voltage()
Definition: battery.cpp:33
void setColor(KDColor c)
Definition: led.cpp:8
Definition: rect.h:26
Definition: color.h:6
void displayModalViewController(ViewController *vc, float verticalAlignment, float horizontalAlignment, KDCoordinate topMargin=0, KDCoordinate leftMargin=0, KDCoordinate bottomMargin=0, KDCoordinate rightMargin=0)
constexpr KDColor KDColorRed
Definition: color.h:43
Definition: view.h:23
constexpr KDColor KDColorGreen
Definition: color.h:44
static constexpr KDSize charSize(FontSize size=FontSize::Large)
Definition: text.h:16
virtual void setColor(KDColor color)
int strcmp(const char *s1, const char *s2)
Definition: strcmp.c:3
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
constexpr int Height
Definition: display.h:27
ViewController * USBController()
Definition: app.cpp:26
constexpr KDCoordinate height() const
Definition: size.h:11