The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

3 Pages V < 1 2 3 >  
Reply to this topicStart new topic
> How to run .py in html
CharlesEF
post Mar 8 2016, 01:58 AM
Post #21


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Let me be sure I understand. You have a .py command that starts the moter and continues to run. You have another .py command to stop the motor. Is that correct? Can you post the actual exec() commands and the keys you want to activate them, a complete list for all commands you want to use.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 8 2016, 03:09 AM
Post #22


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



QUOTE
switch(status)
this was mistake,this is corect:
switch(key_press)
everything works perfectly,thank you a lot Charles,could you explin me how to use switch(key_Code) so i could use arrows
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 8 2016, 03:13 AM
Post #23


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



QUOTE(CharlesEF @ Mar 8 2016, 07:58 AM) *

CODE
switch(key_code)
{
  case '38':
   controlit('Up');
   break;
  case '40':
   controlit('Down');
   break;
  case '37':
   controlit('Left');
   break;
  case '39':
   controlit('Right');
   break;
}

(key_code)37=%(key_press) (Left arrow) if i write case '%'; left arrow works
38=& (Up arrow) if i write case '&'; up arrow works
39=' (Right arrow) if i write case '''; nothing works
40=( (Down arrow) if i write case '('; up arrow works
So i think solution is in using key_code :/

This post has been edited by Kristijan1392: Mar 8 2016, 03:41 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 8 2016, 05:25 AM
Post #24


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 8 2016, 02:13 AM) *

QUOTE(CharlesEF @ Mar 8 2016, 07:58 AM) *

CODE
switch(key_code)
{
  case '38':
   controlit('Up');
   break;
  case '40':
   controlit('Down');
   break;
  case '37':
   controlit('Left');
   break;
  case '39':
   controlit('Right');
   break;
}

(key_code)37=%(key_press) (Left arrow) if i write case '%'; left arrow works
38=& (Up arrow) if i write case '&'; up arrow works
39=' (Right arrow) if i write case '''; nothing works
40=( (Down arrow) if i write case '('; up arrow works
So i think solution is in using key_code :/

So, this means you don't want to catch the W or A key at all? Also, I don't think the onkeyup event will do what you want. Think about it, if you press the UP arrow and hold it the keyboard buffer will continue to hold that value. By the time you release the key you might have to wait for the keyboard buffer to empty before the stop action kicks in. You may be better off without the onkeyup function and just define another onkeydown key, like "S", to stop. If you do want to stick with the onkeyup function then key_code is NOT the way to go. Because then you have no way to tell when the key is released.

Here is my recent attempt, you can use the W key for UP, the A key for DOWN and the 4 arrow keys for UP, DOWN, LEFT and RIGHT. When you release the key the STOP command should kick in, after awhile, because of keyboard buffering.

control.php
CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control Module</title>
<script type="text/javascript">
document.onkeydown = function(event)
{
var key_press = 'd' + String.fromCharCode(event.which);
var key_code = event.keyCode;
kp.innerHTML = key_press;
kc.innerHTML = key_code;
stat.innerHTML = "DOWN Event Fired For : " + key_press;
controlit(key_press);
}

document.onkeyup = function(event)
{
var key_press = 'u' + String.fromCharCode(event.which);
var key_code = event.keyCode;
kp.innerHTML = key_press;
kc.innerHTML = key_code;
stat.innerHTML = "UP Event Fired For : " + key_press;
controlit(key_press);
}

function controlit(key)
{
switch(key)
{
  case "dW": // W Key - key press (Down)
  case "d&": // Up arrow key - key press (Down)
   action = 'Up';
   break;
  case "dA": // A key - key press (Down)
  case "d(": // Down arrow key - key press (Down)
   action = 'Down';
   break;
  case "d%": // Left arrow key - key press (Down)
   action = 'Left';
   break;
  case "d'": // Right arrow key - key press (Down)
   action = 'Right';
   break;
  case "uA": // A key - key up (Up)
  case "uW": // W Key - key up (Up)
  case "u%": // Left arrow key - key up (Up)
  case "u'": // Right arrow key - key up (Up)
  case "u&": // Up arrow key - key up (Up)
  case "u(": // Down arrow key - key up (Up)
   action = 'Stop';
   break;
}
var formData = new FormData();
formData.append("moveit", action);
var xhr = new XMLHttpRequest();
xhr.open("POST", "controlit.php", true);
xhr.send(formData);
}
</script>
</head>
<body>
<input type="button" name="up" title="Click to move up." value="Up" onclick="controlit(this.value);">
<input type="button" name="down" title="Click to move down." value="Down" onclick="controlit(this.value);">
<input type="button" name="left" title="Click to move left." value="Left" onclick="controlit(this.value);">
<input type="button" name="right" title="Click to move right." value="Right" onclick="controlit(this.value);"><br>
<h2>Javascript Capture Keyboard Input Example</h2>
<h3>onkeydown - onkeyup</h3>
Key Pressed : <span id="kp"></span><br>
Key Code : <span id="kc"></span><br>
<p id="stat">Keyboard Event Status</p>
<script type="text/javascript">
window.onload=function()
{
var action = null;
var kp = document.getElementById('kp');
var kc = document.getElementById('kc');
var stat = document.getElementById('stat');
}
</script>
</body>
</html>

controlit.php
CODE
<?php
if(isset($_POST['moveit']))
{
switch($_POST['moveit'])
{
  case 'Up':
   exec("sudo python /home/pi/up.py");
   break;
  case 'Down':
   exec("sudo python /home/pi/down.py");
   break;
  case 'Left':
   exec("sudo python /home/pi/left.py");
   break;
  case 'Right':
   exec("sudo python /home/pi/right.py");
   break;
  case 'Stop':
   exec("sudo python /home/pi/stop.py");
   break;
}
}
?>

Be sure to change the exec() commands to match your needs. Also, because of the changes the 4 buttons will not work anymore. They need to be changed a little but I leave that for you. If you need any help or have questions then just post again.

This post has been edited by CharlesEF: Mar 8 2016, 05:32 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 8 2016, 06:08 AM
Post #25


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



CharlesEF you are Godlike!! Thank you very very much,you are the best biggrin.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 8 2016, 06:10 AM
Post #26


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 8 2016, 05:08 AM) *

