Skip to content

Commit 122eba0

Browse files
committed
Functions are replaced for mouse
1 parent 5149889 commit 122eba0

3 files changed

Lines changed: 18 additions & 262 deletions

File tree

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMouse/HidMouse.ino

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
HID Keyboard example
33
44
5-
created 2020
6-
by Deqing Sun for use with CH55xduino
5+
created 2021
6+
by betaEncoder for use with CH55xduino
77
88
This example code is in the public domain.
99
@@ -17,13 +17,7 @@
1717
#error "This example needs to be compiled with a USER USB setting"
1818
#endif
1919

20-
#include "src/userUsbHidKeyboard/USBHIDKeyboard.h"
21-
22-
#define BUTTON1_PIN 30
23-
#define BUTTON2_PIN 31
24-
#define BUTTON3_PIN 32
25-
26-
#define LED_BUILTIN 33
20+
#include "src/userUsbHidMouse/USBHIDMouse.h"
2721

2822
bool button1PressPrev = false;
2923
bool button2PressPrev = false;
@@ -35,53 +29,20 @@ void setup() {
3529
pinMode(BUTTON1_PIN, INPUT_PULLUP);
3630
pinMode(BUTTON2_PIN, INPUT_PULLUP);
3731
pinMode(BUTTON3_PIN, INPUT_PULLUP);
38-
pinMode(LED_BUILTIN, OUTPUT);
3932
}
4033

4134
void loop() {
42-
//button 1 is mapped to letter 'a'
35+
//button 1 is mapped to left click
4336
bool button1Press = !digitalRead(BUTTON1_PIN);
4437
if (button1PressPrev != button1Press) {
4538
button1PressPrev = button1Press;
4639
if (button1Press) {
47-
Keyboard_press('a');
40+
Mouse_press(BUTTON_LEFT);
4841
} else {
49-
Keyboard_release('a');
50-
}
51-
}
52-
53-
//button 2 is mapped to string 'hello'
54-
bool button2Press = !digitalRead(BUTTON2_PIN);
55-
if (button2PressPrev != button2Press) {
56-
button2PressPrev = button2Press;
57-
if (button2Press) {
58-
Keyboard_write('H');
59-
Keyboard_write('e');
60-
Keyboard_write('l');
61-
Keyboard_write('l');
62-
Keyboard_write('o');
42+
Mouse_release(BUTTON_LEFT);
6343
}
6444
}
6545

66-
//button 3 is mapped to Capslock
67-
bool button3Press = !digitalRead(BUTTON3_PIN);
68-
if (button3PressPrev != button3Press) {
69-
button3PressPrev = button3Press;
70-
if (button3Press) {
71-
Keyboard_press(KEY_CAPS_LOCK);
72-
delay(100); //a quick capslock press is not recognized on mac
73-
Keyboard_release(KEY_CAPS_LOCK);
74-
}
75-
}
76-
77-
//map capsLock to LED
78-
//Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana,
79-
if (Keyboard_getLEDStatus() & 2) {
80-
digitalWrite(LED_BUILTIN, HIGH);
81-
} else {
82-
digitalWrite(LED_BUILTIN, LOW);
83-
}
84-
8546
delay(50); //naive debouncing
8647

8748
}

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMouse/src/userUsbHidMouse/USBHIDMouse.c

Lines changed: 9 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -10,141 +10,7 @@ extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[];
1010

1111
volatile __xdata uint8_t UpPoint1_Busy = 0; //Flag of whether upload pointer is busy
1212

