From 1d72ebb391068db88db138d8c78c795127422302 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Mon, 25 Sep 2017 22:23:07 +0300
Subject: [PATCH 1/6] Add `stty_params` target.

Make it run a command according to arch wiki article on Arduino #stty.
Make `monitor` target depend on `stty_params`.
---
 Arduino.mk | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/Arduino.mk b/Arduino.mk
index 0f34a82..e58326c 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -1687,6 +1687,28 @@ reset_stty:
 		(sleep 0.1 2>/dev/null || sleep 1) ; \
 		$$STTYF $(call get_monitor_port) -hupcl
 
+stty_params:
+	stty -F $(call get_monitor_port) \
+		cs8 \
+		$(MONITOR_BAUDRATE) \
+		ignbrk \
+		-brkint \
+		-icrnl \
+		-imaxbel \
+		-opost \
+		-onlcr \
+		-isig \
+		-icanon \
+		-iexten \
+		-echo \
+		-echoe \
+		-echok \
+		-echoctl \
+		-echoke \
+		noflsh \
+		-ixon \
+		-crtscts
+
 ispload:	$(TARGET_EEP) $(TARGET_HEX) verify_size
 		$(AVRDUDE) $(AVRDUDE_COM_OPTS) $(AVRDUDE_ISP_OPTS) -e \
 			$(AVRDUDE_ISPLOAD_OPTS)
@@ -1730,8 +1752,8 @@ show_boards:
 show_submenu:
 	@$(CAT) $(BOARDS_TXT) | grep -E '[a-zA-Z0-9_\-]+.menu.(cpu|chip).[a-zA-Z0-9_\-]+=' | sort -uf | sed 's/.menu.\(cpu\|chip\)./:/' | sed 's/=/:/' | column -s: -t
 
-monitor:
-ifeq ($(notdir $(MONITOR_CMD)), putty)
+monitor: stty_params
+ifeq ($(MONITOR_CMD), 'putty')
 	ifneq ($(strip $(MONITOR_PARAMS)),)
 	$(MONITOR_CMD) -serial -sercfg $(MONITOR_BAUDRATE),$(MONITOR_PARAMS) $(call get_monitor_port)
 	else

From c5d744f70c52146e8e2c2c5d7a7872c7a2728ff0 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Mon, 25 Sep 2017 22:28:34 +0300
Subject: [PATCH 2/6] Add `monitor` commands using `less` and `cat`.

Use `less` as the default `MONITOR_CMD`.
Change comment explenation about `# Serial Monitor`.
---
 Arduino.mk | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/Arduino.mk b/Arduino.mk
index e58326c..33d88f7 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -936,11 +936,11 @@ ifndef ARDUINO_LIBS
 endif
 
 ########################################################################
-# Serial monitor (just a screen wrapper)
+# Serial monitor
 
-# Quite how to construct the monitor command seems intimately tied
-# to the command we're using (here screen). So, read the screen docs
-# for more information (search for 'character special device').
+# In order to construct a monitor command, we need to use either `less`,
+# `screen` or `cat`. With `less`, as the default fallback, we will use
+# `-f` flag. Read it's man page to get a better understanding.
 
 ifeq ($(strip $(NO_CORE)),)
     ifndef MONITOR_BAUDRATE
@@ -958,9 +958,8 @@ ifeq ($(strip $(NO_CORE)),)
     else
         $(call show_config_variable,MONITOR_BAUDRATE, [USER])
     endif
-
     ifndef MONITOR_CMD
-        MONITOR_CMD = screen
+        MONITOR_CMD = less
     endif
 endif
 
@@ -1763,6 +1762,10 @@ else ifeq ($(notdir $(MONITOR_CMD)), picocom)
 		$(MONITOR_CMD) -b $(MONITOR_BAUDRATE) $(MONITOR_PARAMS) $(call get_monitor_port)
 else ifeq ($(notdir $(MONITOR_CMD)), cu)
 		$(MONITOR_CMD) -l $(call get_monitor_port) -s $(MONITOR_BAUDRATE)
