Skip to content

Commit

Permalink
Added Runner bat and Updated Handle Script
Browse files Browse the repository at this point in the history
Added runnner batch files for user's ease of executing this program. Also, added a parameter to main.py for speed.
  • Loading branch information
isu-kim committed Apr 26, 2020
1 parent 6a6f2f6 commit a368d6c
Show file tree
Hide file tree
Showing 19 changed files with 698 additions and 13 deletions.
2 changes: 2 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/RainbowAll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@start "" /B %SystemRoot%\System32\cmd.exe /C %cd%/cppSide/AllPheriperalCPP.exe
@start "" /B %SystemRoot%\System32\cmd.exe /C python %cd%/pythonSide/main.py -easteregg false -operationmode 2 -speed 0.005
2 changes: 2 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/ScreenReactive.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@start "" /B %SystemRoot%\System32\cmd.exe /C %cd%/cppSide/AllPheriperalCPP.exe
@start "" /B %SystemRoot%\System32\cmd.exe /C python %cd%/pythonSide/main.py -easteregg false -operationmode 1
Binary file not shown.
Binary file not shown.
67 changes: 67 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/pythonSide/Bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from socket import *


class Bridge():
"""
A class that manages all connections to server
"""
def __init__(self, mode, port=37716):
"""
An intializer method for class Bridge
@params {int} Operation mode to send to server
@params {int} Port number to send communications through. 37716 Default
"""
self.operation_mode = mode
self.port = port
self.client_sock = socket(AF_INET, SOCK_STREAM)

self.connect_server()
self.send_operation_mode()

def connect_server(self):
"""
A method that connects to server for the first time.
@params {void}
@returns {void}
"""
print("[Client] Connecting to port : " + str(self.port))

try:
self.client_sock.connect(('127.0.0.1', self.port))

except ConnectionRefusedError: # the case when server is closed
print("[ERROR] Cannot connect to server.")
print("[ERROR] Please check if server engine is running")
exit(0)

def send_operation_mode(self):
"""
A method that sends server which operation mode would be used.
@params {void}
@returns {void}
"""
print("[Client] Sent operation mode : " + str(self.operation_mode))

operation_mode = bytearray()
operation_mode.append(self.operation_mode)
self.client_sock.send(operation_mode)

def send_rgb_value(self, r, g, b):
"""
A method that sends server RGB value.
@params {int} Red value, 0 ~ 255
@params {int} Green value, 0 ~ 255
@params {int} Blue value, 0 ~ 255
@returns {void}
"""
send_byte = bytearray()
send_byte.append(r)
send_byte.append(g)
send_byte.append(b)

try:
self.client_sock.send(send_byte)

except BrokenPipeError: # the case when server is closed.
print("[Server] The Server is closed!")
exit(0)
50 changes: 50 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/pythonSide/RainbowAll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import time
import Bridge


class RainbowAll:
"""
This class is for doing all the jobs related to rainbow all.
"""
def __init__(self, BridgeObj, speed=0.005):
"""
An intializer method for class RainbowAll
@params {Bridge} a Bridge object for server connection
@params {float} the duration of each led transitions.
@returns {void}
"""
self.speed = speed
self.BridgeObj = BridgeObj
print("\n[RainbowAll] Mode Initialized. Speed : " + str(speed))

def rainbow_all(self):
"""
A method for setting all the leds into one color and rainbow shfts.
@params {void}
@returns {void}
"""
while True:
for g in range(0, 255, 1):
self.BridgeObj.send_rgb_value(255, g, 0)
time.sleep(self.speed)

for r in range(255, 0, -1):
self.BridgeObj.send_rgb_value(r, 255, 0)
time.sleep(self.speed)

for b in range(0, 255, 1):
self.BridgeObj.send_rgb_value(0, 255, b)
time.sleep(self.speed)

for g in range(255, 0, -1):
self.BridgeObj.send_rgb_value(0, g, 255)
time.sleep(self.speed)

for r in range(0, 255, 1):
self.BridgeObj.send_rgb_value(r, 0, 255)
time.sleep(self.speed)

for b in range(255, 0, -1):
self.BridgeObj.send_rgb_value(255, 0, b)
time.sleep(self.speed)

53 changes: 53 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/pythonSide/ScreenReactive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import mss
from PIL import Image
import numpy as np
import scipy.cluster
import time


class ScreenReactive:
"""
A class that manages all actions related to Screen Reactive Script
"""
def __init__(self, period=0):
"""
An initializer method for class Screen Reactive.
@params {float} the peroid for each screen detection
@returns {void}
"""
self.period = period
print("\n[ScreenReactive] Mode Initialized. Delay : " + str(period))

