Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
usb_hid.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 16.09.2013 00:38:25 CEST
3 // File: usb_hid.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __USB_HID_HEADER
12 #define __USB_HID_HEADER
13 
14 #include <Arduino.h>
15 
16 #include <UsbKeyboardMod.h>
17 #include <device/hid_keys.hpp>
18 
19 namespace device {
20  class usb_hid_class {
21  public:
22  //------------------- ctors -------------------
24  data_[0] = 0;
25  data_[1] = 0;
26  data_[2] = 0;
27  data_[3] = 0;
28  data_[4] = 0;
29  data_.push_back(0); //mods
30  }
31  //------------------- connect -------------------
32  void connect() {
33  TIMSK0 &= !(1<<TOIE0);
34  cli();
35  usbDeviceDisconnect();
36  delay_ms(250);
37  usbDeviceConnect();
38  sei();
39 
40  for(int i = 0; i < 150; ++i) {
41  UsbKeyboard.update();
42  delay_ms(10);
43  }
44  }
45  //------------------- helper -------------------
46  void delay_ms(unsigned int const & ms) {
47  for(unsigned int i = 0; i < ms; i++) {
48  delayMicroseconds(1000);
49  }
50  }
51  //=================== press/release helper ===================
52  void set_key(uint8_t const & _key) {
53  if(data_.size() == data_.capacity())
54  data_.erase(1);
55  data_.push_back(_key);
56  //~ if(data_[4] == 0) {
57  //~ data_[4] = _key;
58  //~ return;
59  //~ }
60  //~ if(data_[3] == 0) {
61  //~ data_[3] = data_[4];
62  //~ data_[4] = _key;
63  //~ return;
64  //~ }
65  //~ if(data_[2] == 0) {
66  //~ data_[2] = data_[3];
67  //~ data_[3] = data_[4];
68  //~ data_[4] = _key;
69  //~ return;
70  //~ }
71  //~ if(data_[1] == 0) {
72  //~ data_[1] = data_[2];
73  //~ data_[2] = data_[3];
74  //~ data_[3] = data_[4];
75  //~ data_[4] = _key;
76  //~ return;
77  //~ }
78  }
79  void unset_key(uint8_t const & _key) {
80  data_.erase(data_.find(_key, 1));
81  //~ if(data_[1] == _key) {
82  //~ data_[1] = 0;
83  //~ return;
84  //~ }
85  //~ if(data_[2] == _key) {
86  //~ data_[2] = data_[1];
87  //~ data_[1] = 0;
88  //~ return;
89  //~ }
90  //~ if(data_[3] == _key) {
91  //~ data_[3] = data_[2];
92  //~ data_[2] = data_[1];
93  //~ data_[1] = 0;
94  //~ return;
95  //~ }
96  //~ if(data_[4] == _key) {
97  //~ data_[4] = data_[3];
98  //~ data_[3] = data_[2];
99  //~ data_[2] = data_[1];
100  //~ data_[1] = 0;
101  //~ return;
102  //~ }
103  }
104  void set_mod(uint8_t const & _mod) {
105  if(_mod == 0)
106  return;
107  if((_mod & 1) == 1) {
108  data_[0] |= _mod;
109  ++(mod_vec_[0]);
110  }
111  if((_mod & 2) == 2) {
112  data_[0] |= _mod;
113  ++(mod_vec_[1]);
114  }
115  if((_mod & 4) == 4) {
116  data_[0] |= _mod;
117  ++(mod_vec_[2]);
118  }
119  if((_mod & 8) == 8) {
120  data_[0] |= _mod;
121  ++(mod_vec_[3]);
122  }
123  }
124  void unset_mod(uint8_t const & _mod) {
125  if(_mod == 0)
126  return;
127 
128  if((_mod & 1) == 1) {
129  if(--(mod_vec_[0]) == 0)
130  data_[0] &= ~_mod;
131  }
132  if((_mod & 2) == 2) {
133  if(--(mod_vec_[1]) == 0)
134  data_[0] &= ~_mod;
135  }
136  if((_mod & 4) == 4) {
137  if(--(mod_vec_[2]) == 0)
138  data_[0] &= ~_mod;
139  }
140  if((_mod & 8) == 8) {
141  if(--(mod_vec_[3]) == 0)
142  data_[0] &= ~_mod;
143  }
144  }
145  //=================== press and release ===================
146  void press(uint8_t const & _key, uint8_t const & _mod = (uint8_t)key::none) {
147  set_key(_key);
148  set_mod(_mod);
149  //~ UsbKeyboard.sendBuffer(data_);
150  }
151  void release(uint8_t const & _key, uint8_t const & _mod = (uint8_t)key::none) {
152  unset_key(_key);
153  unset_mod(_mod);
154  //~ UsbKeyboard.sendBuffer(data_);
155  }
156  void press(key const & _key, key const & _mod = key::none) {
157  press((uint8_t)_key, (uint8_t)_mod);
158  }
159  void release(key const & _key, key const & _mod = key::none) {
160  release((uint8_t)_key, (uint8_t)_mod);
161  }
162  void release_all() {
163  data_[0] = 0;
164  data_[1] = 0;
165  data_[2] = 0;
166  data_[3] = 0;
167  mod_vec_.clear();
168 
169  //~ UsbKeyboard.sendBuffer(data_);
170  }
171  bool any_pressed() {
172  return (data_[0] + data_[1] + data_[2] + data_[3]);
173  }
174  constexpr static uint8_t buffer_size() {
175  return BUFFER_SIZE;
176  }
177  void print_buffer() {
178  for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
179  ustd::cout << data_[i] << ", ";
180  }
182  }
183  //------------------- update -------------------
184  void update() {
185  UsbKeyboard.sendBuffer(data_.data());
186  UsbKeyboard.update();
187  }
188  private:
189  //~ uint8_t data_[BUFFER_SIZE]; //first is mod, last three keys
190  ustd::static_vector<uint8_t, BUFFER_SIZE> data_; //first is mod, last three keys
191  ustd::array<uint8_t, 4> mod_vec_;
192  } ;
193 }//end namespace device
194 
195 #endif //__USB_HID_HEADER
bool any_pressed()
Definition: usb_hid.hpp:171
void press(key const &_key, key const &_mod=key::none)
Definition: usb_hid.hpp:156
void release_all()
Definition: usb_hid.hpp:162
void push_back(T const &in)
Definition: static_vector.hpp:40
void set_mod(uint8_t const &_mod)
Definition: usb_hid.hpp:104
void release(uint8_t const &_key, uint8_t const &_mod=(uint8_t) key::none)
Definition: usb_hid.hpp:151
usb_hid_class()
Definition: usb_hid.hpp:23
void connect()
Definition: usb_hid.hpp:32
size_type find(T const &val, size_type pos=0) const
Definition: static_vector.hpp:49
void delay_ms(unsigned int const &ms)
Definition: usb_hid.hpp:46
void press(uint8_t const &_key, uint8_t const &_mod=(uint8_t) key::none)
Definition: usb_hid.hpp:146
void unset_mod(uint8_t const &_mod)
Definition: usb_hid.hpp:124
constexpr size_type capacity()
Definition: static_vector.hpp:125
void erase(size_type const &pos)
Definition: static_vector.hpp:59
struct ustd::endl_class endl
void set_key(uint8_t const &_key)
Definition: usb_hid.hpp:52
static constexpr uint8_t buffer_size()
Definition: usb_hid.hpp:174
void update()
Definition: usb_hid.hpp:184
void print_buffer()
Definition: usb_hid.hpp:177
T * data()
Definition: static_vector.hpp:90
void release(key const &_key, key const &_mod=key::none)
Definition: usb_hid.hpp:159
key
Definition: hid_keys.hpp:14
size_type const & size() const
Definition: static_vector.hpp:118
void unset_key(uint8_t const &_key)
Definition: usb_hid.hpp:79
Definition: usb_hid.hpp:20
void clear()
set all elements to "zero"
Definition: array.hpp:138
ustd::cout_class cout