#define SOFT_TX_PIN PA2
#define SOFT_RX_PIN PA3
#define BUTTON_PIN PA0
#define LED_PIN PC13
HardwareSerial DemoUart(SOFT_RX_PIN, SOFT_TX_PIN); // RX, TX on PA3/PA2 (USART2 pins)
bool ledOn = false;
bool lastButton = HIGH;
char rxBuf[32];
uint8_t rxLen = 0;
void setLed(bool on) {
ledOn = on;
digitalWrite(LED_PIN, on ? LOW : HIGH); // PC13 is active-low.
}
void handleRxByte(uint8_t value) {
if (value == '\r' || value == '\n') {
rxBuf[rxLen] = '\0';
if (rxLen > 0 && strcmp(rxBuf, "hello") == 0) {
setLed(!ledOn);
DemoUart.print("PC13 toggled\r\n");
}
rxLen = 0;
return;
}
if (rxLen < sizeof(rxBuf) - 1) {
rxBuf[rxLen++] = (char)value;
} else {
rxLen = 0;
}
}
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
setLed(false);
DemoUart.begin(115200);
DemoUart.print("ready: PA2 TX, PA3 RX, PA0 button, PC13 LED\r\n");
}
void loop() {
bool button = digitalRead(BUTTON_PIN);
if (lastButton == HIGH && button == LOW) {
DemoUart.print("hello\r\n");
}
lastButton = button;
while (DemoUart.available()) {
handleRxByte((uint8_t)DemoUart.read());
}
}