Battery Sample

From Digi Developer

Jump to: navigation, search

This page contains a sample application that demonstrates the use of the battery_voltage python API on Digi devices.

## From the digihw embedded library, import the battery_voltage object
from digihw import battery_voltage
 
## Import the Queue library to act as a thread safe object between callback and
## the main thread.
from Queue import Queue
 
## Create a empty Queue object
sample_queue = Queue()
 
## Get a handle to the battery_voltage object
battery = battery_voltage()
 
## Get a sample
sample =  battery.sample()
print "Current battery sample: ", sample
 
## Define a callback when the threshold is reached.  The second argument is
## expected to be a queue object.
 
def callback(sample, sample_queue):
  print "Callback function was called"
  print "Sample: ", sample
  print "Placing sample in queue: ", str(sample_queue)  
  sample_queue.put(sample)
 
## The threshold for the battery voltage is reached when the measured voltage
## drops below the set threshold.  For example, if the threshold is set to 
## 10 volts, the measured voltage is 9 volts, the threshold is reached.
 
## Here we set the threshold to whatever the sample is + 1 volt.
## This will likely cause the threshold event to trigger.
 
threshold_in_v = sample + 1.0 
print "Setting the voltage threshold to %.2f for the battery" %threshold_in_v
 
battery.register_threshold(threshold_in_v, callback, sample_queue)
 
print "Waiting for threshold to be triggered"
sample = sample_queue.get()
 
print "Sample that triggered the threshold: ", sample
Personal tools
Wiki Editing