;;; $Id: gidei.el,v 1.1 2003/07/16 14:09:01 rriipp Exp $

(defvar gidei-buffer "*GIDEI-commands*"
  "The buffer where the GIDEI command(s) will be written to.")

(defvar gidei-file "gidcom__.dat"
  "The file where gidei-buffer will be dumped and that is copied to COM1.")

(defvar gidei-serial-port-linux "ttyS0"
  "The Linux serial port COM1.")

(defvar gidei-path-dos "c:\\Temp\\"
  "The DOS path for gidei-file.")

(defvar gidei-path-linux "/tmp/"
  "The Linux path for gidei-file.")

(defvar gidei-serial-port-dos "COM1"
  "The DOS serial port COM1.")

(defvar gidei-escape-char ""
  "The ASCII code 27.")

(defun gidei-write-file (&optional path)
  "Writes the contents of gidei-buffer to gidei-file."
  (interactive)
  (set-buffer (get-buffer-create gidei-buffer))
  (write-region (point-min) (point-max) (concat path gidei-file)))

(defun gidei-send-to-device-dos (dev)
  "Copies gidei-file to the specified DOS device."
  (interactive)
  (gidei-write-file gidei-path-dos)
  (shell-command (concat "copy " (concat gidei-path-dos gidei-file) " " dev)))

(defun gidei-send-to-device-linux (dev)
  "Copies gidei-file to the specified Linux device."
  (interactive)
  (gidei-write-file gidei-path-linux)
  (shell-command (concat "cat " (concat gidei-path-linux gidei-file) " > /dev/" dev)))

(defun gidei-send-to-serial-port-dos ()
  "Send gidei-file to gidei-serial-port-dos."
  (interactive)
  (gidei-send-to-device-dos gidei-serial-port-dos))

(defun gidei-send-to-serial-port-linux ()
  "Send gidei-file to gidei-serial-port-linux."
  (interactive)
  (gidei-send-to-device-linux gidei-serial-port-linux))

(defun gidei-send-string-to-serial-port-dos (s)
  "Send string s to gidei-serial-port-dos."
  (interactive "sString: ")
  (gidei-clear-commands)
  (gidei-add-command s)
  (gidei-send-to-device-dos gidei-serial-port-dos))

(defun gidei-send-string-to-serial-port-linux (s)
  "Send string s to gidei-serial-port-linux."
  (interactive "sString: ")
  (gidei-clear-commands)
  (gidei-add-command s)
  (gidei-send-to-device-linux gidei-serial-port-linux))

(defun gidei-send-to-console ()
  "Sends gidei-file to the console (for debugging under Linux)."
  (interactive)
  (gidei-send-to-device-linux "console"))

(defun gidei-command (command)
  "Returns the command of the form '<ESC>command'"
  (interactive "sCommand: ")
  (concat gidei-escape-char command))

(defun gidei-keyname (keyname)
  "Returns the simple command of the form '<ESC>keyname.'"
  (interactive "sKey name: ")
  (concat gidei-escape-char keyname "."))

(defun gidei-keyaction (action &optional args)
  "Returns the complex command of the form '<ESC>,action,args[1],args[2],...,args[N].'"
  ;; Example: (gidei-keyaction "move" '("+10" "-10"))
  (interactive "sKey action: \nxParameters: ")
  (concat gidei-escape-char "," action (mapconcat (lambda (x) (concat "," x)) args "") "."))

(defun gidei-add-command (command)
  "Adds the command to the gidei-buffer."
  (interactive "sCommand: ")
  (set-buffer (get-buffer-create gidei-buffer))
  (insert (concat command "\n")))

(defun gidei-clear-commands ()
  "Erases the contents from gidei-buffer."
  (interactive)
  (erase-buffer (get-buffer-create gidei-buffer)))

(defun gidei-test ()
  (interactive)
  (gidei-add-command (gidei-keyname "test"))
  (gidei-send-to-serial-port-dos)
  )