13-
__xdata uint8_t HIDKey[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
14-
15-
#define SHIFT 0x80
16-
__code uint8_t _asciimap[128] =
17-
{
18-
0x00, // NUL
19-
0x00, // SOH
20-
0x00, // STX
21-
0x00, // ETX
22-
0x00, // EOT
23-
0x00, // ENQ
24-
0x00, // ACK
25-
0x00, // BEL
26-
0x2a, // BS Backspace
27-
0x2b, // TAB Tab
28-
0x28, // LF Enter
29-
0x00, // VT
30-
0x00, // FF
31-
0x00, // CR
32-
0x00, // SO
33-
0x00, // SI
34-
0x00, // DEL
35-
0x00, // DC1
36-
0x00, // DC2
37-
0x00, // DC3
38-
0x00, // DC4
39-
0x00, // NAK
40-
0x00, // SYN
41-
0x00, // ETB
42-
0x00, // CAN
43-
0x00, // EM
44-
0x00, // SUB
45-
0x00, // ESC
46-
0x00, // FS
47-
0x00, // GS
48-
0x00, // RS
49-
0x00, // US
50-
51-
0x2c, // ' '
52-
0x1e|SHIFT, // !
53-
0x34|SHIFT, // "
54-
0x20|SHIFT, // #
55-
0x21|SHIFT, // $
56-
0x22|SHIFT, // %
57-
0x24|SHIFT, // &
58-
0x34, // '
59-
0x26|SHIFT, // (
60-
0x27|SHIFT, // )
61-
0x25|SHIFT, // *
62-
0x2e|SHIFT, // +
63-
0x36, // ,
64-
0x2d, // -
65-
0x37, // .
66-
0x38, // /
67-
0x27, // 0
68-
0x1e, // 1
69-
0x1f, // 2
70-
0x20, // 3
71-
0x21, // 4
72-
0x22, // 5
73-
0x23, // 6
74-
0x24, // 7
75-
0x25, // 8
76-
0x26, // 9
77-
0x33|SHIFT, // :
78-
0x33, // ;
79-
0x36|SHIFT, // <
80-
0x2e, // =
81-
0x37|SHIFT, // >
82-
0x38|SHIFT, // ?
83-
0x1f|SHIFT, // @
84-
0x04|SHIFT, // A
85-
0x05|SHIFT, // B
86-
0x06|SHIFT, // C
87-
0x07|SHIFT, // D
88-
0x08|SHIFT, // E
89-
0x09|SHIFT, // F
90-
0x0a|SHIFT, // G
91-
0x0b|SHIFT, // H
92-
0x0c|SHIFT, // I
93-
0x0d|SHIFT, // J
94-
0x0e|SHIFT, // K
95-
0x0f|SHIFT, // L
96-
0x10|SHIFT, // M
97-
0x11|SHIFT, // N
98-
0x12|SHIFT, // O
99-
0x13|SHIFT, // P
100-
0x14|SHIFT, // Q
101-
0x15|SHIFT, // R
102-
0x16|SHIFT, // S
103-
0x17|SHIFT, // T
104-
0x18|SHIFT, // U
105-
0x19|SHIFT, // V
106-
0x1a|SHIFT, // W
107-
0x1b|SHIFT, // X
108-
0x1c|SHIFT, // Y
109-
0x1d|SHIFT, // Z
110-
0x2f, // [
111-
0x31, // bslash
112-
0x30, // ]
113-
0x23|SHIFT, // ^
114-
0x2d|SHIFT, // _
115-
0x35, // `
116-
0x04, // a
117-
0x05, // b
118-
0x06, // c
119-
0x07, // d
120-
0x08, // e
121-
0x09, // f
122-
0x0a, // g
123-
0x0b, // h
124-
0x0c, // i
125-
0x0d, // j
126-
0x0e, // k
127-
0x0f, // l
128-
0x10, // m
129-
0x11, // n
130-
0x12, // o
131-
0x13, // p
132-
0x14, // q
133-
0x15, // r
134-
0x16, // s
135-
0x17, // t
136-
0x18, // u
137-
0x19, // v
138-
0x1a, // w
139-
0x1b, // x
140-
0x1c, // y
141-
0x1d, // z
142-
0x2f|SHIFT, // {
143-
0x31|SHIFT, // |
144-
0x30|SHIFT, // }
145-
0x35|SHIFT, // ~
146-
0 // DEL
147-
};
13+
__xdata uint8_t HIDRep[4] = {0x0,0x0,0x0,0x0};
14814

14915
typedef void( *pTaskFn)( void );
15016

@@ -185,100 +51,33 @@ uint8_t USB_EP1_send(){
18551
if (waitWriteCount>=50000) return 0;
18652
}
18753

188-
for (uint8_t i=0;i<sizeof(HIDKey);i++){ //load data for upload
189-
Ep1Buffer[64+i] = HIDKey[i];
54+
for (uint8_t i=0;i<sizeof(HIDRep);i++){ //load data for upload
55+
Ep1Buffer[64+i] = HIDRep[i];
19056
}
19157

192-
UEP1_T_LEN = sizeof(HIDKey); //data length
58+
UEP1_T_LEN = sizeof(HIDRep); //data length
19359
UpPoint1_Busy = 1;
19460
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //upload data and respond ACK
19561

19662
return 1;
19763
}
19864

199-
uint8_t Keyboard_press(uint8_t k) {
65+
uint8_t Mouse_press(uint8_t k) {
20066
uint8_t i;
201-
if (k >= 136) { // it's a non-printing key (not a modifier)
202-
k = k - 136;
203-
} else if (k >= 128) { // it's a modifier key
204-
HIDKey[0] |= (1<<(k-128));
205-
k = 0;
206-
} else { // it's a printing key
207-
k = _asciimap[k];
208-
if (!k) {
209-
//setWriteError();
210-
return 0;
211-
}
212-
if (k & 0x80) { // it's a capital letter or other character reached with shift
213-
HIDKey[0] |= 0x02; // the left shift modifier
214-
k &= 0x7F;
215-
}
216-
}
217-
218-
// Add k to the key report only if it's not already present
219-
// and if there is an empty slot.
220-
if (HIDKey[2] != k && HIDKey[3] != k &&
221-
HIDKey[4] != k && HIDKey[5] != k &&
222-
HIDKey[6] != k && HIDKey[7] != k) {
223-
224-
for (i=2; i<8; i++) {
225-
if (HIDKey[i] == 0x00) {
226-
HIDKey[i] = k;
227-
break;
228-
}
229-
}
230-
if (i == 8) {
231-
//setWriteError();
232-
return 0;
233-
}
234-
}
67+
HIDRep[0] |= k;
23568
USB_EP1_send();
23669
return 1;
23770
}
23871

23972
uint8_t Keyboard_release(uint8_t k) {
24073
uint8_t i;
241-
if (k >= 136) { // it's a non-printing key (not a modifier)
242-
k = k - 136;
243-
} else if (k >= 128) { // it's a modifier key
244-
HIDKey[0] &= ~(1<<(k-128));
245-
k = 0;
246-
} else { // it's a printing key
247-
k = _asciimap[k];
248-
if (!k) {
249-
return 0;
250-
}
251-
if (k & 0x80) { // it's a capital letter or other character reached with shift
252-
HIDKey[0] &= ~(0x02); // the left shift modifier
253-
k &= 0x7F;
254-
}
255-
}
256-
257-
// Test the key report to see if k is present. Clear it if it exists.
258-
// Check all positions in case the key is present more than once (which it shouldn't be)
259-
for (i=2; i<8; i++) {
260-
if (0 != k && HIDKey[i] == k) {
261-
HIDKey[i] = 0x00;
262-
}
263-
}
74+
HIDRep[0] &= ~k;
26475

26576
USB_EP1_send();
26677
return 1;
26778
}
26879

269-
void Keyboard_releaseAll(void){
270-
for (uint8_t i=0;i<sizeof(HIDKey);i++){ //load data for upload
271-
HIDKey[i] = 0;
272-
}
80+
void Mouse_releaseAll(void){
81+
HIDRep[0] = 0;
27382
USB_EP1_send();
27483
}
275-
276-
uint8_t Keyboard_write(uint8_t c){
277-
uint8_t p = Keyboard_press(c); // Keydown
278-
Keyboard_release(c); // Keyup
279-
return p; // just return the result of press() since release() almost always returns 1
280-
}
281-
282-
uint8_t Keyboard_getLEDStatus(){
283-
return Ep1Buffer[0]; //The only info we gets
284-
}

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidMouse/src/userUsbHidMouse/USBHIDMouse.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ extern "C" {
1616

1717
void USBInit(void);
1818

19-
uint8_t Keyboard_press(uint8_t k);
20-
uint8_t Keyboard_release(uint8_t k);
21-
void Keyboard_releaseAll(void);
22-
23-
uint8_t Keyboard_write(uint8_t c);
24-
25-
uint8_t Keyboard_getLEDStatus();
19+
uint8_t Mouse_press(uint8_t k);
20+
uint8_t Mouse_release(uint8_t k);
21+
void Mouse_releaseAll(void);
2622

2723
#ifdef __cplusplus
2824
} // extern "C"

0 commit comments

Comments
 (0)