Skip to content

Commit 5cb44f2

Browse files
Levi--GAymane-ST
authored andcommitted
feat(usb): add PluggableUSB examples for JoystickMouse and SerialUSB
Signed-off-by: Aymane Bahssain <aymane.bahssain@st.com>
1 parent a7e7cb0 commit 5cb44f2

File tree

2 files changed

+90
-0
lines changed
  • libraries/USBDevice/examples

2 files changed

+90
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <Arduino.h>
2+
#include "Mouse.h"
3+
#include "Joystick.h"
4+
5+
#define LED PC13
6+
#define HIGHPIN A7
7+
#define ADCPIN A6
8+
#define LOWPIN A5
9+
10+
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
11+
JOYSTICK_TYPE_JOYSTICK, 0, 0,
12+
true, false, false, false, false, false,
13+
false, false, false, false, false);
14+
15+
int analogvalue = 0;
16+
17+
void setup()
18+
{
19+
Joystick.setXAxisRange(0, 255);
20+
Joystick.begin();
21+
Mouse.begin();
22+
USB_Begin();
23+
pinMode(HIGHPIN, OUTPUT);
24+
pinMode(ADCPIN, INPUT);
25+
pinMode(LOWPIN, OUTPUT);
26+
digitalWrite(HIGHPIN, HIGH);
27+
digitalWrite(LOWPIN, LOW);
28+
pinMode(LED, OUTPUT);
29+
}
30+
31+
bool high = false;
32+
int loops = 0;
33+
34+
void loop()
35+
{
36+
auto last = analogvalue;
37+
auto val = analogRead(ADCPIN);
38+
analogvalue <<= 1;
39+
analogvalue += val;
40+
analogvalue /= 3;
41+
if (analogvalue >> 2 != last >> 2)
42+
{
43+
Joystick.setXAxis(analogvalue >> 2);
44+
}
45+
delay(1);
46+
loops++;
47+
if (loops >= 500)
48+
{
49+
// every 500ms
50+
loops = 0;
51+
high = !high;
52+
digitalWrite(LED, high ? HIGH : LOW);
53+
if (high)
54+
{
55+
Mouse.move(10, 0);
56+
}
57+
else
58+
{
59+
Mouse.move(-10, 0);
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <Arduino.h>
2+
#include "USBCDC.h"
3+
4+
USBCDC USBSerial;
5+
6+
void setup()
7+
{
8+
USBSerial.begin(115200);
9+
USB_Begin();
10+
while (!USB_Running())
11+
{
12+
// wait until usb connected
13+
delay(5);
14+
}
15+
while (!USBSerial)
16+
{
17+
//(optional) wait until Serial port is connected
18+
delay(5);
19+
}
20+
}
21+
22+
void loop()
23+
{
24+
if (USBSerial.available())
25+
{
26+
USBSerial.print((char)USBSerial.read());
27+
}
28+
}

0 commit comments

Comments
 (0)