如何继续打电话给佛罗里达州金贾2号的混蛋

2024-03-28 23:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,在你们说这是一个复制品之前,请耐心等我一分钟,相信我,当我说这个的时候,我试过了其他类似问题的所有答案。我想使用javascript从html页面调用runForward和runBackward。你知道吗

这是我的密码:

#motordriver.py

import time
import RPi.GPIO as gp
import os

class MotorDriver:
    en1 = 24
    c1a = 26
    c1b = 22
    en2 = 23
    c2a = 21
    c2b = 19

    def __init__(self):
        gp.setwarnings(False)
        gp.setmode(gp.BOARD)
        gp.setup(self.en1, gp.OUT)
        gp.setup(self.c1a, gp.OUT)
        gp.setup(self.c1b, gp.OUT)
        gp.setup(self.en2, gp.OUT)
        gp.setup(self.c2a, gp.OUT)
        gp.setup(self.c2b, gp.OUT)
        gp.output(self.en1, True)
        gp.output(self.en2, True)

    def runForward(self):
        gp.output(self.c1a, True)
        gp.output(self.c2a, True)
        time.sleep(1)
        gp.output(self.c1a, False)
        gp.output(self.c2a, False)
        return "sooo"

    def runBackward(self):
        gp.output(self.c1b, True)
        gp.output(self.c2b, True)
        time.sleep(1)
        gp.output(self.c1b, False)
        gp.output(self.c2b, False)
        return "booo"

if __name__ == '__main__':
    #app.run(host = '0.0.0.0')
    drive = raw_input('Enter forward or backward(a/b):')
    obj = MotorDriver()
    if drive == 'a':
        obj.runForward()
    elif drive == 'b':
        obj.runBackward()
    else:
        print("Wrong input")
    gp.output(obj.en1, False)
    gp.output(obj.en2, False)

我将这个文件作为一个模块导入到我的__init__.py文件中,下面是该文件的内容。你知道吗

from flask import Flask
from flask import abort, redirect, url_for, request
from flask import render_template
from jinja2 import Template
import motordriver as md

app = Flask(__name__)
app.debug = True
#'import yourmodule; instance = yourmodule.YourClass(); instance.method()'
inst = md.MotorDriver()

def clirunForward():
    inst.runForward()
def clirunBackward():
    inst.runBackward()

app.jinja_env.globals.update(clirunForward=clirunForward)
app.jinja_env.globals.update(clirunBackward=clirunBackward)

@app.route('/')
def showPageWithControls():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(host= '0.0.0.0')

现在终于我的模板index.html

<!doctype html>
<html>
<heead>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script>
  $(document).ready(function(){
      $(".back").mouseup(function(){
          $("div").after("<p style='color:green;'>{{ clirunBackward() }}</p>");
      });
      $(".up").mousedown(function(){
          $("div").after("<p style='color:purple;'>{{ clirunForward() }}</p>");
      });
  });
</script
  <title>
    Hello from PiBall
  </title>
</head>
<body>
  <h1>PiBall!</h1>
  <button type='button' class="up">&#8593;</button><br />
  <button type='button'>&#8592;</button><button type='button'>&#8594;</button><br />
  <button type='button' class="back">&#8595;</button><br />
  <div>Test it here!</div>
</body></html>

你们能告诉我为什么不行吗。根据其他答案,这应该行得通吧?你知道吗

顺便说一句,我试过了


Tags: fromimportselffalsetrueobjappoutput
1条回答
网友
1楼 · 发布于 2024-03-28 23:45:41

将post与Ajax结合使用是可行的。这是我的index.html

$(document).ready(function(){
    $(".up").mousedown(function(){
      $("div").after("<p style='color:green;'>Forward it is</p>");
      $.ajax({
        type: "POST",
          data: {motion: "forward"},
          dataType: 'text'
      });
    }).mouseup(function(){
        $("div").after("<p style='color:green;'>Stopped!</p>");
      $.ajax({
        type: "POST",
        data: {motion: "stop"},
        dataType: 'text'
      });
    });
    $(".back").mousedown(function(){
      $("div").after("<p style='color:purple;'>Forward it is</p>");
      $.ajax({
        type: "POST",
        data: {motion: "backward"},
        dataType: 'text'
      });
    }).mouseup(function(){
      $("div").after("<p style='color:green;'>Stopped!</p>");
      $.ajax({
        type: "POST",
        data: {motion: "stop"},
        dataType: 'text'
      });
    });
  });

这里是我处理帖子的__init__.py部分:

@app.route('/', methods = ['POST'])
def motorFunctions():
    if request.form['motion'] == "forward":
        inst.runForward()
    elif request.form['motion'] == "backward":
        inst.runBackward()

相关问题 更多 >