aboutsummaryrefslogtreecommitdiffstats
path: root/samd/samd.ino
blob: a09cb5ed16d3ea1191ffe85bda8eda39391e3c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// -*- mode: c++ -*-

#include "i2c_message.h"

#include <hidboot.h>
#include <usbhub.h>
#include <Wire.h>

#ifdef ADAFRUIT_TRINKET_M0
#include <Adafruit_DotStar.h>

const uint8_t NUMPIXELS = 1;
const uint8_t DATAPIN = 7;
const uint8_t CLOCKPIN = 8;

Adafruit_DotStar px(NUMPIXELS, DATAPIN, CLOCKPIN);


void initTXLED() {
}

void turnOffTXLED() {
  px.clear();
  px.show();
}

void turnOnTXLED() {
  px.setPixelColor(0, 63, 63, 0);
  px.show();
}
#else // ADAFRUIT_TRINKET_M0
void initTXLED() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void turnOffTXLED() {
  digitalWrite(LED_BUILTIN, HIGH);
}

void turnOnTXLED() {
  digitalWrite(LED_BUILTIN, LOW);
}
#endif // ADAFRUIT_TRINKET_M0

void dbg(char *msg) {
  uint8_t len = strlen(msg);

  Wire.beginTransmission(BTLE_WIREADDR);
  Wire.write(DBG);
  Wire.write(len);
  Wire.write(msg);
  Wire.endTransmission();
}

class KeyboardRaw: public KeyboardReportParser {
public:
  void Parse(HID *hid, uint32_t is_rpt_id, uint32_t len, uint8_t *buf);
};

void KeyboardRaw::Parse(HID *hid, uint32_t is_rpt_id, uint32_t len, uint8_t *buf) {
  turnOnTXLED();

  KeyboardReportParser::Parse(hid, is_rpt_id, len, buf);

  Wire.beginTransmission(BTLE_WIREADDR);
  Wire.write(KBD);
  Wire.write(len);
  Wire.write(buf, len);
  Wire.endTransmission();

  turnOffTXLED();
}

USBHost usb;
USBHub hub(&usb);
HIDBoot<HID_PROTOCOL_KEYBOARD> hidKeyBoard(&usb);
KeyboardRaw parser;

void setup() {
  initTXLED();
  turnOffTXLED();

  Wire.begin();

  dbg("Booting\r\n");

  if (usb.Init()) {
    dbg("USB Host did not start!\r\n");
    while (true) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(125);
      digitalWrite(LED_BUILTIN, LOW);
      delay(125);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(125);
      digitalWrite(LED_BUILTIN, LOW);
      delay(125);

      delay(500);
    }
  }

  hidKeyBoard.SetReportParser(0, &parser);
}

void loop() {
  asm("wfe \n");
  usb.Task();
}