#!/usr/bin/env python from deps.samplebase import SampleBase from rgbmatrix import graphics from flask import Flask, request, jsonify from waitress import serve import time, threading api = Flask(__name__) @api.route('/text', methods=['POST']) def display_text(): response = { 'success': True } try: global matrix data = request.get_json() text = data.get('text') x_coord = data.get('x') y_coord = data.get('y') matrix.text(x_coord, y_coord, text) except Exception as e: print(e) response['success'] = False return jsonify(response) @api.route('/update', methods=['GET']) def update_matrix(): response = { 'success': True } try: global matrix matrix.update() except Exception as e: print(e) response['success'] = False return jsonify(response) class Matrix(SampleBase): def __init__(self, *args, **kwargs): super(Matrix, self).__init__(*args, **kwargs) def run(self): self.font = graphics.Font() self.font.LoadFont("deps/fonts/9x18B.bdf") self.color = graphics.Color(255, 255, 255) self.canvas = self.matrix.CreateFrameCanvas() self.canvas.Clear() while True: time.sleep(.05) def clear(self): self.canvas.Clear() def update(self): self.canvas = self.matrix.SwapOnVSync(self.canvas) self.clear() def text(self, x, y, t): graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, t) matrix = None if __name__ == "__main__": matrix = Matrix() threading.Thread(target=lambda: serve(api, host="0.0.0.0", port=8080)).start() if not matrix.process(): matrix.print_help()