+else ifeq ($(MONITOR_CMD), less)
+		$(MONITOR_CMD) -f $(call get_monitor_port)
+else ifeq ($(MONITOR_CMD), cat)
+		$(MONITOR_CMD) $(call get_monitor_port)
 else
 		$(MONITOR_CMD) $(call get_monitor_port) $(MONITOR_BAUDRATE)
 endif

From b07de28cd42f64984468223591748ca780626a3a Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Wed, 3 Oct 2018 13:16:37 +0300
Subject: [PATCH 3/6] Add stty_params to help target

---
 Arduino.mk | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Arduino.mk b/Arduino.mk
index 33d88f7..6f772fa 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -1828,6 +1828,7 @@ help:
                            rate on the serial port.\n\
   make show_boards       - list all the boards defined in boards.txt\n\
   make show_submenu      - list all board submenus defined in boards.txt\n\
+  make stty_params       - set proper tty paramerters for the monitor port\n\
   make monitor           - connect to the Arduino's serial port\n\
   make debug_init        - start openocd gdb server\n\
   make debug             - connect to gdb target and begin debugging\n\

From 1e766cf1aca7c35ecd5749321428c00b731634ca Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Wed, 3 Oct 2018 13:20:46 +0300
Subject: [PATCH 4/6] Add +F to less in MONITOR_CMD

---
 Arduino.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Arduino.mk b/Arduino.mk
index 6f772fa..5b85b56 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -959,7 +959,7 @@ ifeq ($(strip $(NO_CORE)),)
         $(call show_config_variable,MONITOR_BAUDRATE, [USER])
     endif
     ifndef MONITOR_CMD
-        MONITOR_CMD = less
+        MONITOR_CMD = "less +F"
     endif
 endif
 

From e76be67d6e64d281589aa867021a2d45e55dfffe Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Tue, 9 Oct 2018 19:26:23 +0300
Subject: [PATCH 5/6] Revert default MONITOR_CMD to screen

Add support for tail as `MONITOR_CMD`.
---
 Arduino.mk | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Arduino.mk b/Arduino.mk
index 5b85b56..3b4ecdb 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -959,7 +959,7 @@ ifeq ($(strip $(NO_CORE)),)
         $(call show_config_variable,MONITOR_BAUDRATE, [USER])
     endif
     ifndef MONITOR_CMD
-        MONITOR_CMD = "less +F"
+        MONITOR_CMD = "screen"
     endif
 endif
 
@@ -1763,6 +1763,8 @@ else ifeq ($(notdir $(MONITOR_CMD)), picocom)
 else ifeq ($(notdir $(MONITOR_CMD)), cu)
 		$(MONITOR_CMD) -l $(call get_monitor_port) -s $(MONITOR_BAUDRATE)
 else ifeq ($(MONITOR_CMD), less)
+		$(MONITOR_CMD) +F $(call get_monitor_port)
+else ifeq ($(MONITOR_CMD), tail)
 		$(MONITOR_CMD) -f $(call get_monitor_port)
 else ifeq ($(MONITOR_CMD), cat)
 		$(MONITOR_CMD) $(call get_monitor_port)

From cd754458b2a99489a9f3b532ac48bd0e27c40976 Mon Sep 17 00:00:00 2001
From: Doron Behar <doron.behar@gmail.com>
Date: Fri, 12 Oct 2018 20:24:54 +0300
Subject: [PATCH 6/6] Fix error `/dev/ttyUSB0 is not a regular file`

---
 Arduino.mk | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Arduino.mk b/Arduino.mk
index 3b4ecdb..a8144e3 100644
--- a/Arduino.mk
+++ b/Arduino.mk
@@ -1763,7 +1763,7 @@ else ifeq ($(notdir $(MONITOR_CMD)), picocom)
 else ifeq ($(notdir $(MONITOR_CMD)), cu)
 		$(MONITOR_CMD) -l $(call get_monitor_port) -s $(MONITOR_BAUDRATE)
 else ifeq ($(MONITOR_CMD), less)
-		$(MONITOR_CMD) +F $(call get_monitor_port)
+		$(MONITOR_CMD) +F -f $(call get_monitor_port)
 else ifeq ($(MONITOR_CMD), tail)
 		$(MONITOR_CMD) -f $(call get_monitor_port)
 else ifeq ($(MONITOR_CMD), cat)