started rewriting the manual_control Arduino sketch

master
BodgeMaster 2021-05-04 09:52:18 +02:00
parent f896e72ef6
commit da29965b64
1 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,148 @@
/*
* getMode function has been taken from https://forum.arduino.cc/t/check-current-pinmode/232140
*/
#define UNKNOWN_PIN 0xFF
#define TX_PIN 1
#define RX_PIN 0
uint8_t getMode(uint8_t pin){
uint8_t bitmask = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
// I don't see an option for mega to return this, but whatever...
if (port == NOT_A_PIN) return UNKNOWN_PIN;
// Is there a bit we can check?
if (bitmask == 0) return UNKNOWN_PIN;
// Is there only a single bit set?
if (bitmask & bitmask - 1) return UNKNOWN_PIN;
volatile uint8_t *reg, *out;
reg = portModeRegister(port);
out = portOutputRegister(port);
if (*reg & bitmask)
return OUTPUT;
else if (*out & bitmask)
return INPUT_PULLUP;
else
return INPUT;
}
uint8_t toggleState(uint8_t pin){
uint8_t mode = getMode(pin);
switch (mode){
case OUTPUT:
if (digitalRead(pin) == HIGH){
digitalWrite(pin, LOW);
return LOW;
} else {
digitalWrite(pin, HIGH);
return HIGH;
}
case INPUT_PULLUP:
pinMode(pin, INPUT);
return LOW;
case INPUT:
pinMode(pin, INPUT_PULLUP);
return HIGH;
default:
return UNKNOWN_PIN;
}
}
uint8_t toogleMode(uint8_t pin){
uint8_t mode = getMode(pin);
// declared here bc it wont let me inside the switch/case statement
uint8_t state;
switch (mode){
case OUTPUT:
state = digitalRead(pin);
pinMode(pin, INPUT);
digitalWrite(pin, state);
return getMode(pin); // probably sacrificed performance for readability here
case INPUT_PULLUP:
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
return OUTPUT;
case INPUT:
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
return OUTPUT;
default:
return UNKNOWN_PIN;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup(){
Serial.begin(115200);
// set all pins to input and low
for (uint8_t i; i<NUM_DIGITAL_PINS; i++){
Serial.print(".");
if (i==TX_PIN || i==RX_PIN);
else pinMode(i, INPUT);
}
Serial.print(".");
pinMode(LED_BUILTIN, OUTPUT); // set LED to output bc having it as an input would make little sense
while (Serial.available() > 0){
Serial.print(".");
Serial.read();
}
Serial.print("s\r\n");
}
/*
status (output from serial line)
<letter of command> command acknowledgement
. internal operation or waiting
<data>
s success
e error
\n ready
commands (input to serial line)
R pseudo-reset
run setup again
h show help
m set mode
usage: followed by two digit pin number and o or i
set a pin to input or output mode
"pinMode"
x toggle mode
usage: followed by two digit pin number
toggle a pin's mode
enables pullup resistor for previously high output pins
sets output high for previous input pins with pullup enabled
a analog read
usage: followed by two digit analog pin number
returns state of an analog pin
pin number will be converted to digital pin automatically
r read
usage: followed by pin number
returns state of a pin
"digitalRead"
w write
usage: followed by two digit pin number and h or l
sets a pin to the desired state
sets pullup resistor for input pins
"digitalWrite"
p write pwm
usage: followed by two digit pin number and three digit value between 000 and 255
"analogWrite"
t toggle pin state
usage: followed by two digit pin number
toggles state of output pins
toggles pullup resistor for input pins
q lock
lock the Arduino's state by running an infinite loop
*/
void loop(){
}