From e9118e44865618b6c57f04b51917c4d1ca8e9d0d Mon Sep 17 00:00:00 2001 From: SKyoKen Date: Sun, 15 Mar 2020 12:12:43 +0800 Subject: [PATCH] Basic file --- command.py | 143 +++++++++++++++++++++++++++++++++++++++++++ file/command.txt | 0 file/message.txt | 1 + file/msg.txt | 31 ++++++++++ file/script.txt | 0 file/scriptcopy.txt | 97 +++++++++++++++++++++++++++++ run.py | 66 ++++++++++++++++++++ templates/index.html | 132 +++++++++++++++++++++++++++++++++++++++ web.py | 77 +++++++++++++++++++++++ 9 files changed, 547 insertions(+) create mode 100644 command.py create mode 100644 file/command.txt create mode 100644 file/message.txt create mode 100644 file/msg.txt create mode 100644 file/script.txt create mode 100644 file/scriptcopy.txt create mode 100755 run.py create mode 100644 templates/index.html create mode 100644 web.py diff --git a/command.py b/command.py new file mode 100644 index 0000000..ca819d1 --- /dev/null +++ b/command.py @@ -0,0 +1,143 @@ +import inspect +import logging +import asyncio + +from aioconsole import ainput + +from joycontrol.controller_state import button_push, ControllerState + +logger = logging.getLogger(__name__) + + +class ControllerCLI: + def __init__(self, controller_state: ControllerState): + self.controller_state = controller_state + self.available_buttons = self.controller_state.button_state.get_available_buttons() + self.available_sticks = {'ls','rs'} + self.script = False + + async def write(self,msg): + with open('file/message.txt','a') as f: + f.write(msg+'\n') + + async def get(self,file): + with open('file/'+file,'r') as f: + result = list() + for line in f.readlines(): #依次读取每行 + line = line.strip() #去掉每行头尾空白 + if not len(line) or line.startswith('#'): #判断是否是空行或注释行 + continue #是的话,跳过不处理 + result.append(line.lower()) #保存小写文字 + return result + async def clear(self,file): + with open('file/'+file,'w+') as f: + return f.truncate() + + async def runCommand(self): + user_input = await self.get('command.txt') + if not user_input: + return + await self.clear('command.txt') + + for command in user_input: + cmd, *args = command.split() + + if cmd == 'run': + self.script = True + elif cmd == 'stop': + self.script = False + elif cmd == 'off' or cmd =='on': + print('开/关 未完成') + else: + await self.pressButton(command) + return ' ' + async def pressButton(self,*commands): + for command in commands: + cmd,*args=command.split() + if cmd in self.available_sticks: + print('side = ',cmd,'direction = ',args[0]) + elif cmd in self.available_buttons: #按钮 + await button_push(self.controller_state,cmd) + elif cmd.isdecimal(): #等待(ms) + await asyncio.sleep(float(cmd)/1000) + elif cmd == 'print': + print(args[0]) + else: #错误代码 + print('command',cmd,'not found') + async def readCommand(self,file): + user_input = await self.get(file) + if not user_input: + return + await self.clear(file) + def isCommand(self,cmd): + return cmd in self.available_sticks or cmd in self.available_buttons or cmd.isdecimal() or cmd =='print' + + def forCheck(self,n,user_input): + commands = [] + until = -1 + for i in range(len(user_input)): + if i <= n or i<= until: + continue + + cmd,*args = user_input[i].split() + if cmd == 'for': + for _ in range(int(args[0])): + until,forcmd = self.forCheck(i,user_input) + for get in forcmd: + commands.append(get) + elif cmd == 'next': + return i,commands + elif self.isCommand(cmd): + commands.append(user_input[i]) + else: + print('command',cmd,'not found') + + async def runScript(self): + user_input = await self.get('script.txt') + if not user_input: + return + await self.clear('script.txt') + + commands=[] + until=-1 + for i in range(len(user_input)): + #检测按键 + await self.runCommand() + #确认脚本是否要停止 + if self.script == False: + return + + if i <= until: + continue + + cmd, *args =user_input[i].split() + if cmd == 'for': + for _ in range(int(args[0])): + until,forcmd = self.forCheck(i,user_input) + for get in forcmd: + commands.append(get) + elif self.isCommand(cmd): + commands.append(user_input[i]) + else: + print('commands',cmd,'not found') + + for command in commands: + await self.runCommand() + if self.script == False: + return + + await self.pressButton(command) + + self.script = False + + async def run(self): + + while True: + #等待输入 + cmd = await self.runCommand() + if cmd == 'exit': + return + if self.script == True: + await self.runScript() + + diff --git a/file/command.txt b/file/command.txt new file mode 100644 index 0000000..e69de29 diff --git a/file/message.txt b/file/message.txt new file mode 100644 index 0000000..e6907c3 --- /dev/null +++ b/file/message.txt @@ -0,0 +1 @@ +该功能暂时无法使用 diff --git a/file/msg.txt b/file/msg.txt new file mode 100644 index 0000000..5b00343 --- /dev/null +++ b/file/msg.txt @@ -0,0 +1,31 @@ +使用方法 + +所有代码不区分大小写,支持前置后置空格,支持行内注释。 + +【注释】 +语法:# 注释内容 + +【输出】 +语法:PRINT 输出内容 +暂不显示 + +【按键】 +语法:键位 [持续时间(ms)|DOWN|UP] +(键位可以取A、B、X、Y、L、R、ZL、ZR、MINUS(-)、PLUS(+)、LCLICK(按左摇杆)、RCLICK(按右摇杆)、HOME(返回系统)、CAPTURE(截屏)、LEFT、RIGHT、UP、DOWN;持续时间省略则为默认50ms) +示例:A(按下A键50毫秒) + +【摇杆】 +暂时无法使用 + +【等待】 +语法:等待时间(ms) +示例:2000 + +【循环】 +语法: +FOR 循环次数 +... +NEXT + + +脚本参考铃落大佬的伊机控写的 diff --git a/file/script.txt b/file/script.txt new file mode 100644 index 0000000..e69de29 diff --git a/file/scriptcopy.txt b/file/scriptcopy.txt new file mode 100644 index 0000000..2c712c0 --- /dev/null +++ b/file/scriptcopy.txt @@ -0,0 +1,97 @@ +#<<剑盾自动放生>> +#———————准备工作——————— +# 语速——快; +# 对战动画——不看; +# 比赛规则——连战; +# 自动记录——关闭; +# 小轻松模式——关闭; +# 跳过动画——开启; +#———————使用方法——————— +#1.打开游戏菜单进入宝可梦队伍 +#2.按R进入箱子选到需要放生的那个箱子上(请保证有一整箱要放生的精灵) +#3.光标对准要放生的第一箱的第一个精灵,点击RUN即可 +#———————注意事项——————— +#想放生的箱子都是满的并且是可以放生的精灵 +#运行前需要修改下面的是放生箱数 +#—————这数字是放生箱数————— +for 1 +#—————————————— + for 3 + a + 800 + up + 100 + up + 100 + a + 1000 + up + 100 + a + 1000 + a + 500 + for 4 + down + 100 + a + 800 + up + 100 + up + 100 + a + 1000 + up + 100 + a + 1000 + a + 500 + next + right + 100 + a + 800 + up + 100 + up + 100 + a + 1000 + up + 100 + a + 1000 + a + 500 + for 4 + up + 100 + a + 800 + up + 100 + up + 100 + a + 1000 + up + 100 + a + 1000 + a + 500 + next + right + 100 + next + left + 100 + l + 100 + for 5 + left + 100 + next +next \ No newline at end of file diff --git a/run.py b/run.py new file mode 100755 index 0000000..cf3a5ac --- /dev/null +++ b/run.py @@ -0,0 +1,66 @@ +import argparse +import asyncio +import logging +import os +from contextlib import contextmanager + +from joycontrol import logging_default as log +from joycontrol.command_line_interface import ControllerCLI +from joycontrol.controller import Controller +from joycontrol.memory import FlashMemory +from joycontrol.protocol import controller_protocol_factory +from joycontrol.server import create_hid_server +import command +logger = logging.getLogger(__name__) + + +async def _main(controller, capture_file=None, spi_flash=None): + factory = controller_protocol_factory(controller, spi_flash=spi_flash) + transport, protocol = await create_hid_server(factory, 17, 19, capture_file=capture_file) + + controller_state = protocol.get_controller_state() + + cli = command.ControllerCLI(controller_state) + await controller_state.connect() + await cli.run() + + logger.info('Stopping communication...') + await transport.close() + + +if __name__ == '__main__': + # check if root + if not os.geteuid() == 0: + raise PermissionError('Script must be run as root!') + + # setup logging + log.configure() + + parser = argparse.ArgumentParser() + parser.add_argument('-l', '--log') + parser.add_argument('--spi_flash') + args = parser.parse_args() + + controller = Controller.PRO_CONTROLLER + + spi_flash = None + if args.spi_flash: + with open(args.spi_flash, 'rb') as spi_flash_file: + spi_flash = spi_flash_file.read() + + # creates file if arg is given + @contextmanager + def get_output(path=None): + """ + Opens file if path is given + """ + if path is not None: + file = open(path, 'wb') + yield file + file.close() + else: + yield None + + with get_output(args.log) as capture_file: + loop = asyncio.get_event_loop() + loop.run_until_complete(_main(controller, capture_file=capture_file, spi_flash=spi_flash)) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6134e14 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,132 @@ + + RasCon ver.1.0 + + + +
+ + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Virtual Controller
+
+
+ +
+
+