CharlesEF you are Godlike!! Thank you very very much,you are the best biggrin.gif

Does this mean the keyboard buffer was not a problem after all?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 8 2016, 06:27 AM
Post #27


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



yes,but everything else is working,i will look into the buttons

This post has been edited by Kristijan1392: Mar 8 2016, 06:33 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 8 2016, 06:32 AM
Post #28


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 8 2016, 05:27 AM) *

i guess it does,everything works great now,thank you a lot! smile.gif

Great, have fun. And you're welcome. If you need help with the buttons just post again.

This post has been edited by CharlesEF: Mar 8 2016, 06:33 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 8 2016, 03:23 PM
Post #29


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Here is a new version of control.php, which should address the keyboard buffer problem (I think). Also, I have changed the buttons so they should work like keydown and keyup. This version should allow you to hold the key or button and fire the stop command when you release. Give it a try and let me know if it is any better.
control.php
CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control Module</title>
<script type="text/javascript">
document.onkeydown = function(event)
{
var key_press = 'd' + String.fromCharCode(event.which);
var key_code = event.keyCode;
kp.innerHTML = key_press;
kc.innerHTML = key_code;
stat.innerHTML = "DOWN Event Fired For : " + key_press;
controlit(key_press);
}

document.onkeyup = function(event)
{
var key_press = 'u' + String.fromCharCode(event.which);
var key_code = event.keyCode;
kp.innerHTML = key_press;
kc.innerHTML = key_code;
stat.innerHTML = "UP Event Fired For : " + key_press;
controlit(key_press);
}

function controlit(key)
{
if(key == prekey)
{
  return(false);
}
switch(key)
{
  case "dW": // W Key - key press (Down)
  case "d&": // Up arrow key - key press (Down)
   action = 'Up';
   break;
  case "dA": // A key - key press (Down)
  case "d(": // Down arrow key - key press (Down)
   action = 'Down';
   break;
  case "d%": // Left arrow key - key press (Down)
   action = 'Left';
   break;
  case "d'": // Right arrow key - key press (Down)
   action = 'Right';
   break;
  case "uA": // A key - key up (Up)
  case "uW": // W Key - key up (Up)
  case "u%": // Left arrow key - key up (Up)
  case "u'": // Right arrow key - key up (Up)
  case "u&": // Up arrow key - key up (Up)
  case "u(": // Down arrow key - key up (Up)
   action = 'Stop';
   break;
}
prekey = key;
var formData = new FormData();
formData.append("moveit", action);
var xhr = new XMLHttpRequest();
xhr.open("POST", "controlit.php", true);
xhr.send(formData);
}
</script>
</head>
<body>
<input type="button" name="up" title="Click to move up." value="Up" onmousedown="controlit('d&');" onmouseup="controlit('u&');">
<input type="button" name="down" title="Click to move down." value="Down" onmousedown="controlit('d\(');" onmouseup="controlit('u\(');">
<input type="button" name="left" title="Click to move left." value="Left" onmousedown="controlit('d%');" onmouseup="controlit('u%');">
<input type="button" name="right" title="Click to move right." value="Right" onmousedown="controlit('d\'');" onmouseup="controlit('u\'');"><br>
<h2>Javascript Capture Keyboard Input Example</h2>
<h3>onkeydown - onkeyup</h3>
Key Pressed : <span id="kp"></span><br>
Key Code : <span id="kc"></span><br>
<p id="stat">Keyboard Event Status</p>
<script type="text/javascript">
var action = null, prekey = null, kp = null, kc = null, stat = null;

