1
0

setup_kiosk.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/bin/bash
  2. #############################################
  3. # Dune Weaver Touch - Kiosk Mode Setup Script
  4. # For Raspberry Pi with Freenove 5" DSI Display
  5. # Author: Dune Weaver Team
  6. # Version: 1.0
  7. #############################################
  8. set -e # Exit on error
  9. # Color codes for output
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. NC='\033[0m' # No Color
  14. # Configuration variables
  15. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  16. USER_NAME=$(whoami)
  17. HOME_DIR="/home/$USER_NAME"
  18. APP_DIR="$SCRIPT_DIR"
  19. SERVICE_NAME="dune-weaver-kiosk"
  20. VENV_PATH="$APP_DIR/bin/activate"
  21. # Display banner
  22. echo -e "${GREEN}"
  23. echo "================================================"
  24. echo " Dune Weaver Touch - Kiosk Setup"
  25. echo " Freenove 5\" DSI Display (800x480)"
  26. echo "================================================"
  27. echo -e "${NC}"
  28. # Function to detect Raspberry Pi model
  29. detect_pi_model() {
  30. local model=$(cat /proc/cpuinfo | grep "Model" | cut -d ':' -f 2 | sed 's/^ *//')
  31. if [[ $model == *"Pi 3"* ]]; then
  32. echo "3"
  33. elif [[ $model == *"Pi 4"* ]]; then
  34. echo "4"
  35. elif [[ $model == *"Pi 5"* ]]; then
  36. echo "5"
  37. else
  38. echo "unknown"
  39. fi
  40. }
  41. # Function to backup files
  42. backup_file() {
  43. local file=$1
  44. if [ -f "$file" ]; then
  45. cp "$file" "${file}.backup.$(date +%Y%m%d_%H%M%S)"
  46. echo -e "${YELLOW}Backed up: $file${NC}"
  47. fi
  48. }
  49. # Function to setup boot configuration
  50. setup_boot_config() {
  51. echo -e "${GREEN}Configuring boot settings for DSI display...${NC}"
  52. backup_file "/boot/config.txt"
  53. local pi_model=$(detect_pi_model)
  54. echo -e "${GREEN}Detected Raspberry Pi Model: $pi_model${NC}"
  55. # Remove conflicting HDMI settings if they exist
  56. sudo sed -i '/hdmi_force_hotplug/d' /boot/config.txt
  57. sudo sed -i '/hdmi_group/d' /boot/config.txt
  58. sudo sed -i '/hdmi_mode/d' /boot/config.txt
  59. sudo sed -i '/hdmi_cvt/d' /boot/config.txt
  60. sudo sed -i '/hdmi_drive/d' /boot/config.txt
  61. sudo sed -i '/config_hdmi_boost/d' /boot/config.txt
  62. sudo sed -i '/dtoverlay=ads7846/d' /boot/config.txt
  63. # Remove old overlays
  64. sudo sed -i '/dtoverlay=vc4-fkms-v3d/d' /boot/config.txt
  65. sudo sed -i '/dtoverlay=vc4-kms-v3d/d' /boot/config.txt
  66. # Add proper configuration based on Pi model
  67. if [ "$pi_model" == "3" ]; then
  68. # Pi 3 configuration
  69. echo -e "${GREEN}Configuring for Pi 3...${NC}"
  70. cat << EOF | sudo tee -a /boot/config.txt > /dev/null
  71. # Dune Weaver Kiosk - DSI Display Configuration (Pi 3)
  72. dtoverlay=vc4-fkms-v3d
  73. gpu_mem=128
  74. max_framebuffers=2
  75. display_auto_detect=1
  76. disable_overscan=1
  77. EOF
  78. else
  79. # Pi 4/5 configuration
  80. echo -e "${GREEN}Configuring for Pi 4/5...${NC}"
  81. cat << EOF | sudo tee -a /boot/config.txt > /dev/null
  82. # Dune Weaver Kiosk - DSI Display Configuration (Pi 4/5)
  83. dtoverlay=vc4-kms-v3d
  84. gpu_mem=128
  85. max_framebuffers=2
  86. display_auto_detect=1
  87. disable_overscan=1
  88. EOF
  89. fi
  90. # Add common settings
  91. cat << EOF | sudo tee -a /boot/config.txt > /dev/null
  92. # Common settings
  93. dtparam=i2c_arm=on
  94. dtparam=spi=on
  95. enable_uart=1
  96. max_usb_current=1
  97. # Disable splash screens for faster boot
  98. disable_splash=1
  99. EOF
  100. echo -e "${GREEN}Boot configuration updated${NC}"
  101. }
  102. # Function to install dependencies
  103. install_dependencies() {
  104. echo -e "${GREEN}Installing required dependencies...${NC}"
  105. sudo apt-get update
  106. sudo apt-get install -y \
  107. libgles2-mesa \
  108. libgles2-mesa-dev \
  109. libgbm-dev \
  110. libdrm-dev \
  111. libinput-dev \
  112. libudev-dev \
  113. libxkbcommon-dev \
  114. fbset \
  115. evtest \
  116. qtvirtualkeyboard-plugin \
  117. qml-module-qtquick-virtualkeyboard \
  118. qt6-virtualkeyboard-plugin \
  119. qml6-module-qt-labs-qmlmodels || {
  120. echo -e "${YELLOW}Some packages may not be available, continuing...${NC}"
  121. }
  122. echo -e "${GREEN}Dependencies installed${NC}"
  123. }
  124. # Function to create startup script
  125. create_startup_script() {
  126. echo -e "${GREEN}Creating startup script...${NC}"
  127. local pi_model=$(detect_pi_model)
  128. local START_SCRIPT="$APP_DIR/start_kiosk.sh"
  129. cat << 'EOF' > "$START_SCRIPT"
  130. #!/bin/bash
  131. # Dune Weaver Touch Kiosk Startup Script
  132. # Auto-generated by setup_kiosk.sh
  133. # Wait for system to fully boot
  134. sleep 10
  135. # Log file
  136. LOG_FILE="/tmp/dune-weaver-kiosk.log"
  137. exec &> >(tee -a "$LOG_FILE")
  138. echo "========================================="
  139. echo "Starting Dune Weaver Touch Kiosk"
  140. echo "Date: $(date)"
  141. echo "========================================="
  142. # Detect Pi model for platform selection
  143. PI_MODEL=$(cat /proc/cpuinfo | grep "Model" | cut -d ':' -f 2)
  144. # Set working directory
  145. cd "$(dirname "$0")"
  146. # Check if virtual environment exists
  147. if [ ! -f "bin/activate" ]; then
  148. echo "ERROR: Virtual environment not found!"
  149. exit 1
  150. fi
  151. # Activate virtual environment
  152. source bin/activate
  153. # Function to run with LinuxFB (Pi 3 with DSI)
  154. run_linuxfb() {
  155. export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0
  156. export QT_QPA_FONTDIR=/usr/share/fonts/truetype
  157. export QT_QPA_FB_HIDECURSOR=1
  158. export QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS=/dev/input/event0:rotate=0
  159. # Enable Virtual Keyboard
  160. export QT_IM_MODULE=qtvirtualkeyboard
  161. export QT_VIRTUALKEYBOARD_STYLE=default
  162. export QT_VIRTUALKEYBOARD_LAYOUT_PATH=/usr/share/qt5/qtvirtualkeyboard/layouts
  163. echo "Starting with LinuxFB platform and virtual keyboard..."
  164. python main.py
  165. }
  166. # Function to run with EGLFS (Pi 4/5 with DSI)
  167. run_eglfs() {
  168. export QT_QPA_PLATFORM=eglfs
  169. export QT_QPA_EGLFS_WIDTH=800
  170. export QT_QPA_EGLFS_HEIGHT=480
  171. export QT_QPA_EGLFS_INTEGRATION=eglfs_kms
  172. export QT_QPA_EGLFS_KMS_ATOMIC=1
  173. export QT_QPA_EGLFS_HIDECURSOR=1
  174. export QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS=/dev/input/event0:rotate=0
  175. # Enable Virtual Keyboard
  176. export QT_IM_MODULE=qtvirtualkeyboard
  177. export QT_VIRTUALKEYBOARD_STYLE=default
  178. export QT_VIRTUALKEYBOARD_LAYOUT_PATH=/usr/share/qt5/qtvirtualkeyboard/layouts
  179. echo "Starting with EGLFS platform and virtual keyboard..."
  180. python main.py
  181. }
  182. # Check for DRM device (needed for EGLFS)
  183. if [ -e /dev/dri/card0 ] || [ -e /dev/dri/card1 ]; then
  184. echo "DRM device found, using EGLFS"
  185. run_eglfs
  186. else
  187. echo "No DRM device, using LinuxFB"
  188. run_linuxfb
  189. fi
  190. echo "Application exited at $(date)"
  191. EOF
  192. chmod +x "$START_SCRIPT"
  193. echo -e "${GREEN}Startup script created: $START_SCRIPT${NC}"
  194. }
  195. # Function to create systemd service
  196. create_systemd_service() {
  197. echo -e "${GREEN}Creating systemd service...${NC}"
  198. local SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
  199. cat << EOF | sudo tee "$SERVICE_FILE" > /dev/null
  200. [Unit]
  201. Description=Dune Weaver Touch Kiosk Mode
  202. After=multi-user.target graphical.target
  203. Wants=network-online.target
  204. After=network-online.target
  205. [Service]
  206. Type=simple
  207. User=$USER_NAME
  208. Group=$USER_NAME
  209. WorkingDirectory=$APP_DIR
  210. # Auto-restart on failure
  211. Restart=always
  212. RestartSec=10
  213. # Start script
  214. ExecStart=$APP_DIR/start_kiosk.sh
  215. # Stop timeout
  216. TimeoutStopSec=10
  217. # Logging
  218. StandardOutput=journal
  219. StandardError=journal
  220. [Install]
  221. WantedBy=multi-user.target
  222. EOF
  223. # Reload systemd and enable service
  224. sudo systemctl daemon-reload
  225. sudo systemctl enable "${SERVICE_NAME}.service"
  226. echo -e "${GREEN}Systemd service created and enabled${NC}"
  227. }
  228. # Function to create uninstall script
  229. create_uninstall_script() {
  230. echo -e "${GREEN}Creating uninstall script...${NC}"
  231. cat << 'EOF' > "$APP_DIR/uninstall_kiosk.sh"
  232. #!/bin/bash
  233. # Dune Weaver Touch - Kiosk Mode Uninstall Script
  234. set -e
  235. RED='\033[0;31m'
  236. GREEN='\033[0;32m'
  237. YELLOW='\033[1;33m'
  238. NC='\033[0m'
  239. SERVICE_NAME="dune-weaver-kiosk"
  240. echo -e "${YELLOW}Uninstalling Dune Weaver Kiosk Mode...${NC}"
  241. # Stop and disable service
  242. if systemctl list-units --full -all | grep -Fq "${SERVICE_NAME}.service"; then
  243. echo "Stopping and disabling service..."
  244. sudo systemctl stop "${SERVICE_NAME}.service" 2>/dev/null || true
  245. sudo systemctl disable "${SERVICE_NAME}.service" 2>/dev/null || true
  246. sudo rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
  247. sudo systemctl daemon-reload
  248. echo -e "${GREEN}Service removed${NC}"
  249. fi
  250. # Remove startup script
  251. if [ -f "start_kiosk.sh" ]; then
  252. rm -f "start_kiosk.sh"
  253. echo -e "${GREEN}Startup script removed${NC}"
  254. fi
  255. # Restore boot config if backup exists
  256. LATEST_BACKUP=$(ls -t /boot/config.txt.backup.* 2>/dev/null | head -n1)
  257. if [ -n "$LATEST_BACKUP" ]; then
  258. echo -e "${YELLOW}Found backup: $LATEST_BACKUP${NC}"
  259. read -p "Restore boot configuration from backup? (y/n): " -n 1 -r
  260. echo
  261. if [[ $REPLY =~ ^[Yy]$ ]]; then
  262. sudo cp "$LATEST_BACKUP" /boot/config.txt
  263. echo -e "${GREEN}Boot configuration restored${NC}"
  264. echo -e "${YELLOW}Reboot required for changes to take effect${NC}"
  265. fi
  266. fi
  267. echo -e "${GREEN}Kiosk mode uninstalled successfully${NC}"
  268. echo -e "${YELLOW}You may want to reboot your Raspberry Pi${NC}"
  269. EOF
  270. chmod +x "$APP_DIR/uninstall_kiosk.sh"
  271. echo -e "${GREEN}Uninstall script created: $APP_DIR/uninstall_kiosk.sh${NC}"
  272. }
  273. # Function to test the display
  274. test_display() {
  275. echo -e "${GREEN}Testing display configuration...${NC}"
  276. # Check framebuffer
  277. if [ -e /dev/fb0 ]; then
  278. echo -e "${GREEN}✓ Framebuffer device found${NC}"
  279. fbset -i | head -5
  280. else
  281. echo -e "${RED}✗ No framebuffer device found${NC}"
  282. fi
  283. # Check for DRM device
  284. if [ -e /dev/dri/card0 ] || [ -e /dev/dri/card1 ]; then
  285. echo -e "${GREEN}✓ DRM device found${NC}"
  286. ls -la /dev/dri/
  287. else
  288. echo -e "${YELLOW}! No DRM device (normal for Pi 3)${NC}"
  289. fi
  290. # Check touch input
  291. echo -e "${GREEN}Available input devices:${NC}"
  292. ls -la /dev/input/event* 2>/dev/null || echo "No input devices found"
  293. }
  294. # Main installation function
  295. install_kiosk() {
  296. echo -e "${GREEN}Starting Kiosk Mode Installation...${NC}"
  297. # Check if running on Raspberry Pi
  298. if ! grep -q "Raspberry Pi" /proc/cpuinfo; then
  299. echo -e "${YELLOW}Warning: This doesn't appear to be a Raspberry Pi${NC}"
  300. read -p "Continue anyway? (y/n): " -n 1 -r
  301. echo
  302. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  303. exit 1
  304. fi
  305. fi
  306. # Check if virtual environment exists
  307. if [ ! -f "$VENV_PATH" ]; then
  308. echo -e "${RED}Error: Virtual environment not found at $VENV_PATH${NC}"
  309. echo "Please run this script from the dune-weaver-touch directory"
  310. exit 1
  311. fi
  312. # Install steps
  313. install_dependencies
  314. setup_boot_config
  315. create_startup_script
  316. create_systemd_service
  317. create_uninstall_script
  318. test_display
  319. echo -e "${GREEN}"
  320. echo "================================================"
  321. echo " Installation Complete!"
  322. echo "================================================"
  323. echo -e "${NC}"
  324. echo
  325. echo "Next steps:"
  326. echo "1. Reboot your Raspberry Pi:"
  327. echo " ${GREEN}sudo reboot${NC}"
  328. echo
  329. echo "2. The kiosk will start automatically after reboot"
  330. echo
  331. echo "3. To manually control the service:"
  332. echo " Start: ${GREEN}sudo systemctl start ${SERVICE_NAME}${NC}"
  333. echo " Stop: ${GREEN}sudo systemctl stop ${SERVICE_NAME}${NC}"
  334. echo " Status: ${GREEN}sudo systemctl status ${SERVICE_NAME}${NC}"
  335. echo " Logs: ${GREEN}journalctl -u ${SERVICE_NAME} -f${NC}"
  336. echo
  337. echo "4. To uninstall:"
  338. echo " ${GREEN}./uninstall_kiosk.sh${NC}"
  339. echo
  340. read -p "Reboot now? (y/n): " -n 1 -r
  341. echo
  342. if [[ $REPLY =~ ^[Yy]$ ]]; then
  343. sudo reboot
  344. fi
  345. }
  346. # Parse command line arguments
  347. case "${1:-}" in
  348. uninstall)
  349. if [ -f "$APP_DIR/uninstall_kiosk.sh" ]; then
  350. "$APP_DIR/uninstall_kiosk.sh"
  351. else
  352. echo -e "${RED}Uninstall script not found${NC}"
  353. exit 1
  354. fi
  355. ;;
  356. test)
  357. test_display
  358. ;;
  359. status)
  360. sudo systemctl status "${SERVICE_NAME}.service"
  361. ;;
  362. *)
  363. install_kiosk
  364. ;;
  365. esac