Skip to content

Commit 61c293a

Browse files
committed
More examples
1 parent 3f70cbc commit 61c293a

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// Lists the capabilities of the connected power supply.
11+
// Please see https://github.com/manuelbl/usb-pd-arduino/wiki for
12+
// instructions how to wire your specific board to a USB C connector.
13+
// For this sketch, "Sink Mode" is relevant.
14+
//
15+
16+
#include "USBPowerDelivery.h"
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
PowerSink.start(handleEvent);
21+
}
22+
23+
void loop() {
24+
PowerSink.poll();
25+
}
26+
27+
void handleEvent(PDSinkEventType eventType) {
28+
if (eventType == PDSinkEventType::sourceCapabilitiesChanged && PowerSink.isConnected())
29+
listCapabilities();
30+
}
31+
32+
void listCapabilities() {
33+
Serial.println("USB PD capabilities:");
34+
Serial.println("__Type_________Vmin____Vmax____Imax");
35+
36+
for (int i = 0; i < PowerSink.numSourceCapabilities; i += 1) {
37+
auto cap = PowerSink.sourceCapabilities[i];
38+
Serial.printf(" %-9s %6d %6d %6d", getSupplyTypeName(cap.supplyType), cap.minVoltage, cap.maxVoltage, cap.maxCurrent);
39+
Serial.println();
40+
}
41+
42+
Serial.println("(voltage in mV, current in mA)");
43+
Serial.println();
44+
}
45+
46+
static const char* const SupplyTypeNames[] = {
47+
[(int)PDSupplyType::fixed] = "Fixed",
48+
[(int)PDSupplyType::battery] = "Battery",
49+
[(int)PDSupplyType::variable] = "Variable",
50+
[(int)PDSupplyType::pps] = "PPS",
51+
};
52+
53+
const char* getSupplyTypeName(PDSupplyType type) {
54+
unsigned int arraySize = sizeof(SupplyTypeNames) / sizeof(SupplyTypeNames[0]);
55+
if ((unsigned int)type < arraySize)
56+
return SupplyTypeNames[(unsigned int)type];
57+
else
58+
return "<unknown>";
59+
}

examples/ProtocolAnalyzer/ProtocolAnalyzer.ino

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// Monitors the USB PD communication between two USB C devices
11+
// and prints the decoded messages.
12+
//
13+
// Please see https://github.com/manuelbl/usb-pd-arduino/wiki for
14+
// instructions how to wire your specific board to a USB C intermediate
15+
// adapter. For this sketch, "Monitor Mode" is relevant.
16+
//
17+
118
#include "USBPowerDelivery.h"
219

320
void setup() {

examples/TriggerBoard/TriggerBoard.ino

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// Simple trigger board selecting 12V at 1A when the power supply is
11+
// connected and if 12V is available.
12+
//
13+
// Please see https://github.com/manuelbl/usb-pd-arduino/wiki for
14+
// instructions how to wire your specific board to a USB C connector.
15+
// For this sketch, "Sink Mode" is relevant.
16+
//
17+
118
#include "USBPowerDelivery.h"
219

320
void setup() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// Selects 12V if available, and 15V otherwise.
11+
12+
// Please see https://github.com/manuelbl/usb-pd-arduino/wiki for
13+
// instructions how to wire your specific board to a USB C connector.
14+
// For this sketch, "Sink Mode" is relevant.
15+
//
16+
17+
#include "USBPowerDelivery.h"
18+
19+
void setup() {
20+
Serial.begin(115200);
21+
PowerSink.start(handleEvent);
22+
}
23+
24+
void loop() {
25+
PowerSink.poll();
26+
}
27+
28+
void handleEvent(PDSinkEventType eventType) {
29+
30+
if (eventType == PDSinkEventType::sourceCapabilitiesChanged) {
31+
// source capabilities have changed
32+
if (PowerSink.isConnected()) {
33+
// USB PD supply is connected
34+
requestVoltage();
35+
36+
} else {
37+
// no supply or no USB PD capable supply is connected
38+
PowerSink.requestPower(5000); // reset to 5V
39+
}
40+
41+
} else if (eventType == PDSinkEventType::voltageChanged) {
42+
// voltage has changed
43+
if (PowerSink.activeVoltage != 0) {
44+
Serial.printf("Voltage: %d mV @ %d mA (max)", PowerSink.activeVoltage, PowerSink.activeCurrent);
45+
Serial.println();
46+
} else {
47+
Serial.println("Disconnected");
48+
}
49+
50+
} else if (eventType == PDSinkEventType::powerRejected) {
51+
// rare case: power supply rejected requested power
52+
Serial.println("Power request rejected");
53+
Serial.printf("Voltage: %d mV @ %d mA (max)", PowerSink.activeVoltage, PowerSink.activeCurrent);
54+
}
55+
}
56+
57+
void requestVoltage() {
58+
// check if 12V is supported
59+
for (int i = 0; i < PowerSink.numSourceCapabilities; i += 1) {
60+
if (PowerSink.sourceCapabilities[i].minVoltage <= 12000
61+
&& PowerSink.sourceCapabilities[i].maxVoltage >= 12000) {
62+
PowerSink.requestPower(12000);
63+
return;
64+
}
65+
}
66+
67+
// check if 15V is supported
68+
for (int i = 0; i < PowerSink.numSourceCapabilities; i += 1) {
69+
if (PowerSink.sourceCapabilities[i].minVoltage <= 15000
70+
&& PowerSink.sourceCapabilities[i].maxVoltage >= 15000) {
71+
PowerSink.requestPower(15000);
72+
return;
73+
}
74+
}
75+
76+
Serial.println("Neither 12V nor 15V is supported");
77+
}
78+

library.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
33
"name": "USBPowerDelivery",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"description": "USB Power Delivery for Arduino. Create a USB PD protocol analyzer, trigger board or power sink for no or only few additional components. Supports several STM32 boards.",
66
"keywords": "usb,usb-pd,usb-power-delivery,protocol-analyzer,trigger-board",
77
"homepage": "https://github.com/manuelbl/usb-pd-arduino",
@@ -29,6 +29,16 @@
2929
"name": "Trigger Board",
3030
"base": "examples/TriggerBoard",
3131
"files": [ "TriggerBoard.ino" ]
32+
},
33+
{
34+
"name": "Trigger Board Advanced",
35+
"base": "examples/TriggerBoardAdvanced",
36+
"files": [ "TriggerBoardAdvanced.ino" ]
37+
},
38+
{
39+
"name": "List Capabilities",
40+
"base": "examples/ListCapabilities",
41+
"files": [ "ListCapabilities.ino" ]
3242
}
3343
]
3444
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=USBPowerDelivery
2-
version=1.0.1
2+
version=1.0.2
33
author=Manuel Bl. <manuel.bleichenbacher@gmail.com>
44
maintainer=Manuel Bl. <manuel.bleichenbacher@gmail.com>
55
sentence=USB Power Delivery for Arduino.

0 commit comments

Comments
 (0)