alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Need help with my shutdown code

Fri Jan 27, 2017 2:19 pm

This doesn't work and I have no idea why? The "os.system("sudo shutdown now -P")" and "raise SystemExit" don't work? The b up down works though, the LED matrix brightness goes up down when I press the joystick up down.

Code: Select all

def clamp(value, min_value=75, max_value=255):
    return min(max_value, max(min_value, value))

def pushed_up(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b - 5)

def pushed_down(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b + 5)

def pushed_left(event):
    if event.action != ACTION_RELEASED:
        os.system("sudo shutdown now -P")
        
def pushed_right(event):
    if event.action != ACTION_RELEASED:
        raise SystemExit        

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right
If I do it this way it does though.

Code: Select all

b=(200) # brightness
s=(0.065) # scroll speed
x=(100)

def clamp(value, min_value=75, max_value=255):
    return min(max_value, max(min_value, value))

def pushed_up(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b - 5)

def pushed_down(event):
    global b
    if event.action != ACTION_PRESSED:
        b = clamp(b + 5)

def pushed_left(event):
    global x
    if event.action != ACTION_PRESSED:
        x = clamp(x + 10)
        
def pushed_right(event):
    global x
    if event.action != ACTION_PRESSED:
        x = clamp(x - 10)        

sense.stick.direction_up = pushed_up
sense.stick.direction_down = pushed_down
sense.stick.direction_left = pushed_left
sense.stick.direction_right = pushed_right

    if x > 100:
        os.system("sudo shutdown now -P")
    elif x < 100:
        raise SystemExit
Having to do it the second way just seems so lame? What I know about Python I learned by trial and error. I'm an armature, just so you know. ;)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Fri Jan 27, 2017 3:37 pm

Try adding some debugging. e.g.

Code: Select all

def pushed_left(event):
    print "Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED)
    if event.action != ACTION_RELEASED:
        os.system("sudo shutdown now -P")
Run your script and try to trigger this function. If this comes up False then you know why os.system isn't firing.

EDIT: your second code uses ACTION_PRESSED for all events but your first has some ACTION_RELEASEDs. Could that be your problem?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:13 pm

I originally tried pressed, then tried released. That's why it says pressed in one and released in the other. It didn't make any difference. They do work differently though. With pressed, if I hold the joystick in that direction its like holding the key down on your keyboard. It does multiply presses as long as you hold it. Released just does the one action when you let go. That's how it seems anyway.

If I hold the stick up it goes to full bright, hold it down it goes to full dim. Give it one short move up or down and it adjusts slightly.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:25 pm

The only difference between the two, other than the last line, is the global b line, and I'm not sure that's even needed at all?

I'll give your code a try when I get a chance. Can't say as I understand what it does though. Python noob.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:31 pm

The global b line won't make a difference.

I'll explain my code. You originally had:

Code: Select all

if event.action != ACTION_RELEASED:
This checks whether the "action" attribute is different to ACTION_RELEASED. If they are different then the test is True and the code in your "if" block is executed. However, as you say the shutdown code isn't being triggered, my concern is that the condition is being met. So, to debug it, I added this line:

Code: Select all

    print "Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED)
. The ".format" bit basically replaces the curly brackets in the string with the output of "event.action != ACTION_RELEASED". This will either be True or False. If it's False then you know why your shutdown code is not being triggered.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:39 pm

"This checks whether the "action" attribute is different to ACTION_RELEASED."

Shouldn't that be "the same as"' ACTION_RELEASED?

if event.action != ACTION_RELEASED: >> = <<

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:40 pm

No "!=" means not equal to. The same as would be "==".
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:42 pm

Ok so maybe all I need to do is change != to == then?
Last edited by alphanumeric on Fri Jan 27, 2017 4:46 pm, edited 1 time in total.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 4:45 pm

I was using this for reference, https://pythonhosted.org/sense-hat/api/#joystick I didn't know != was not equalt to, I thought the statement as written meant = to?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Fri Jan 27, 2017 5:06 pm

I don't have a sense hat so can't comment too much with any certainty but my guess would be that the code is meant to be triggered while the joystick is held in a certain position (and so will trigger repeatedly) but the ACTION_RELEASED event (which presumably is triggered when the joystick is released) is ignored by the function.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 5:23 pm

They call it a joystick but its really more like the hat switch on a game controller. It's just a switch with up, down, right, left, and center push. Whether its being pushed into or released from a position is detectable. According to this anyway, https://pythonhosted.org/sense-hat/api/#joystick If I knew python better it likely would make more sense.

