Skip to content

Commit

Permalink
Got rid of a hack for the two channel plot
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Nov 14, 2021
1 parent 863af26 commit 4bd3f0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Just upload the default firmata sketch into your Arduino and you are all set.
pyFirmata2 is an updated version of pyFirmata which *replaces loops
with callbacks*. Instead of unreliable "sleep" commands in a loop the
Python application registers callbacks which are then called
every time after new data has arrived.
every time after new data has arrived. This means for the analogue
channels the callbacks are called at the specified sampling rate
while the the digital ports call the callback functions after
a state change has happened (from 0 to 1 or 1 to 0).

This API has been used in the Digital Signal Processing (DSP) class to
practise realtime filtering of analogue sensor
Expand Down Expand Up @@ -138,13 +141,12 @@ number, and the mode:
- 'o' for output
- 'p' for pwm
- 's' for servo.
All seperated by `:`. Eg. `a:0:i` for analog 0 as input or `d:3:p` for
digital pin 3 as pwm:
All seperated by `:`, for example:
```
analog_0 = board.get_pin('a:0:i')
analog_0.read()
pin3 = board.get_pin('d:3:p')
pin3.write(0.6)
digital_0 = board.get_pin('d:0:i')
v = digital_0.read()
pin3 = board.get_pin('d:3:o')
pin3.write(True)
```

### Closing the board
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
- Realtime oscilloscope. This script shows how to process data at a given sampling rate.
- Printing data on the screen using an event handler
- Digital in reads from a digital pin using an event handler
- Flashing LED using a loop
- Flashing LED using a timer
18 changes: 10 additions & 8 deletions examples/realtime_two_channel_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ def addData(self,d):

# called for every new sample at channel 0 which has arrived from the Arduino
# "data" contains the new sample
def callBack(data):
def callBack1(data):
# filter your channel 0 samples here:
# data = self.filter_of_channel0.dofilter(data)
# send the sample to the plotwindow
qtPanningPlot1.addData(data)
ch1 = board.analog[1].read()
# 1st sample of 2nd channel might arrive later so need to check
if ch1:
# filter your channel 1 samples here:
# ch1 = self.filter_of_channel1.dofilter(ch1)
qtPanningPlot2.addData(ch1)

def callBack2(data):
# filter your channel 0 samples here:
# data = self.filter_of_channel0.dofilter(data)
# send the sample to the plotwindow
qtPanningPlot2.addData(data)


# Get the Ardunio board.
board = Arduino(PORT)
Expand All @@ -79,7 +80,8 @@ def callBack(data):
# Register the callback which adds the data to the animated plot
# The function "callback" (see above) is called when data has
# arrived on channel 0.
board.analog[0].register_callback(callBack)
board.analog[0].register_callback(callBack1)
board.analog[1].register_callback(callBack2)

# Enable the callback
board.analog[0].enable_reporting()
Expand Down

0 comments on commit 4bd3f0b

Please sign in to comment.