Numworks Epsilon  1.4.1
Graphing Calculator Operating System
apps_container.cpp
Go to the documentation of this file.
1 #include "apps_container.h"
2 #include "global_preferences.h"
3 #include <ion.h>
4 
5 extern "C" {
6 #include <assert.h>
7 }
8 
9 using namespace Shared;
10 
12  Container(),
13  m_window(),
14  m_emptyBatteryWindow(),
15  m_globalContext(),
16  m_variableBoxController(&m_globalContext),
17  m_examPopUpController(this),
18  m_updateController(),
19  m_ledTimer(LedTimer()),
20  m_batteryTimer(BatteryTimer(this)),
21  m_suspendTimer(SuspendTimer(this)),
22  m_backlightDimmingTimer(),
23  m_homeSnapshot(),
24  m_onBoardingSnapshot(),
25  m_hardwareTestSnapshot(),
26  m_usbConnectedSnapshot()
27 {
28  m_emptyBatteryWindow.setFrame(KDRect(0, 0, Ion::Display::Width, Ion::Display::Height));
30 }
31 
34  return state.keyDown(Ion::Keyboard::Key::A6);
35 }
36 
38  return &m_hardwareTestSnapshot;
39 }
40 
42  return &m_onBoardingSnapshot;
43 }
44 
46  return &m_usbConnectedSnapshot;
47 }
48 
51  for (int i = 0; i < numberOfApps(); i++) {
53  }
54 }
55 
57  return &m_globalContext;
58 }
59 
61  return &m_mathToolbox;
62 }
63 
65  return &m_variableBoxController;
66 }
67 
68 void AppsContainer::suspend(bool checkIfPowerKeyReleased) {
69  resetShiftAlphaStatus();
70 #if EPSILON_SOFTWARE_UPDATE_PROMPT
71  if (activeApp()->snapshot()!= onBoardingAppSnapshot() && GlobalPreferences::sharedGlobalPreferences()->showUpdatePopUp()) {
72  activeApp()->displayModalViewController(&m_updateController, 0.f, 0.f);
73  }
74 #endif
75  Ion::Power::suspend(checkIfPowerKeyReleased);
76  /* Ion::Power::suspend() completely shuts down the LCD controller. Therefore
77  * the frame memory is lost. That's why we need to force a window redraw
78  * upon wakeup, otherwise the screen is filled with noise. */
80  m_backlightDimmingTimer.reset();
81  window()->redraw(true);
82 }
83 
85  bool alphaLockWantsRedraw = updateAlphaLock();
86 
87  bool didProcessEvent = false;
88 
89  if (event == Ion::Events::USBPlug) {
90  if (Ion::USB::isPlugged()) {
92  displayExamModePopUp(false);
93  } else {
95  }
97  } else {
99  }
100  didProcessEvent = true;
101  } else if (event == Ion::Events::USBEnumeration) {
102  if (Ion::USB::isPlugged()) {
103  App::Snapshot * activeSnapshot = (activeApp() == nullptr ? appSnapshotAtIndex(0) : activeApp()->snapshot());
105  Ion::USB::DFU();
106  switchTo(activeSnapshot);
107  didProcessEvent = true;
108  } else {
109  /* Sometimes, the device gets an ENUMDNE interrupts when being unplugged
110  * from a non-USB communicating host (e.g. a USB charger). The interrupt
111  * must me cleared: if not the next enumeration attempts will not be
112  * detected. */
114  }
115  } else {
116  didProcessEvent = Container::dispatchEvent(event);
117  }
118 
119  if (!didProcessEvent) {
120  didProcessEvent = processEvent(event);
121  }
122  if (event.isKeyboardEvent()) {
123  m_backlightDimmingTimer.reset();
124  m_suspendTimer.reset();
126  }
127  if (!didProcessEvent && alphaLockWantsRedraw) {
128  window()->redraw();
129  return true;
130  }
131  return didProcessEvent || alphaLockWantsRedraw;
132 }
133 
134 bool AppsContainer::processEvent(Ion::Events::Event event) {
135  if (event == Ion::Events::Home || event == Ion::Events::Back) {
137  return true;
138  }
139  if (event == Ion::Events::OnOff) {
140  suspend(true);
141  return true;
142  }
143  return false;
144 }
145 
147  if (activeApp() && snapshot != activeApp()->snapshot()) {
148  resetShiftAlphaStatus();
149  }
150  if (snapshot == hardwareTestAppSnapshot() || snapshot == onBoardingAppSnapshot()) {
151  m_window.hideTitleBarView(true);
152  } else {
153  m_window.hideTitleBarView(false);
154  }
155  if (snapshot) {
156  m_window.setTitle(snapshot->descriptor()->upperName());
157  }
158  Container::switchTo(snapshot);
159 }
160 
164 #if EPSILON_ONBOARDING_APP
166 #else
167  if (numberOfApps() == 2) {
169  } else {
171  }
172 #endif
173  Container::run();
174  switchTo(nullptr);
175 }
176 
178  if (m_window.updateBatteryLevel() ||
179  m_window.updateIsChargingState() ||
180  m_window.updatePluggedState()) {
181  return true;
182  }
183  return false;
184 }
185 
187  m_window.refreshPreferences();
188 }
189 
191  m_examPopUpController.setActivatingExamMode(activate);
193 }
194 
197  m_emptyBatteryWindow.redraw(true);
198  Ion::msleep(3000);
200  }
201  window()->redraw(true);
202 }
203 
206  updateAlphaLock();
207 }
208 
209 bool AppsContainer::updateAlphaLock() {
210  return m_window.updateAlphaLock();
211 }
212 
214  return &m_updateController;
215 }
216 
218  m_window.redraw();
219 }
220 
222  if (Ion::USB::isPlugged()) {
224  }
225 }
226 
227 Window * AppsContainer::window() {
228  return &m_window;
229 }
230 
231 int AppsContainer::numberOfContainerTimers() {
233 }
234 
235 Timer * AppsContainer::containerTimerAtIndex(int i) {
236  Timer * timers[4] = {&m_batteryTimer, &m_suspendTimer, &m_backlightDimmingTimer, &m_ledTimer};
237  return timers[i];
238 }
239 
240 void AppsContainer::resetShiftAlphaStatus() {
242  updateAlphaLock();
243 }
bool updateBatteryLevel()
Definition: apps_window.cpp:18
Charge level()
Definition: battery.cpp:20
bool isPlugged()
Definition: usb.cpp:12
void setTitle(I18n::Message title)
Definition: apps_window.cpp:14
virtual void switchTo(App::Snapshot *snapshot)
Definition: container.cpp:16
void displayExamModePopUp(bool activate)
Definition: timer.h:15
void displayModalViewController(ViewController *vc, float verticalAlignment, float horizontalAlignment, KDCoordinate topMargin=0, KDCoordinate leftMargin=0, KDCoordinate bottomMargin=0, KDCoordinate rightMargin=0)
Definition: app.cpp:85
ShiftAlphaStatus
Definition: events.h:41
Definition: window.h:6
virtual App::Snapshot * appSnapshotAtIndex(int index)=0
void shutdownDueToLowBattery()
void setFrame(KDRect frame)
Definition: view.cpp:125
constexpr int Width
Definition: display.h:26
void switchTo(App::Snapshot *snapshot) override
constexpr Event Home
Definition: events.h:68
Snapshot * snapshot()
Definition: app.cpp:41
void msleep(long ms)
Definition: ion.cpp:4
void setBrightness(uint8_t b)
Definition: backlight.cpp:16
virtual bool dispatchEvent(Ion::Events::Event event) override
bool updatePluggedState()
Definition: apps_window.cpp:26
constexpr Event Back
Definition: events.h:66
static void setCircuitBreaker(CircuitBreaker cb)
Definition: expression.cpp:61
void clearEnumerationInterrupt()
Definition: usb.cpp:23
VariableBoxController * variableBoxController()
void reset()
Definition: clipboard.cpp:17
OnBoarding::UpdateController * updatePopUpController()
void setShiftAlphaStatus(Ion::Events::ShiftAlphaStatus newStatus)
virtual void reset()
Definition: app.cpp:24
App::Snapshot * hardwareTestAppSnapshot()
virtual void run()
Definition: container.cpp:46
void suspend(bool checkIfPowerKeyReleased=false)
App::Snapshot * onBoardingAppSnapshot()
void DFU()
Poincare::Context * globalContext()
bool keyDown(Key k) const
Definition: keyboard.h:47
void enable()
Definition: usb.cpp:27
void redraw(bool force=false)
Definition: window.cpp:12
constexpr Event USBPlug
Definition: events.h:219
bool updateIsChargingState()
Definition: apps_window.cpp:22
App::Snapshot * usbConnectedAppSnapshot()
bool updateBatteryState()
bool isKeyboardEvent() const
Definition: events.h:33
void examDeactivatingPopUpIsDismissed() override
void refreshPreferences()
Definition: apps_window.cpp:30
void setShiftAlphaStatus(ShiftAlphaStatus s)
void disable()
Definition: usb.cpp:32
void run() override
virtual int numberOfApps()=0
MathToolbox * mathToolbox()
static constexpr KDCoordinate ExamPopUpBottomMargin
Definition: metric.h:25
bool updateAlphaLock()
Definition: apps_window.cpp:34
constexpr uint8_t MaxBrightness
Definition: backlight.h:9
constexpr Event OnOff
Definition: events.h:69
State scan()
Definition: keyboard.cpp:50
Definition: rect.h:26
virtual I18n::Message upperName()
Definition: app.cpp:11
virtual Descriptor * descriptor()=0
ExamMode examMode() const
virtual bool dispatchEvent(Ion::Events::Event event) override
Definition: container.cpp:38
constexpr Event USBEnumeration
Definition: events.h:218
static Clipboard * sharedClipboard()
Definition: clipboard.cpp:5
void refreshPreferences()
static constexpr KDCoordinate PopUpRightMargin
Definition: metric.h:22
constexpr int Height
Definition: display.h:27
static bool poincareCircuitBreaker()
void reset()
Definition: timer.cpp:19
App * activeApp()
Definition: container.cpp:34
void setActivatingExamMode(bool activingExamMode)
static GlobalPreferences * sharedGlobalPreferences()
void hideTitleBarView(bool hide)
Definition: apps_window.cpp:38
static constexpr KDCoordinate ExamPopUpTopMargin
Definition: metric.h:24
void suspend(bool checkIfPowerKeyReleased=false)
Definition: power.cpp:11
static constexpr KDCoordinate PopUpLeftMargin
Definition: metric.h:21