void ShowPrompt()

in BusTools/GpioTestTool/main.cpp [264:416]


void ShowPrompt (_In_ IGpioPin* pin)
{
    auto listener = Callback<IGpioPinValueChangedHandler>([] (
        _In_ IGpioPin* /*Pin*/,
        _In_ IGpioPinValueChangedEventArgs* Args
        ) -> HRESULT
    {
        GpioPinEdge edge;
        Args->get_Edge(&edge);
        wprintf(L"%s edge occurred.\n", StringFromGpioPinEdge(edge));
        return S_OK;
    });

    auto token = EventRegistrationToken();
    bool listenerRegistered = false;

    GpioPinValue outputLatch = GpioPinValue_High;
    while (std::wcin) {
        wprintf(L"> ");

        std::wstring line;
        if (!std::getline(std::wcin, line)) {
            return;
        }
        std::wistringstream linestream(line);

        std::wstring command;
        linestream >> command;
        if ((command == L"q") || (command == L"quit")) {
            return;
        } else if ((command == L"h") || (command == L"help")) {
            wprintf(L"%s\n", Help);
        } else if ((command == L"w") || (command == L"write")) {
            GpioPinValue value;
            linestream >> value;
            if (linestream.fail()) {
                wprintf(L"Syntax error: expecting 0 or 1\nUsage: write 0|1\n");
                continue;
            }

            HRESULT hr = pin->Write(value);
            if (FAILED(hr)) {
                wprintf(L"Failed to write pin. (hr = 0x%x)\n", hr);
                continue;
            }
        } else if (command == L"high") {
            HRESULT hr = pin->Write(GpioPinValue_High);
            if (FAILED(hr)) {
                wprintf(L"Failed to write pin. (hr = 0x%x)\n", hr);
                continue;
            }
        } else if (command == L"low") {
            HRESULT hr = pin->Write(GpioPinValue_Low);
            if (FAILED(hr)) {
                wprintf(L"Failed to write pin. (hr = 0x%x)\n", hr);
                continue;
            }
        } else if ((command == L"t") || (command == L"toggle")) {
            outputLatch = !outputLatch;
            HRESULT hr = pin->Write(outputLatch);
            if (FAILED(hr)) {
                wprintf(L"Failed to write pin. (hr = 0x%x)\n", hr);
                continue;
            }
        } else if ((command == L"r") || (command == L"read")) {
            GpioPinValue value;
            HRESULT hr = pin->Read(&value);
            if (FAILED(hr)) {
                wprintf(L"Failed to read pin. (hr = 0x%x)\n", hr);
                continue;
            }
            wprintf(L"%s\n", StringFromGpioPinValue(value));
        } else if (command == L"setdrivemode") {
            GpioPinDriveMode driveMode;
            linestream >> driveMode;
            if (!linestream) {
                wprintf(L"Syntax error: expecting valid drive mode. Type 'help' for usage.\n");
                continue;
            }

            HRESULT hr = pin->SetDriveMode(driveMode);
            if (FAILED(hr)) {
                wprintf(L"Failed to set drive mode. (hr = 0x%x)\n", hr);
                continue;
            }
        } else if ((command == L"int") || (command == L"interrupt")) {
            std::wstring onOrOff;
            linestream >> onOrOff;
            if (onOrOff == L"on") {
                if (listenerRegistered) {
                    wprintf(L"Interrupt listener already registered.\n");
                    continue;
                }

                HRESULT hr = pin->add_ValueChanged(listener.Get(), &token);
                if (FAILED(hr)) {
                    wprintf(
                        L"Failed to add event listener to ValueChanged event. (hr = 0x%x)\n",
                        hr);
                    continue;
                }
                listenerRegistered = true;
            } else if (onOrOff == L"off") {
                if (!listenerRegistered) {
                    wprintf(L"No interrupt listener is currently registered.\n");
                    continue;
                }

                HRESULT hr = pin->remove_ValueChanged(token);
                if (FAILED(hr)) {
                    wprintf(
                        L"Failed to remove ValueChanged event. (hr = 0x%x)\n",
                        hr);
                    continue;
                }
                listenerRegistered = false;
            } else {
                wprintf(
                    L"Expecting 'on' or 'off': %s. Type 'help' for usage.\n",
                    onOrOff.c_str());
            }
        } else if ((command == L"i") || (command == L"info")) {
            int pinNumber;
            pin->get_PinNumber(&pinNumber);
            GpioSharingMode sharingMode;
            pin->get_SharingMode(&sharingMode);
            TimeSpan debounceTimeout;
            pin->get_DebounceTimeout(&debounceTimeout);
            GpioPinDriveMode driveMode;
            pin->GetDriveMode(&driveMode);
            GpioPinValue value;
            pin->Read(&value);

            wprintf(
                L"        Pin Number: %d\n"
                L"      Sharing Mode: %s\n"
                L"  Debounce Timeout: %d ms\n"
                L"        Drive Mode: %s\n"
                L"             Value: %s\n",
                pinNumber,
                StringFromGpioSharingMode(sharingMode),
                int(debounceTimeout.Duration / 10000LL),
                StringFromGpioPinDriveMode(driveMode),
                StringFromGpioPinValue(value));
        } else if (command.empty()) {
            // ignore
        } else {
            wprintf(
                L"Unrecognized command: %s. Type 'help' for command usage.\n",
                command.c_str());
        }
    }
}