Skip to content

Commit

Permalink
Stopping work to transfer over
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenwizard committed Apr 16, 2023
1 parent 007e9f1 commit 5a5b9cc
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ build:
cp src/settings.py target/
cp -r src/www/ target/
cp src/nu_gundam.py target/
cp src/LED.py target/
cp src/BaseGundam.py target/
cp src/config/nu_gundam.json target/config/nu_gundam.json
cp src/webserver.py target/main.py
Expand Down
12 changes: 8 additions & 4 deletions src/BaseGundam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

from LED import LED
from phew import server
import json

Expand All @@ -20,8 +21,8 @@ def get_config_file(self)->str:
def add_routes(self, server: server):
server.add_route(f"/led/<led_name>/on", self.led_on, methods=["GET"])
server.add_route(f"/led/<led_name>/off", self.led_off, methods=["GET"])
server.add_route("/led/all/on", self.all_on, methods=["GET"])
server.add_route(f"/led/all/off", self.all_off, methods=["GET"])
server.add_route("/all/on", self.all_on, methods=["GET"])
server.add_route("/all/off", self.all_off, methods=["GET"])
for lightshow in self.config['lightshow']:
server.add_route(f"/lightshow/{lightshow['path']}", getattr(self, lightshow['method']), methods=["GET"])

Expand All @@ -32,7 +33,7 @@ def led_on(self, request: Request, led_name: str) -> Response:
try:
pin_num = self.get_pin_from_name(led_name)
led = self.generic_pin(pin_num)
self.head_led.on()
self.led.on()
return f"{led_name} on", 200
except Exception as e:
return str(e), 500
Expand All @@ -44,7 +45,7 @@ def led_off(self, request: Request, led_name: str) -> Response:
try:
pin_num = self.get_pin_from_name(led_name)
led = self.generic_pin(pin_num)
self.head_led.off()
self.led.off()
return f"{led_name} off", 200
except Exception as e:
return str(e), 500
Expand Down Expand Up @@ -76,6 +77,9 @@ def all_off(self, request:Request) -> Response:
return str(e), 500


def get_led_from_name(self, led_name:str)->LED:
return LED(self.get_pin_from_name(led_name), led_name)

def generic_pin(self, pin_num:int) -> Pin:
"""
Given a pin number, returns the Pin for it
Expand Down
37 changes: 37 additions & 0 deletions src/LED.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from machine import Pin, PWM
from time import sleep


class LED:

def __init__(self, pin_number: int, name: str):
self.pin: Pin = Pin(pin_number, Pin.OUT)
self.name = name

def on(self):
self.pin.on()

def off(self):
self.pin.off()

def name(self) -> str:
return self.name()

def pin(self) -> Pin:
return self.pin()

def brighten(self, start_percent:int =0, end_percent:int =100, speed: int = 1)-> None:
"""
Starting from start_pct goes to end_pct over the course of speed
:param intensity:
:param speed:
:return:
"""
pwm = PWM(self.pin())
pwm.freq(1000)
start = int((65025 * start_percent) / 100)
end = int((65025 * end_percent) / 100)

for duty in range(start, end):
pwm.duty_u16(duty)
sleep(0.0001)
2 changes: 1 addition & 1 deletion src/nu_gundam.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class NuGundam(BaseGundam):
# head_led = Pin(1, Pin.OUT)
head_led = Pin(1, Pin.OUT)
fin_funnel1 = Pin(2, Pin.OUT)

def __init__(self):
Expand Down
23 changes: 21 additions & 2 deletions src/webserver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from machine import Pin
import time

import BaseGundam
import settings

from nu_gundam import NuGundam
Expand All @@ -10,11 +11,28 @@

board_led: Pin = Pin("LED", Pin.OUT)

gundam = NuGundam()


def createLedButton(name: str) -> str:
return f"""<form action="./led/{name}/on">
<input type="submit" value="Turn on {name}"/>
</form>"""


def createLeds() -> str:
leds = gundam.config['leds']
allButtons = ""
for led in leds:
name = led['name']
button = createLedButton(name)
allButtons += button
return allButtons

@server.route("/index", methods=["GET"])
def index(request: Request) -> Response:
server.logging.info("requested index")
return await render_template("www/index.html")
all_buttons = createLeds()
return await render_template("www/index.html", all_buttons=all_buttons)


@server.route("/canary", methods=["GET"])
Expand Down Expand Up @@ -43,6 +61,7 @@ def blink() -> None:
time.sleep(0.5)
board_led.off()


def main():
server.logging.info(f"Connect to {settings.webserver['ssid']} with {settings.webserver['password']}")
ipaddress: str = connect_to_wifi(settings.webserver['ssid'], settings.webserver['password'])
Expand Down
2 changes: 1 addition & 1 deletion src/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<form action="./lightshow/blink">
<input type="submit" value="Activate Nu Gundam"/>
</form>

{{all_buttons}}
<p>Sanity</p>
<form action="./all/on">
<input type="submit" value="Turn on All LEDs "/>
Expand Down

0 comments on commit 5a5b9cc

Please sign in to comment.