Numworks Epsilon  1.4.1
Graphing Calculator Operating System
calculator.cpp
Go to the documentation of this file.
1 #include "calculator.h"
2 #include "../regs/regs.h"
3 #include "../keyboard.h"
4 #include <ion/usb.h>
5 #include "../device.h"
6 
7 namespace Ion {
8 namespace USB {
9 namespace Device {
10 
11 void Calculator::PollAndReset(bool exitWithKeyboard) {
12  char serialNumber[Ion::SerialNumberLength+1];
13  Ion::getSerialNumber(serialNumber);
14  Calculator c(serialNumber);
15 
16  /* Leave DFU mode if the Back key is pressed, the calculator unplugged or the
17  * USB core soft-disconnected. */
19  uint8_t exitKeyRow = Ion::Keyboard::Device::rowForKey(exitKey);
20  uint8_t exitKeyColumn = Ion::Keyboard::Device::columnForKey(exitKey);
21 
23 
24  while (!(exitWithKeyboard && Ion::Keyboard::Device::columnIsActive(exitKeyColumn)) &&
26  !c.isSoftDisconnected()) {
27  c.poll();
28  }
29  if (!c.isSoftDisconnected()) {
30  c.detach();
31  }
32  if (c.resetOnDisconnect()) {
33  /* We don't perform a core reset because at this point in time the USB cable
34  * is most likely plugged in. Doing a full core reset would be the clean
35  * thing to do but would therefore result in the device entering the ROMed
36  * DFU bootloader, which we want to avoid. By performing a jump-reset, we
37  * will enter the newly flashed firmware. */
39  }
40 }
41 
43  /* Special case: Microsoft OS String Descriptor should be returned when
44  * searching for string descriptor at index 0xEE. */
45  if (type == m_microsoftOSStringDescriptor.type() && index == 0xEE) {
46  return &m_microsoftOSStringDescriptor;
47  }
48  int typeCount = 0;
49  for (size_t i=0; i<sizeof(m_descriptors)/sizeof(m_descriptors[0]); i++) {
50  Descriptor * descriptor = m_descriptors[i];
51  if (descriptor->type() != type) {
52  continue;
53  }
54  if (typeCount == index) {
55  return descriptor;
56  } else {
57  typeCount++;
58  }
59  }
60  return nullptr;
61 }
62 
63 bool Calculator::processSetupInRequest(SetupPacket * request, uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength) {
64  if (Device::processSetupInRequest(request, transferBuffer, transferBufferLength, transferBufferMaxLength)) {
65  return true;
66  }
68  if (request->bRequest() == k_webUSBVendorCode && request->wIndex() == 2) {
69  // This is a WebUSB, GET_URL request
70  assert(request->wValue() == k_webUSBLandingPageIndex);
71  return getURLCommand(transferBuffer, transferBufferLength, transferBufferMaxLength);
72  }
73  if (request->bRequest() == k_microsoftOSVendorCode && request->wIndex() == 0x0004) {
74  // This is a Microsoft OS descriptor, Extended Compat ID request
75  assert(request->wValue() == 0);
76  return getExtendedCompatIDCommand(transferBuffer, transferBufferLength, transferBufferMaxLength);
77  }
78  }
79  return false;
80 }
81 
82 bool Calculator::getURLCommand(uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength) {
83  *transferBufferLength = m_workshopURLDescriptor.copy(transferBuffer, transferBufferMaxLength);
84  return true;
85 }
86 
87 bool Calculator::getExtendedCompatIDCommand(uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength) {
88  *transferBufferLength = m_extendedCompatIdDescriptor.copy(transferBuffer, transferBufferMaxLength);
89  return true;
90 }
91 
92 }
93 }
94 }
bool isPlugged()
Definition: usb.cpp:12
#define assert(e)
Definition: assert.h:9
bool columnIsActive(uint8_t column)
Definition: keyboard.h:62
unsigned short uint16_t
Definition: stdint.h:5
unsigned char uint8_t
Definition: stdint.h:4
virtual Descriptor * descriptor(uint8_t type, uint8_t index) override
Definition: calculator.cpp:42
void jumpReset()
Definition: device.cpp:104
uint16_t copy(void *target, size_t maxSize) const
Definition: streamable.cpp:7
c(generic_all_nodes)
uint8_t columnForKey(Key key)
Definition: keyboard.h:45
void activateRow(uint8_t row)
Definition: keyboard.h:49
Definition: app.cpp:5
bool processSetupInRequest(SetupPacket *request, uint8_t *transferBuffer, uint16_t *transferBufferLength, uint16_t transferBufferMaxLength) override
Definition: calculator.cpp:63
void getSerialNumber(char *buffer)
Definition: device.cpp:77
bool processSetupInRequest(SetupPacket *request, uint8_t *transferBuffer, uint16_t *transferBufferLength, uint16_t transferBufferMaxLength) override
Definition: device.cpp:90
RequestType requestType() const
static void PollAndReset(bool exitWithKeyboard)
Definition: calculator.cpp:11
Definition: backlight.h:6
uint8_t rowForKey(Key key)
Definition: keyboard.h:42