I changed the != to == but it didn't make any difference. Brightness up down works but the other two functions don't. The switch movements are being detected, otherwise b and x wouldn't be changing values. For now I'm just going to change it back to what works and take a break, I've been at it most of the day and my backs getting sore so Time to take a break.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Fri Jan 27, 2017 5:26 pm

Have a rest!

Once you're rested, put my print statement in and let me know what gets printed when you push and release the button.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Fri Jan 27, 2017 5:30 pm

Wii do, thanks.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Mon Jan 30, 2017 3:57 pm

Invalid syntax, the second " between the two .. is highlighted in red.

print "Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED)

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help with my shutdown code

Mon Jan 30, 2017 4:03 pm

alphanumeric wrote:Invalid syntax, the second " between the two .. is highlighted in red.

print "Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED)
Do you use Python 3?
If so then the print statement should be:

Code: Select all

    print("Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED))
Last edited by DirkS on Mon Jan 30, 2017 4:15 pm, edited 1 time in total.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Mon Jan 30, 2017 4:04 pm

yes python 3

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Mon Jan 30, 2017 4:13 pm

invalid syntax last : in the next line.
if event.action != ACTION_RELEASED:

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help with my shutdown code

Mon Jan 30, 2017 4:20 pm

alphanumeric wrote:invalid syntax last : in the next line.
if event.action != ACTION_RELEASED:
I just noticed that the bracket at the end of the new print statement was outside the code block. That probably caused the new error.
I edited my post above (notice the 2 ')' at the end of the statement).

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Mon Jan 30, 2017 4:40 pm

DirkS wrote:
alphanumeric wrote:Invalid syntax, the second " between the two .. is highlighted in red.

print "Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED)
Do you use Python 3?
If so then the print statement should be:

Code: Select all

    print("Pushed Left. Event condition is {}.".format(event.action != ACTION_RELEASED))
Sorry - I'm not a good person to be giving python code samples. I do everything in python 2.7 (I know I shouldn't...) so I default to using that when helping out here.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Mon Jan 30, 2017 5:17 pm

Ok weird things are happing? It works, shuts down, then it doesn't work? No edits done between it working and not working? It's just not repeatable. May be different depending on whether it auto runs on boot or I launch it from idle? Moving the joystick is also moving the cursor on screen in the open idle edit box? I've had weird things like this happen before too, when using evdev for the joystick functions. If I didn't shut down my script hitting enter on my keyboard would shut down the Pi. I originally had the center button press mapped as the shutdown. For now I'm just going back to what I have working. One advantage to doing my commands at the end of my file is the display is blank when they are executed. When it worked with your code the message just stopped moving (froze) and stayed on screen. I had to unplug the power pack to clear it. Not what I want,

Don't get me wrong, I really appreciate your time and effort in this. But at this time I think I'll just go back to my old code. It looks amateurish (to me anyway lol) but it works. Thanks again. If I do have another crack at it I will post back to this thread.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Need help with my shutdown code

Mon Jan 30, 2017 5:33 pm

When it worked, the Pi presumably shut down as you intended, hence the need to recycle power.
Don't get me wrong, I really appreciate your time and effort in this. But at this time I think I'll just go back to my old code. It looks amateurish (to me anyway lol) but it works. Thanks again. If I do have another crack at it I will post back to this thread.
Don't worry, I don't take any offence at all! If I had a choice between code that works and code that doesn't, I know what I would choose.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

alphanumeric
Posts: 2566
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Need help with my shutdown code

Mon Jan 30, 2017 5:53 pm

I have a 2 pin header soldered to the reset pins. I just momentarily short them when I want it to boot up again. Most of my Pi's have a momentary push button wired to the reset pins. My KODI Pi like this one, I just shut down and leave plugged in. Then when I want to use them I tap the reset button once. This Pi normally runs 24/7 and is hardly ever shut down. It's running headless thus the need for the shutdown code. When I'm tinkering with my code I shut down. Then move it to a TV to use for a monitor. Since my file auto runs every time I turn it on I also have the code just to kill the running program but not shut down Raspbian. that way I can launch idle and play with my test file and not screw up my working file. If I don't get any syntax errors running it manually, I rename my files so the test file runs on boot up as a final test. If that works I leave it as is. if it doesn't I rename them back to the original names. Wash rinse and repeat lol.

Return to “Python”