|
| 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 | +// --- List the capabilities of the connected power supply |
| 11 | +// |
| 12 | + |
| 13 | +#include <Arduino.h> |
| 14 | +#include "PowerDelivery.h" |
| 15 | + |
| 16 | +static void handleEvent(PDSinkEventType eventType); |
| 17 | +static void listCapabilities(); |
| 18 | +static const char* getSupplyTypeName(PDSupplyType type); |
| 19 | + |
| 20 | +void setup() { |
| 21 | + Serial.begin(115200); |
| 22 | + PowerSink.start(handleEvent); |
| 23 | +} |
| 24 | + |
| 25 | +void loop() { |
| 26 | + PowerSink.poll(); |
| 27 | +} |
| 28 | + |
| 29 | +void handleEvent(PDSinkEventType eventType) { |
| 30 | + if (eventType == PDSinkEventType::sourceCapabilitiesChanged && PowerSink.isConnected()) |
| 31 | + listCapabilities(); |
| 32 | +} |
| 33 | + |
| 34 | +void listCapabilities() { |
| 35 | + Serial.println("USB PD capabilities:"); |
| 36 | + Serial.println("__Type_________Vmin____Vmax____Imax"); |
| 37 | + |
| 38 | + for (int i = 0; i < PowerSink.numSourceCapabilities; i += 1) { |
| 39 | + auto cap = PowerSink.sourceCapabilities[i]; |
| 40 | + Serial.printf(" %-9s %6d %6d %6d", getSupplyTypeName(cap.supplyType), cap.minVoltage, cap.maxVoltage, cap.maxCurrent); |
| 41 | + Serial.println(); |
| 42 | + } |
| 43 | + |
| 44 | + Serial.println("(voltage in mV, current in mA)"); |
| 45 | + Serial.println(); |
| 46 | +} |
| 47 | + |
| 48 | +static const char* const SupplyTypeNames[] = { |
| 49 | + [(int)PDSupplyType::fixed] = "Fixed", |
| 50 | + [(int)PDSupplyType::battery] = "Battery", |
| 51 | + [(int)PDSupplyType::variable] = "Variable", |
| 52 | + [(int)PDSupplyType::pps] = "PPS", |
| 53 | +}; |
| 54 | + |
| 55 | +const char* getSupplyTypeName(PDSupplyType type) { |
| 56 | + unsigned int arraySize = sizeof(SupplyTypeNames) / sizeof(SupplyTypeNames[0]); |
| 57 | + if ((unsigned int)type < arraySize) |
| 58 | + return SupplyTypeNames[(unsigned int)type]; |
| 59 | + else |
| 60 | + return "<unknown>"; |
| 61 | +} |
0 commit comments