Skip to content

Commit

Permalink
Add an automatic PrusaLink image builder script
Browse files Browse the repository at this point in the history
  • Loading branch information
TojikCZ committed Aug 9, 2023
1 parent c58387d commit 41b4026
Show file tree
Hide file tree
Showing 11 changed files with 637 additions and 23 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Hold the STOPPED and FINISHED state for at least 11s
* Fix MK2(.5)S SN being broken on multi-instance images
* Implemented UPGRADE high lvl Connect command
* Add an automatic PrusaLink image builder script

0.7.0 (2023-05-19)
* Fixed printer sends info about api key change to Connect after change
Expand Down
54 changes: 35 additions & 19 deletions prusa/link/cameras/v4l2_driver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Contains implementation of a camera driver utilizing the V4L2 API"""
import collections
import ctypes
import errno
import fcntl
Expand All @@ -11,7 +10,7 @@
import select
from glob import glob
from types import MappingProxyType
from typing import Any
from typing import Any, NamedTuple

from prusa.connect.printer.camera import Resolution
from prusa.connect.printer.camera_driver import CameraDriver
Expand All @@ -38,23 +37,40 @@

# --- code taken from v4l2py, unused features cut

Info = collections.namedtuple(
"Info",
"driver card bus_info version physical_capabilities capabilities "
"formats frame_sizes, focus_info",
)

ImageFormat = collections.namedtuple(
"ImageFormat", "type description flags pixel_format",
)

FrameType = collections.namedtuple(
"FrameType", "pixel_format width height",
)

FocusInfo = collections.namedtuple(
"FocusInfo", "available min max step",
)
class Info(NamedTuple):
"""Contains information about the device"""
driver: Any
card: Any
bus_info: Any
version: Any
physical_capabilities: Any
capabilities: Any
formats: Any
frame_sizes: Any
focus_info: Any


class ImageFormat(NamedTuple):
"""Contains information about a specific image format"""
type: Any
description: Any
flags: Any
pixel_format: Any


class FrameType(NamedTuple):
"""Contains information about a specific frame type"""
pixel_format: Any
width: Any
height: Any


class FocusInfo(NamedTuple):
"""Contains information about the focus capabilities of the device"""
available: Any
min: Any
max: Any
step: Any


STREAM_TYPE = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
Expand Down
9 changes: 9 additions & 0 deletions prusa/link/data/image_builder/boot-message.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Boot message

[Service]
Type=simple
ExecStart=/bin/sh -c 'stty -F /dev/ttyAMA0 115200; printf \'M117 RPi booting...\n\' > /dev/ttyAMA0'

[Install]
WantedBy=basic.target
24 changes: 24 additions & 0 deletions prusa/link/data/image_builder/first-boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set_up_port () {
# Sets the baudrate and cancels the hangup at the end of a connection
stty -F "$1" 115200 -hupcl;
}

message() {
printf "M117 $2\n" > "$1"
}

set_up_port "/dev/ttyAMA0"
message "/dev/ttyAMA0" "Please wait < 10min";

for i in {0..5}; do
set_up_port "/dev/ttyACM$i"
done

sleep 8

for i in {0..5}; do
message "/dev/ttyACM$i" "Please wait < 10min"
done

# This generates the host keys for the ssh server to work
ssh-keygen -A
24 changes: 24 additions & 0 deletions prusa/link/data/image_builder/manager-start-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Forward the port 80 to 8080 even on the loopback, so we van ping ourselves
iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -I OUTPUT -p tcp -o lo -d localhost --dport 80 -j REDIRECT --to-ports 8080

set_up_port () {
# Sets the baudrate and cancels the hangup at the end of a connection
stty -F "$1" 115200 -hupcl;
}

message() {
printf "M117 $2\n" > "$1"
}

username=$(id -nu 1000)
user_site=$(su $username -c "python -m site --user-site")

set_up_port "/dev/ttyAMA0"
message "/dev/ttyAMA0" "Starting PrusaLink";

/home/$username/.local/bin/prusalink-boot
rm -f /run/prusalink/manager.pid
export PYTHONOPTIMIZE=2
PYTHONPATH=$user_site /home/$username/.local/bin/prusalink-manager -p "PYTHONPATH=$user_site /home/$username/.local/bin/" start
23 changes: 23 additions & 0 deletions prusa/link/data/image_builder/prusalink-start-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Forward the port 80 to 8080 even on the loopback, so we van ping ourselves
iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -I OUTPUT -p tcp -o lo -d localhost --dport 80 -j REDIRECT --to-ports 8080

set_up_port () {
# Sets the baudrate and cancels the hangup at the end of a connection
stty -F "$1" 115200 -hupcl;
}

message() {
printf "M117 $2\n" > "$1"
}

username=$(id -nu 1000)

set_up_port "/dev/ttyAMA0"
message "/dev/ttyAMA0" "Starting PrusaLink";

/home/$username/.local/bin/prusalink-boot
rm -f /home/$username/prusalink.pid
export PYTHONOPTIMIZE=2
su $username -c "/home/$username/.local/bin/prusalink -i start"
Empty file.
Loading

0 comments on commit 41b4026

Please sign in to comment.