Skip to content

Commit 20a41ee

Browse files
committed
tweak(core): move +serial-* to me-lib, several enhancements
1 parent a2c8f98 commit 20a41ee

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

core/me-lib.el

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,67 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."
991991

992992

993993

994+
;;; Serial port
995+
996+
(autoload 'term-send-string "term")
997+
(defcustom +serial-port "/dev/ttyUSB0"
998+
"The default port (device) to use."
999+
:group 'minemacs-serial
1000+
:type 'file)
1001+
1002+
(defcustom +serial-baudrate 115200
1003+
"The default baudrate to use."
1004+
:group 'minemacs-serial
1005+
:type 'natnum)
1006+
1007+
(defcustom +serial-first-commands nil
1008+
"A list of commands to run in the serial terminal after creation."
1009+
:group 'minemacs-serial
1010+
:type '(repeat string))
1011+
1012+
(defvar +serial-buffer nil)
1013+
(defvar +serial-process nil)
1014+
1015+
(defun +serial-running-p ()
1016+
"Is there a serial port terminal running?"
1017+
(buffer-live-p +serial-buffer) (process-live-p +serial-process))
1018+
1019+
(defun +serial--run-commands (port baud &rest commands)
1020+
"Run COMMANDS on a device via serial communication.
1021+
1022+
Connect at PORT with baudrate BAUD."
1023+
(let ((commands (ensure-list commands)))
1024+
(unless (+serial-running-p)
1025+
(setq +serial-buffer (serial-term port baud)
1026+
+serial-process (get-buffer-process +serial-buffer)
1027+
commands (append +serial-first-commands commands)))
1028+
(if (+serial-running-p)
1029+
(term-send-string +serial-process (string-join (append commands '("\n")) "\n"))
1030+
(user-error "Unable to communicate with the serial terminal process"))))
1031+
1032+
(defun +serial-run-commands (commands &optional port baud)
1033+
"Run COMMANDS on a device via serial communication.
1034+
1035+
If PORT or BAUD are nil, use values from `+serial-port' and `+serial-baudrate'."
1036+
(interactive (list (read-shell-command (format "Run command via serial port: "))))
1037+
(let ((port (or port +serial-port))
1038+
(baud (or baud +serial-baudrate)))
1039+
(+log! "Dev %s@%d: running commands %S" port baud commands)
1040+
(apply #'+serial--run-commands (append (list port baud) (ensure-list commands)))))
1041+
1042+
1043+
1044+
;;; Networking
1045+
1046+
(defvar +net-default-device "wlan0")
1047+
1048+
(defun +net-get-ip-address (&optional dev)
1049+
"Get the IP-address for device DEV (default: eth0) of the current machine."
1050+
(let ((dev (or dev +net-default-device)))
1051+
(format-network-address (car (network-interface-info dev)) t)))
1052+
1053+
1054+
9941055
;;; Github
9951056

9961057
(defun +github-latest-release (repo &optional fallback-release)

modules/me-embedded.el

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,50 +69,6 @@
6969
(unless (file-exists-p x86-lookup-pdf)
7070
(url-copy-file "https://cdrdv2.intel.com/v1/dl/getContent/671200" x86-lookup-pdf t)))
7171

72-
(autoload 'term-send-string "term")
73-
(defcustom +serial-port "/dev/ttyUSB0"
74-
"The default port (device) to use."
75-
:group 'minemacs-embedded
76-
:type 'file)
77-
78-
(defcustom +serial-baudrate 115200
79-
"The default baudrate to use."
80-
:group 'minemacs-embedded
81-
:type 'natnum)
82-
83-
(defcustom +serial-first-commands nil
84-
"A list of commands to run in the serial terminal after creation."
85-
:group 'minemacs-embedded
86-
:type '(repeat string))
87-
88-
(defvar +serial-buffer nil)
89-
(defvar +serial-process nil)
90-
91-
(defun +serial-running-p () (buffer-live-p +serial-buffer) (process-live-p +serial-process))
92-
93-
(defun +serial--run-command-via-serial (port baud &rest commands)
94-
"Run COMMANDS on a device via serial communication.
95-
96-
Connect at PORT with baudrate BAUD."
97-
(let ((commands (ensure-list commands)))
98-
(unless (+serial-running-p)
99-
(setq +serial-buffer (serial-term port baud)
100-
+serial-process (get-buffer-process +serial-buffer)
101-
commands (append +serial-first-commands commands)))
102-
(if (+serial-running-p)
103-
(term-send-string +serial-process (string-join (append commands '("\n")) "\n"))
104-
(user-error "Unable to communicate with the serial terminal process"))))
105-
106-
(defun +serial-run-command-via-serial (&optional port baud &rest commands)
107-
"Run COMMANDS on a device via serial communication.
108-
109-
If PORT or BAUD are nil, use values from `+serial-port' and `+serial-baudrate'."
110-
(interactive)
111-
(let* ((port (or port +serial-port))
112-
(baud (or baud +serial-baudrate))
113-
(commands (or commands (list (read-shell-command (format "Run on %s@%d: " port baud))))))
114-
(apply #'+serial--run-command-via-serial (append (list port baud) commands))))
115-
11672

11773
(provide 'me-embedded)
11874

0 commit comments

Comments
 (0)