Script

+
+
+ + +
+
+
+ + + + + + + + diff --git a/web.py b/web.py new file mode 100644 index 0000000..b5d5440 --- /dev/null +++ b/web.py @@ -0,0 +1,77 @@ +from flask import Flask,render_template,request +import os + +app = Flask(__name__) +script = "" + +def default(): + msg = read('msg.txt') + script = read('scriptcopy.txt') + return msg,script + +def read(file): + with open('file/'+file,'r') as f: + return f.read() +def write(file,msg): + with open('file/'+file,'w') as f: + f.write(msg) +def clear(file): + with open('file/'+file,'w+') as f: + f.truncate() + +@app.route('/') +def index(): + msg,script=default() + return render_template('index.html',msg = msg,script =script) + +@app.route('/bluez',methods=['POST']) +def bluez(): + if request.method == 'POST': + #暂时无法使用 + #write('message.txt'request.form['btn']+'\n') + write('message.txt','该功能暂时无法使用\n') + write('command.txt',request.form['btn']) + + msg,script=default() + return render_template('index.html',msg = msg,script =script) + +@app.route('/btn',methods=['POST']) +def btn(): + if request.method == 'POST': + btn = request.form['btn'] + write('command.txt',btn) + + msg,script=default() + return render_template('index.html',msg = msg,script =script) + +@app.route('/script/run',methods=['POST']) +def run(): + if request.method == 'POST': + script = request.form['script'] + write('script.txt',script) + write('scriptcopy.txt',script) + write('command.txt','run') + + msg,script=default() + return render_template('index.html',msg = msg,script =script) + +@app.route('/script/stop',methods=['POST']) +def stop(): + if request.method == 'POST': + write('command.txt','stop') + + msg,script=default() + return render_template('index.html',msg = msg,script =script) + +#raspi +@app.route('/raspi',methods=['POST']) +def raspi(): + if request.method == 'POST': + cmd = request.form['btn'] + os.system(cmd) + +if __name__ == '__main__': + clear('message.txt') + clear('command.txt') + clear('script.txt') + app.run(debug=True,host='0.0.0.0')