From fca1be81e23b39b60ccbffb727a8ec6f883749aa Mon Sep 17 00:00:00 2001
From: Hermod <hermod.inno@gmail.com>
Date: Wed, 28 Mar 2018 21:34:45 +0200
Subject: [PATCH] Using system pipe to diplay color instantly, updated README

---
 README.md                     | 30 ++++++++-----
 scripts/nfc-poll-wrapper.sh   |  4 +-
 scripts/statusRGB_listener.py | 80 +++++++++++++++++++++++++++++++++++
 scripts/waitForNetwork        |  3 +-
 4 files changed, 104 insertions(+), 13 deletions(-)
 create mode 100755 scripts/statusRGB_listener.py

diff --git a/README.md b/README.md
index f466ae2..f952a63 100644
--- a/README.md
+++ b/README.md
@@ -4,20 +4,29 @@
 ## Installation
 
 ### Beginning
-- Create user `hermod`: `adduser hermod`
+- Create user `hermod`: (as root or `sudo`) `adduser hermod`
 - Put it in the same groups as pi (other than the `pi` group): `groups pi | sed 's/ /,/g'`, `usermod -a -G group1,group2,... hermod`
-change `pi` and `root` passwords
+- Change `pi` and `root` passwords
 - `sudo apt remove wolfram-engine minecraft-pi`
 - `sudo apt autoremove`
 - `sudo apt update && sudo apt upgrade && sudo reboot`
 - `sudo apt install git vim screen htop`
-- `sudo date -s '2018-02-15 2:26:00'` if needed
+- `sudo date -s '2018-02-15 2:26:00'` if needed (change the date to current date ofc)
 - Generate RSA key `ssh-keygen -t rsa -C "hermod.inno@gmail.com" -b 4096` and add it to gitlab
 - Clone project in `~`: `git clone git@gitlab.viarezo.fr:hermod/tv_panel.git`
 - `cp front/src/config.template.js front/src/config.js`
 - `cp server/config.template.js server/config.js`
 - Useful links: `ln -s ~/tv_panel/scripts/.screenrc ~/`, `ln -s ~/tv_panel/scripts/.vimrc ~/`
 
+### Telegram message on statup to get IP address
+- `cp scripts/waitForNetwork /root/scripts/waitForNetwork`
+- `cp scripts/startup-telegram-message /root/scripts/startup-telegram-message`
+- `sudo vim /root/scripts/startup-telegram-message` to add the token and chat id
+- Add in root crontab (`sudo crontab -e`):
+```
+@reboot sleep 1 && /root/scripts/waitForNetwork && /root/scripts/startup-telegram-message
+```
+
 ### Boot config
 `vim /boot/config.txt` to comment out `dtparam=audio=on` -> `#dtparam=audio=on` and add
 ```
@@ -50,14 +59,14 @@ After reboot or log out - log in, `npm install -g yarn`
 - `sudo ./test` to test
 - `follow ./python/README.md for python installation`
 
-Script without password for sudo:
-- `sudo mkdir /hermod_bin`
-- `cp tv_panel/scripts/statusRGB.py /hermod_bin/`
-- `sudo visudo` to add
+To display a status LED without root
+- `cp scripts/statusRGB_listener.py /root/scripts/statusRGB_listener.py` (from `tv_panel` directory)
+- Add in root crontab (`sudo crontab -e`):
 ```
