Skip to content

Commit

Permalink
tweak(core): move +serial-* to me-lib, several enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Feb 8, 2024
1 parent a2c8f98 commit 20a41ee
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 44 deletions.
61 changes: 61 additions & 0 deletions core/me-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,67 @@ When MAIL-MODE-P is non-nil, treat INFILE as a mail."



;;; Serial port

(autoload 'term-send-string "term")
(defcustom +serial-port "/dev/ttyUSB0"
"The default port (device) to use."
:group 'minemacs-serial
:type 'file)

(defcustom +serial-baudrate 115200
"The default baudrate to use."
:group 'minemacs-serial
:type 'natnum)

(defcustom +serial-first-commands nil
"A list of commands to run in the serial terminal after creation."
:group 'minemacs-serial
:type '(repeat string))

(defvar +serial-buffer nil)
(defvar +serial-process nil)

(defun +serial-running-p ()
"Is there a serial port terminal running?"
(buffer-live-p +serial-buffer) (process-live-p +serial-process))

(defun +serial--run-commands (port baud &rest commands)
"Run COMMANDS on a device via serial communication.
Connect at PORT with baudrate BAUD."
(let ((commands (ensure-list commands)))
(unless (+serial-running-p)
(setq +serial-buffer (serial-term port baud)
+serial-process (get-buffer-process +serial-buffer)
commands (append +serial-first-commands commands)))
(if (+serial-running-p)
(term-send-string +serial-process (string-join (append commands '("\n")) "\n"))
(user-error "Unable to communicate with the serial terminal process"))))

(defun +serial-run-commands (commands &optional port baud)
"Run COMMANDS on a device via serial communication.
If PORT or BAUD are nil, use values from `+serial-port' and `+serial-baudrate'."
(interactive (list (read-shell-command (format "Run command via serial port: "))))
(let ((port (or port +serial-port))
(baud (or baud +serial-baudrate)))
(+log! "Dev %s@%d: running commands %S" port baud commands)
(apply #'+serial--run-commands (append (list port baud) (ensure-list commands)))))



;;; Networking

(defvar +net-default-device "wlan0")

(defun +net-get-ip-address (&optional dev)
"Get the IP-address for device DEV (default: eth0) of the current machine."
(let ((dev (or dev +net-default-device)))
(format-network-address (car (network-interface-info dev)) t)))



;;; Github

(defun +github-latest-release (repo &optional fallback-release)
Expand Down
44 changes: 0 additions & 44 deletions modules/me-embedded.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,6 @@
(unless (file-exists-p x86-lookup-pdf)
(url-copy-file "https://cdrdv2.intel.com/v1/dl/getContent/671200" x86-lookup-pdf t)))

(autoload 'term-send-string "term")
(defcustom +serial-port "/dev/ttyUSB0"
"The default port (device) to use."
:group 'minemacs-embedded
:type 'file)

(defcustom +serial-baudrate 115200
"The default baudrate to use."
:group 'minemacs-embedded
:type 'natnum)

(defcustom +serial-first-commands nil
"A list of commands to run in the serial terminal after creation."
:group 'minemacs-embedded
:type '(repeat string))

(defvar +serial-buffer nil)
(defvar +serial-process nil)

(defun +serial-running-p () (buffer-live-p +serial-buffer) (process-live-p +serial-process))

(defun +serial--run-command-via-serial (port baud &rest commands)
"Run COMMANDS on a device via serial communication.
Connect at PORT with baudrate BAUD."
(let ((commands (ensure-list commands)))
(unless (+serial-running-p)
(setq +serial-buffer (serial-term port baud)
+serial-process (get-buffer-process +serial-buffer)
commands (append +serial-first-commands commands)))
(if (+serial-running-p)
(term-send-string +serial-process (string-join (append commands '("\n")) "\n"))
(user-error "Unable to communicate with the serial terminal process"))))

(defun +serial-run-command-via-serial (&optional port baud &rest commands)
"Run COMMANDS on a device via serial communication.
If PORT or BAUD are nil, use values from `+serial-port' and `+serial-baudrate'."
(interactive)
(let* ((port (or port +serial-port))
(baud (or baud +serial-baudrate))
(commands (or commands (list (read-shell-command (format "Run on %s@%d: " port baud))))))
(apply #'+serial--run-command-via-serial (append (list port baud) commands))))


(provide 'me-embedded)

Expand Down

0 comments on commit 20a41ee

Please sign in to comment.