def return_rgb(self):
"""
A method that gets screen data and returns most dominant color
@params {void}
@returns {list} a list that includes R, G, B value of dominant color
"""
with mss.mss() as sct:
# Get rid of the first, as it represents the "All in One" monitor:
for num, monitor in enumerate(sct.monitors[1:], 1):
# Get raw pixels from the screen
sct_img = sct.grab(monitor)

# Create the Image
im = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
im = im.resize((150, 150)) # optional, to reduce time

num_clusters = 1

ar = np.asarray(im)
shape = ar.shape
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)

codes, dist = scipy.cluster.vq.kmeans(ar, num_clusters)
# Codes are stored as a matrix
# 0 : R , 1 : G , 2 : B

for i in range(len(codes)):
lst = list()
lst.append(int(codes[i][0]))
lst.append(int(codes[i][1]))
lst.append(int(codes[i][2]))
time.sleep(self.period)
return lst
166 changes: 166 additions & 0 deletions WindowsSupport/ForGitHubRelease/x64/pythonSide/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import ScreenReactive
import RainbowAll
import Bridge
import sys


def print_logo(easter_egg_on):
"""
This function is for printing out our lovely GUI title.
@param{bool} Want easteregg? set as true
@returns {void}
"""
if not easter_egg_on:
print(" █████╗ ██╗ ██╗ ██████╗ ███████╗██████╗ ██╗██████╗ ██╗ ██╗███████╗██████╗ █████╗ ██╗ ███████╗")
print("██╔══██╗██║ ██║ ██╔══██╗██╔════╝██╔══██╗██║██╔══██╗██║ ██║██╔════╝██╔══██╗██╔══██╗██║ ██╔════╝")
print("███████║██║ ██║ ██████╔╝█████╗ ██████╔╝██║██████╔╝███████║█████╗ ██████╔╝███████║██║ ███████╗")
print("██╔══██║██║ ██║ ██╔═══╝ ██╔══╝ ██╔══██╗██║██╔═══╝ ██╔══██║██╔══╝ ██╔══██╗██╔══██║██║ ╚════██║")
print("██║ ██║███████╗███████╗██║ ███████╗██║ ██║██║██║ ██║ ██║███████╗██║ ██║██║ ██║███████╗███████║")
print("╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝")
print(" MacOS PythonSide Script")
print(" By Gooday2die")
print(" Please Check My Github : https://github.com/gooday2die/AllPeripherals")

else:
print(" ▄▄▄ ██▓ ██▓ ██▓███ ▓█████ ██▀███ ██▓ ██▓███ ██░ ██ ▓█████ ██▀███ ▄▄▄ ██▓ ██████ ")
print("▒████▄ ▓██▒ ▓██▒ ▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒▓██▒▓██░ ██▒▓██░ ██▒▓█ ▀ ▓██ ▒ ██▒▒████▄ ▓██▒ ▒██ ▒ ")
print("▒██ ▀█▄ ▒██░ ▒██░ ▓██░ ██▓▒▒███ ▓██ ░▄█ ▒▒██▒▓██░ ██▓▒▒██▀▀██░▒███ ▓██ ░▄█ ▒▒██ ▀█▄ ▒██░ ░ ▓██▄ ")
print("░██▄▄▄▄██ ▒██░ ▒██░ ▒██▄█▓▒ ▒▒▓█ ▄ ▒██▀▀█▄ ░██░▒██▄█▓▒ ▒░▓█ ░██ ▒▓█ ▄ ▒██▀▀█▄ ░██▄▄▄▄██ ▒██░ ▒ ██▒")
print(" ▓█ ▓██▒░██████▒░██████▒▒██▒ ░ ░░▒████▒░██▓ ▒██▒░██░▒██▒ ░ ░░▓█▒░██▓░▒████▒░██▓ ▒██▒ ▓█ ▓██▒░██████▒▒██████▒▒")
print(" ▒▒ ▓▒█░░ ▒░▓ ░░ ▒░▓ ░▒▓▒░ ░ ░░░ ▒░ ░░ ▒▓ ░▒▓░░▓ ▒▓▒░ ░ ░ ▒ ░░▒░▒░░ ▒░ ░░ ▒▓ ░▒▓░ ▒▒ ▓▒█░░ ▒░▓ ░▒ ▒▓▒ ▒ ░")
print(" ▒ ▒▒ ░░ ░ ▒ ░░ ░ ▒ ░░▒ ░ ░ ░ ░ ░▒ ░ ▒░ ▒ ░░▒ ░ ▒ ░▒░ ░ ░ ░ ░ ░▒ ░ ▒░ ▒ ▒▒ ░░ ░ ▒ ░░ ░▒ ░ ░")
print(" ░ ▒ ░ ░ ░ ░ ░░ ░ ░░ ░ ▒ ░░░ ░ ░░ ░ ░ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ")
print(" ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ")
print(" MacOS PythonSide Script")
print(" By Gooday2die")
print(" Please Check My Github : https://github.com/gooday2die/AllPeripherals")