-# Allow the scripts here to be run without sudo password
-hermod ALL=(ALL) NOPASSWD: /hermod_bin/statusRGB.py
+@reboot /usr/bin/python3 /root/scripts/statusRGB_listener.py
 ```
+- Reboot or run `/usr/bin/python3 /root/scripts/statusRGB_listener.py &` as root
+- To display a color for t seconds (float): `echo "55ee33 1" > /dev/rgb_pipe`
 
 ### Disable screen sleeping
 
@@ -72,7 +81,8 @@ hermod ALL=(ALL) NOPASSWD: /hermod_bin/statusRGB.py
 
 - Wiring: VCC -> 5V, GND -> GND, RX -> TX, TX -> RX
 - In `sudo raspi-config`, disable serial messages but enable serial interface
-```wget https://github.com/nfc-tools/libnfc/releases/download/libnfc-1.7.1/libnfc-1.7.1.tar.bz2
+```
+wget https://github.com/nfc-tools/libnfc/releases/download/libnfc-1.7.1/libnfc-1.7.1.tar.bz2
 tar xvjf libnfc-1.7.1.tar.bz2
 cd libnfc-1.7.1
 ./configure --prefix=/usr --sysconfdir=/etc
diff --git a/scripts/nfc-poll-wrapper.sh b/scripts/nfc-poll-wrapper.sh
index 4e2abf8..384a9e7 100755
--- a/scripts/nfc-poll-wrapper.sh
+++ b/scripts/nfc-poll-wrapper.sh
@@ -6,9 +6,9 @@ do
   if [[ ! -z "$res" ]]
   then
     echo $res
-    sudo /hermod_bin/statusRGB.py 11ff11 -t 0.2 &
+    echo "11ff11 0.2" > /dev/rgb_pipe
   else
-    sudo /hermod_bin/statusRGB.py ff1111 -t 0.2 &
+    echo "ff1111 0.2" > /dev/rgb_pipe
   fi
   sleep 1
 done
diff --git a/scripts/statusRGB_listener.py b/scripts/statusRGB_listener.py
new file mode 100755
index 0000000..ff068d8
--- /dev/null
+++ b/scripts/statusRGB_listener.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import time
+import argparse
+import select
+
+from neopixel import Adafruit_NeoPixel, ws
+
+
+"""
+This script opens a pipe to access the RGB status LED by writing into it.
+When the script is running, you can use 'echo "11ee55 3" > PIPE_PATH'
+(PIPE_PATH defined below) to display the color 11ee55 for 3 seconds.
+
+It must be run as root.
+"""
+
+
+# LED strip configuration:
+LED_COUNT      = 1       # Number of LED pixels.
+LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
+LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
+LED_DMA        = 10      # DMA channel to use for generating signal (try 10)
+LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
+LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
+LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53
+LED_STRIP      = ws.WS2811_STRIP_GRB   # Strip type and colour ordering
+
+PIPE_PATH = '/dev/rgb_pipe'
+
+
+def sanitize_color(s):
+    """Return (r, g, b) levels from html color format"""
+    hex_color = s.lstrip('#')
+    if len(hex_color) == 3:
+        hex_color = '{0}{0}{1}{1}{2}{2}'.format(*hex_color)
+    if len(hex_color) != 6:
+        return None
+
+    (r, g, b) = (int(x, 16) for x in [hex_color[i:i+2] for i in (0, 2, 4)])
+    return r, g, b
+
+
+if __name__ == '__main__':
+    if not os.path.exists(PIPE_PATH):
+        os.mkfifo(PIPE_PATH)
+    os.system('chmod 666 ' + PIPE_PATH)  # Acces r+w for non-root users
+
+    # Create NeoPixel object with appropriate configuration.
+    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
+
+    # Intialize the library (must be called once before other functions).
+    strip.begin()
+
+    with open(PIPE_PATH, 'r') as fifo:
+        while True:
+            select.select([fifo],[],[fifo])
+            data0 = fifo.read().strip()
+            data = data0.split(' ')
+            colors = sanitize_color(data[0])
+            if colors is None:
+                continue
+            r, g, b = colors
+            try:
+                t = float(data[1])
+            except:
+                t = -1
+
+            strip.setPixelColorRGB(0, r, g, b)
+            strip.show()
+            if t > 0:
+                time.sleep(t)
+                strip.setPixelColorRGB(0, 0, 0, 0)
+                strip.show()
+
+    strip.setPixelColorRGB(0, 0, 0, 0)
+    strip.show()
diff --git a/scripts/waitForNetwork b/scripts/waitForNetwork
index 3c8192a..8d0e610 100755
--- a/scripts/waitForNetwork
+++ b/scripts/waitForNetwork
@@ -2,11 +2,13 @@
 
 while true
 do
+    echo "1111ff 0.1" > /dev/rgb_pipe
     ping 8.8.8.8 -c 3 -W 15 -q > /dev/null
     res=$?
     if [ "$res" = "0" ]
     then
 	echo "Connection established"
+	echo "1111ff 3" > /dev/rgb_pipe
 	break
     else
 	echo "Connection failed"
@@ -14,5 +16,4 @@ do
     sleep 5
 done
 
-
 exit 0
-- 
GitLab