Skip to content

Instantly share code, notes, and snippets.

@jparrill
Created May 29, 2022 11:05
Show Gist options
  • Save jparrill/8ed1465ec5b6b6f0ee81f11ee1b1b05b to your computer and use it in GitHub Desktop.
Save jparrill/8ed1465ec5b6b6f0ee81f11ee1b1b05b to your computer and use it in GitHub Desktop.
LuxafOS Script for RasPi and GPIO
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
from flask import Flask
from flask_restful import Resource, Api
class LuxaOS(Resource):
def put(self, state):
activateLuxaOS(state)
def get(self, state):
return state
def maping(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def set_color(color):
R_val = (color & 0xFF0000) >> 16
G_val = (color & 0x00FF00) >> 8
B_val = (color & 0x0000FF) >> 0
R_val = int(maping(R_val, 0, 255, 0, 1))
G_val = int(maping(G_val, 0, 255, 0, 1))
B_val = int(maping(B_val, 0, 255, 0, 1))
GPIO.output(pins['Red'], R_val)
GPIO.output(pins['Green'], G_val)
GPIO.output(pins['Blue'], B_val)
def reset():
for i in pins:
GPIO.output(pins[i], 0)
def activateLuxaOS(state):
try:
GPIO.setmode(GPIO.BCM)
for i in pins:
GPIO.setup(pins[i], GPIO.OUT, initial=GPIO.HIGH)
reset()
if int(state) == 0:
color = 0xFF0000
set_color(color)
elif int(state) == 1:
color = 0x00FF00
set_color(color)
except Exception as e:
print(e)
# Base App
app = Flask(__name__)
api = Api(app)
api.add_resource(LuxaOS, '/office_door/<state>')
# Setting the colors and pins
COLORS = [0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF, 0xFF00FF, 0xFFFF00, 0xFFFFFF, 0xB695C0]
pins = {'Red': 22, 'Green': 17, 'Blue': 27}
if __name__ == '__main__':
app.run(debug=True)
reset()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment