Presentation
This task has been the easy part due to MythTV flexibility and only a few shell scripts was created:
- Serial line initialization.
- LED control (called lamp), which accepts commands like ‘on‘ / ‘off‘ to switch on (100 %) or off smoothly in 5 s, ‘half‘ to set to 50 % in 5 s, ‘shutdown‘ to switch off immediately and ‘switch‘ that enables to invert current state (on -> off, off -> 50 %) or if called two times quickly (double press on the remote control) while going from off to 50 % order to light to 100 %.
- Relay control, which accepts commands ‘on‘ and ‘off‘ that turn the Relay on and off.
Serial line initialization
This script is called upon boot, and as the USB Serial emulation was prone to change its name under /dev, I finally setup a UDEV rule to have a fixed name ‘ttyUSB0’ under /dev.
For that, I googled a lot, and finally the procedure is the one described below.
Look at syslog and insert the Arduino Nano
1 2 3 4 5 6 7 8 9 10 11 12 13 |
tail -F /var/log/syslog Nov 17 17:38:51 PCMyth kernel: [ 301.405710] usb 2-1.1: new full-speed USB device number 10 using ehci-pci Nov 17 17:38:51 PCMyth kernel: [ 301.498810] usb 2-1.1: New USB device found, idVendor=067b, idProduct=2303 Nov 17 17:38:51 PCMyth kernel: [ 301.498814] usb 2-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0 Nov 17 17:38:51 PCMyth kernel: [ 301.498817] usb 2-1.1: Product: USB-Serial Controller Nov 17 17:38:51 PCMyth kernel: [ 301.498819] usb 2-1.1: Manufacturer: Prolific Technology Inc. Nov 17 17:38:51 PCMyth mtp-probe: checking bus 2, device 10: "/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1" Nov 17 17:38:51 PCMyth mtp-probe: bus: 2, device: 10 was not an MTP device Nov 17 17:38:51 PCMyth kernel: [ 301.525132] usbcore: registered new interface driver pl2303 Nov 17 17:38:51 PCMyth kernel: [ 301.525396] usbserial: USB Serial support registered for pl2303 Nov 17 17:38:51 PCMyth kernel: [ 301.525935] pl2303 2-1.1:1.0: pl2303 converter detected Nov 17 17:38:51 PCMyth kernel: [ 301.527971] usb 2-1.1: pl2303 converter now attached to ttyUSB0 |
This will tell what is the serial device used (here a pl2303) and the name under /dev (here ttyUSB0: ‘… now attached to ttyUSB0’)
Then issue the command below to have the exact attributes for UDEV:
1 2 3 4 |
$ udevadm info -a -p $(udevadm info -q path -n ttyUSB0) | egrep -i "ATTRS{serial}|ATTRS{idVendor}|ATTRS{idProduct}" -m 3 ATTRS{idVendor}=="067b" ATTRS{idProduct}=="2303" ATTRS{idVendor}=="8087" |
And create the UDEV rule:
1 2 3 |
$ sudo vi /etc/udev/rules.d/10-pl2303.rules SUBSYSTEMS=="usb", ATTRS{idProduct}=="2303", ATTRS{idVendor}=="067b", MODE="0666", OWNER="gg", RUN+="/path-to-dimmer-scripts/commsetup" |
Finally the commsetup script is:
1 2 |
#!/bin/sh /bin/stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts |
LED control
The LED control script just translates the ‘on’ / ‘off’ / ‘half’ / ‘shutdown’ / ‘switch’ parameters mentioned in the introduction into commands (of type ++Dc-vvv-ttt.t ) that are sent to the dimmer over the serial line.
The script is very basic: at the moment, all fade in / fade out have a fixed 5 second duration set within this script.
It is called by MythTV and also during shutdown. For MythTV, references to /path-to-dimmer-scripts/lamp script has been set on the Play / Pause / Stop / Resume… events using the management GUI. This is up to each personal taste…
Here is the source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#!/bin/sh if [ -z "$1" ]; then echo "Usage: $0 COMMAND" exit 1 fi saveState() { if [ "$1" = "on" ]; then touch /tmp/lamp_on chmod go+rw /tmp/lamp_on rm -f /tmp/lamp_off else touch /tmp/lamp_off chmod go+rw /tmp/lamp_off rm -f /tmp/lamp_on fi } light_on() { echo "switch ON" echo "++D0-100-005.0" > /dev/ttyUSB0 saveState "on" } light_off() { echo "switch OFF" echo "++D0-000-005.0" > /dev/ttyUSB0 saveState "off" } light_half() { echo "switch HALF" echo "++D0-030-005.0" > /dev/ttyUSB0 saveState "on" } light_off_immed() { echo "switch OFF" echo "++D0-000-000.0" > /dev/ttyUSB0 saveState "off" } COMMAND="$1" shift case $COMMAND in on) light_on ;; half) light_half ;; off) light_off ;; shutdown) light_off_immed ;; switch) if [ -f /tmp/lamp_off ]; then light_half else time_on=`stat -c %Z /tmp/lamp_on` now=`date +%s` delta=`expr $now - $time_on` if [ $delta -gt 2 ]; then light_off else light_on fi fi ;; *) echo "ERROR" echo "Invalid parameter" exit 1 esac exit 0 |
Relay control
This script is straightforward, it just translates ‘on’ / ‘off’ into commands of type ++Rc-v for the Relay that are sent to the dimmer over the serial line.
It is called when MythTV starts to switch off the ceiling lamps and power on the LED dimmer, and on shutdown to power off the LED dimmer and switch on the ceiling lamps.
The calls for Relay and Dimmer are inserted in the MtyTV /usr/share/mythtv/mythfrontend.sh script:
1 2 |
/path-to-dimmer-scripts/lamp on /path-to-dimmer-scripts/relay on |
Here is the source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/sh if [ -z "$1" ]; then echo "Usage: $0 COMMAND" exit 1 fi COMMAND="$1" shift case $COMMAND in on) echo "++R0-1" > /dev/ttyUSB0 ;; off) echo "++R0-0" > /dev/ttyUSB0 ;; *) echo "ERROR" echo "Only on or off allowed for relay" exit 1 esac exit 0 |
Be First to Comment