Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Extended comments on example for callback_ping_config_and_read #18

Closed
dkurok opened this issue Jul 27, 2015 · 1 comment
Closed

Extended comments on example for callback_ping_config_and_read #18

dkurok opened this issue Jul 27, 2015 · 1 comment

Comments

@dkurok
Copy link

dkurok commented Jul 27, 2015

Hello,
I also tested the callback-example for Sonar and did some extended comments and also mentioned that the signal_handler doe Ctrl-C will not work very well.
If you like, you can replace it in your example or add it as a new one.
sorry for not branching/pull request - not so clear to me at the moment (new to git)

#!/usr/bin/env python
"""
Copyright (c) 2013-2015 Alan Yorinks All rights reserved.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU  General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

You should have received a copy of the GNU  General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
import time
import sys
import signal

from PyMata.pymata import PyMata

# Ping callback function
def cb_ping(data):
    # will always print 11, because first element in data is firmata.SONAR = 0xb = 11
    #print(data[0])
    # will print 12 in this case; it's the pin-number of the attached sonar-device;
    # so here is the place to decide which device has called the callback (if you want to handle multiple devices with one callback)
    # like: if (data[1] == Pin_device_0): ...
    print(data[1])
    print(str(data[2]) + ' centimeters')

# Create a PyMata instance
board = PyMata("COM7", verbose=True)

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
    if board is not None:
        board.reset()
        board.close()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Configure pin 12 for SONAR module (see png)
# every 100 ms it will trigger the sonar to meassure the distance and call the callback, if the value has changed compared to previous meassure
# AND the distance is <=100
 board.sonar_config(12, 12, cb_ping, 100, 100)
# if you just do:
#board.sonar_config(12 , 12, cb_ping)
# it will use 50ms as the ping interval and 200 centimeters (which is also the maximum)

# after 30 seconds stop; sleep will sleep the main-thread; so Ctrl-C will not work (at least under Windows) in this time
# so in this example you may also take out the signal_handler if you like; it will work, but directly 
# between the following sleep and the board.close
time.sleep(30)
board.close()
@MrYsLab
Copy link
Owner

MrYsLab commented Jul 27, 2015

Thanks for your input. I modified the examples. Please let me know if you see any other issues.

@MrYsLab MrYsLab closed this as completed Jul 27, 2015
MrYsLab pushed a commit that referenced this issue Feb 18, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants