From 94e76df2768c2115339e8472ceec55cc877a23c9 Mon Sep 17 00:00:00 2001 From: WillB97 Date: Tue, 6 Aug 2024 07:37:39 +0100 Subject: [PATCH] Check pin mode is set correctly for analog read and digital write --- src/src.ino | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/src.ino b/src/src.ino index 55c4216..27befc5 100644 --- a/src/src.ino +++ b/src/src.ino @@ -125,6 +125,17 @@ void processCommand() { } else if (current_arg.equals("SET")) { getSlice(); + // Check if the pin is in the correct mode + int mode = getPinMode(pin); + if (mode != OUTPUT) { + if (mode == INPUT) { + Serial.print("NACK:Digital write is not supported in INPUT\n"); + } else { + Serial.print("NACK:Digital write is not supported in INPUT_PULLUP\n"); + } + Serial.print("\n"); + return; + } if (current_arg.equals("1")) { // CMD: PIN::DIGITAL:SET:1 digitalWrite(pin, HIGH); @@ -144,6 +155,17 @@ void processCommand() { getSlice(); if (current_arg.equals("GET?")) { // CMD: PIN::ANALOG:GET? + // Check if the pin is in the correct mode + int mode = getPinMode(pin); + if (mode != INPUT) { + if (mode == OUTPUT) { + Serial.print("NACK:Analog read is not supported in OUTPUT\n"); + } else { + Serial.print("NACK:Analog read is not supported in INPUT_PULLUP\n"); + } + Serial.print("\n"); + return; + } Serial.print(analogRead(pin)); Serial.print("\n"); return;