def parse_argv():
"""
This function is for pasring argvs from system call.
The argv call would be in this format
python3 main.py -easteregg true -operationmode 1
@param {void}
@returns {list} list of argvs
"""
argv_list = sys.argv
return_params = list()

positive = ['True', 'true', 'on', 'ON', 'On']
valid_op_mode = [1, 2]

# Operation mode 1 : Screen reactive
# Operation mode 2 : Rainbow All
# Others : not implemented yet...

easter_egg_param = bool()
operation_mode_param = int()
speed_param = float()

if len(argv_list) < 2: # When we do not have any params
pass

elif '-help' in argv_list: # When we entered help param
print("Please check PARAMETERS.txt in this directory")
exit(0)

else: # When we have parameters
try:
easter_egg_index = argv_list.index("-easteregg")
easter_egg_param = argv_list[easter_egg_index + 1] in positive

operation_mode_index = argv_list.index("-operationmode")
operation_mode_param = int(argv_list[operation_mode_index + 1])

speed_index = argv_list.index("-speed")
speed_param = int(argv_list[speed_index] + 1)

if operation_mode_param not in valid_op_mode:
print("[ERROR] Invalid operation mode")
exit(0)
except:
pass

return_params.append(easter_egg_param)
return_params.append(operation_mode_param)
return_params.append(speed_param)

return return_params


def screen_reactive():
"""
This function is for doing all the screen reactive jobs in one function
@param {void}
@returns {void}
"""
bridge_obj = Bridge.Bridge(1)
screen_reactive_obj = ScreenReactive.ScreenReactive()

while True:
rgb_list = screen_reactive_obj.return_rgb()
bridge_obj.send_rgb_value(rgb_list[0], rgb_list[1], rgb_list[2])


def rainbow_all(speed):
"""
This function is for doing all the rainbow all jobs in one function
@param {void}
@returns {void}
"""
bridge_obj = Bridge.Bridge(2)

rainbow_obj = RainbowAll.RainbowAll(bridge_obj, speed)
rainbow_obj.rainbow_all()


if __name__ == "__main__":
params = parse_argv()
print(params)

print_logo(params[0])


if params[1] == 1:
print("[OperationMode] Set Operation Mode 1 : Screen Reactive")
screen_reactive()

elif params[1] == 2:
print("[OperationMode] Set Operation Mode 2 : Rainbow All")
rainbow_all(params[2])

else: # Called this script without parmeters
print("Please Select Operation Mode")
print("1 ScreenReactive")
print("2 RainbowAll")

while True:
try:
op_mode = int(input("Mode : "))
break
except ValueError:
pass

if op_mode == 1:
print("[OperationMode] Set Operation Mode 1 : Screen Reactive")
screen_reactive()

elif op_mode == 2:
print("[OperationMode] Set Operation Mode 2 : Rainbow All")
while True: # This part is for error and exception detection.
try:
speed = float(input("Speed (default 0.005) : "))
break

except ValueError: # if someone types 12swergw as input...
pass

rainbow_all(speed)

else:
print("[ERROR] Not implemented")
exit(0)
2 changes: 2 additions & 0 deletions WindowsSupport/ForGitHubRelease/x86/RainbowAll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@start "" /B %SystemRoot%\System32\cmd.exe /C %cd%/cppSide/AllPheriperalCPP.exe
@start "" /B %SystemRoot%\System32\cmd.exe /C python %cd%/pythonSide/main.py -easteregg false -operationmode 2 -speed 0.005
2 changes: 2 additions & 0 deletions WindowsSupport/ForGitHubRelease/x86/ScreenReactive.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@start "" /B %SystemRoot%\System32\cmd.exe /C %cd%/cppSide/AllPheriperalCPP.exe
@start "" /B %SystemRoot%\System32\cmd.exe /C python %cd%/pythonSide/main.py -easteregg false -operationmode 1
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a368d6c

Please sign in to comment.