Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
timreyes committed Jun 12, 2020
1 parent 4441b28 commit 3d24b29
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
51 changes: 51 additions & 0 deletions average_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import math
import numpy

from saleae.range_measurements import DigitalMeasurer

AVERAGE_PERIOD = 'averagePeriod'

class AveragePeriodMeasurer(DigitalMeasurer):
supported_measurements = [AVERAGE_PERIOD]

# Initialize your measurement extension here
# Each measurement object will only be used once, so feel free to do all per-measurement initialization here
def __init__(self, requested_measurements):
super().__init__(requested_measurements)

# We always need rising/falling edges
self.edges_rising = 0
self.edges_falling = 0
self.first_transition_type = None
self.first_transition_time = None
self.last_transition_of_first_type_time = None

# This method will be called one or more times per measurement with batches of data
# data has the following interface
# * Iterate over to get transitions in the form of pairs of `Time`, Bitstate (`True` for high, `False` for low)
# `Time` currently only allows taking a difference with another `Time`, to produce a `float` number of seconds
def process_data(self, data):
for t, bitstate in data:
if self.first_transition_type is None:
self.first_transition_type = bitstate
self.first_transition_time = t
elif self.first_transition_type == bitstate:
current_period = t - (self.last_transition_of_first_type_time if self.last_transition_of_first_type_time is not None else self.first_transition_time)
self.last_transition_of_first_type_time = t
if bitstate:
self.edges_rising += 1
else:
self.edges_falling += 1

# This method is called after all the relevant data has been passed to `process_data`
# It returns a dictionary of the request_measurements values
def measure(self):
values = {}


if AVERAGE_PERIOD in self.requested_measurements:
if self.first_transition_time is not None and self.last_transition_of_first_type_time is not None:
period_count = (self.edges_rising if self.first_transition_type else self.edges_falling) - 1
values[AVERAGE_PERIOD] = 1 / (float(period_count) / (self.last_transition_of_first_type_time - self.first_transition_time))

return values
20 changes: 20 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "0.0.1",
"apiVersion": "1.0.0",
"author": "Tim Reyes",
"description": "",
"name": "Average Period",
"extensions": {
"AvgPeriod": {
"type": "DigitalMeasurement",
"entryPoint": "average_period.AveragePeriodMeasurer",
"metrics": {
"averagePeriod": {
"name": "Average Period",
"notation": "T<sub>avg</sub>",
"units": "s"
}
}
}
}
}

0 comments on commit 3d24b29

Please sign in to comment.