From 24f3baa65d4b33595c4be81a57f2437868ef7284 Mon Sep 17 00:00:00 2001 From: paul-loedige Date: Fri, 20 Jan 2023 02:05:55 +0900 Subject: [PATCH] finished code --- app.py | 42 +++++++++++++++++++++++++++++++----- indication_driver.py | 19 +++++++++++------ templates/index.html | 51 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index ea2d058..6ba1e62 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,7 @@ from flask import Flask, render_template, Response +from mfrc522 import SimpleMFRC522 +from threading import Thread +from time import sleep from indication_driver import IndicationDriver from camera_driver import CameraDriver @@ -9,7 +12,7 @@ app = Flask(__name__) indication_driver = IndicationDriver() camera_driver = CameraDriver() -def stream(): +def _camera_stream(): global indication_driver global camera_driver last_codes = [] @@ -26,16 +29,45 @@ def stream(): indication_driver.indicate(trash_category) else: indication_driver.stop() - last_codes = codes + last_codes = codes yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + jpg + b'\r\n\r\n' @app.route('/') def index(): return render_template('index.html') -@app.route('/stream_feed') -def stream_feed(): - return Response(stream(), mimetype='multipart/x-mixed-replace; boundary=frame') +@app.route('/camera_stream') +def camera_stream_feed(): + return Response(_camera_stream(), mimetype='multipart/x-mixed-replace; boundary=frame') + +@app.route('/trash_category_stream') +def trash_category_stream(): + def generate(): + global indication_driver + while True: + if indication_driver.trash_category == "": + yield ("READY" + "\\n") + else: + yield (indication_driver.trash_category + "\\n") + sleep(.5) + return Response(generate(), mimetype='text/plain') + +def rfid_loop(): + print("RFID Loop started") + global indication_driver + reader = SimpleMFRC522() + last_id = 0 + while True: + id, _ = reader.read() + if id in config.RFID_LOOKUP_TABLE.keys(): + trash_category = config.RFID_LOOKUP_TABLE[id] + print(f"{id}: {trash_category}") + indication_driver.indicate(trash_category) + else: + indication_driver.stop() + print(id) + last_id = id if __name__ == '__main__': + Thread(target = rfid_loop).start() app.run(debug=False, port=5000, host='0.0.0.0') diff --git a/indication_driver.py b/indication_driver.py index eeb1f4c..e2c78d8 100644 --- a/indication_driver.py +++ b/indication_driver.py @@ -1,5 +1,5 @@ import neopixel -from time import sleep +import time from typing import List from threading import Thread @@ -18,15 +18,19 @@ class IndicationDriver: ) self._run_loop = True self._indication_loop = None + self.trash_category = "" self.stop() + def _clear(self): + self.pixels.fill((0,0,0)) + self.pixels.show() + self.trash_category = "" def stop(self): if self._indication_loop: self._run_loop = False self._indication_loop.join() - self.pixels.fill((0,0,0)) - self.pixels.show() + self._clear() def _get_trash_location(self, trash_type: str) -> List[int]: if trash_type in config.TRASH_LOCATION: @@ -37,6 +41,7 @@ class IndicationDriver: def indicate(self, trash_type: str): self.stop() + self.trash_category = trash_type location = self._get_trash_location(trash_type) def _indication_loop(self, location: List[int]): @@ -44,7 +49,8 @@ class IndicationDriver: self.pixels[idx] = (0, 255, 0) self.pixels.show() self._run_loop = True - while self._run_loop: + start_time = time.process_time() + while self._run_loop and time.process_time() < start_time + 10: for step in range (4): for idx in range(location[0]): if idx % 4 == step: @@ -52,8 +58,9 @@ class IndicationDriver: else: self.pixels[idx] = (0, 0, 0) self.pixels.show() - sleep(.1) + time.sleep(.1) + self._clear() self._indication_loop = Thread(target = _indication_loop, args = (self, location, )) self._indication_loop.start() - sleep(1) \ No newline at end of file + sleep(.1) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 2985d45..a0fd418 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,16 +2,51 @@ - 🔴 Streaming... + T.REx - - + + + + +
+
+ +

+ +
+
\ No newline at end of file