window.onload=function()
{
kp = document.getElementById('kp');
kc = document.getElementById('kc');
stat = document.getElementById('stat');
}
</script>
</body>
</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 11 2016, 08:07 PM
Post #30


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



Much better,earlier it would stuck if i held the key,now it works perfect,i have a feeling that you are doing the same thing as I,but you do it way better smile.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 12 2016, 01:34 AM
Post #31


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 11 2016, 07:07 PM) *

Much better,earlier it would stuck if i held the key,now it works perfect,i have a feeling that you are doing the same thing as I,but you do it way better smile.gif

Glad it worked out for you. If you have any questions about the code, just ask.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 12 2016, 09:19 AM
Post #32


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



can case be two buttons for example up left or up right arrows?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 12 2016, 02:03 PM
Post #33


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 12 2016, 08:19 AM) *

can case be two buttons for example up left or up right arrows?

Yes, it can. But it would also depend on what you want to happen. It would be better if you tell me what you want. Do you want up left to do 2 commands, up then left, or do you want up left to do the same command?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 12 2016, 03:05 PM
Post #34


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



to do the same command,3 motors on,one off
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 12 2016, 03:09 PM
Post #35


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



up turns all motors on forward,left turns one front motor on forward and one back motor on backward,up left turns 3 motors on forward etc..
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 13 2016, 09:24 AM
Post #36


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 12 2016, 03:09 PM) *

up turns all motors on forward,left turns one front motor on forward and one back motor on backward,up left turns 3 motors on forward etc..

Sorry, I still don't understand. What should happen if you press up and left together? Should both the up and left command be issued or do you have a different program to run for this?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 13 2016, 10:40 AM
Post #37


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



i have a different command if left and up are pressed together

This post has been edited by Kristijan1392: Mar 13 2016, 10:41 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 20 2016, 08:59 PM
Post #38


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Kristijan1392 @ Mar 13 2016, 10:40 AM) *

i have a different command if left and up are pressed together

Sorry for the delay, I was out of town and offline for the last week. Anyway, the reason I need to understand what you want to happen is because the current working script I posted will need the logic changed. Currently, the logic is based on a single key.

What if you approached this as the SHIFT key being used as the UP key and ALT key being used as the DOWN key? What should happen if only 1 key is released?

This post has been edited by CharlesEF: Mar 20 2016, 09:00 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 21 2016, 05:36 PM
Post #39


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



other command should execute,i will send you the new code and the part i want to change
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Kristijan1392
post Mar 21 2016, 06:08 PM
Post #40


Member
***

Group: Members
Posts: 32
Joined: 10-November 15
Member No.: 23,730



here are all cases
CODE
switch(key)
{
  case "d&": // Up arrow key - key press (Down)
   action = 'Up';
   break;
  case "d(": // Down arrow key - key press (Down)
   action = 'Down';
   break;
  case "d%": // Left arrow key - key press (Down)
   action = 'Left';
   break;
  case "d'": // Right arrow key - key press (Down)
   action = 'Right';
   break;
  case "dg": // Up and left Key press together - I want this case to execute different command but when i pres UP and LEFT arrows together,case "dg" should be replaced with case "d&" && "d%": action is good,and i would like to leave one key press for up down left and right,but two keys pressed for upleft,upright etc..
   action = 'UpLeft';
   break;
  case "di": // Up and right - key press (Down)  I want this case to execute different command but when i pres UP and RIGHT arrows together
   action = 'UpRight';
   break;
  case "da": // Down and left - key press (Down)  I want this case to execute different command but when i pres DOWN and LEFT arrows together
   action = 'DownLeft';
   break;
  case "dc": // Down and right - key press (Down)  I want this case to execute different command but when i pres DOWN and RIGHT arrows together
   action = 'DownRight';
   break;
  case "de": // Up and down - key press (Down)  I want this case to execute different command but when i pres UP and DOWN arrows together
   action = 'UpDown';
   break;
  case "ue": // Up and down - key up (Up)
  case "uc": //Down and right - key up (Up)
  case "ua": //Down and left - key up (Up)
  case "ui": // Up and right - key up (Up)
  case "ug": // Up and left Key up together   case "u&" && "u%": should be combined instead of case "ug":
  case "u%": // Left arrow key - key up (Up)
  case "u'": // Right arrow key - key up (Up)
  case "u&": // Up arrow key - key up (Up)
  case "u(": // Down arrow key - key up (Up)
   action = 'Stop';
   break;
}

Thank you very much for your time
Is it possible if i for example hold up and left together one action is executed,and when i release up only left action is executed,and when i release left stop is executed

This post has been edited by Kristijan1392: Mar 21 2016, 06:20 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

3 Pages V < 1 2 3 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 08:40 PM