Hardware Reference
In-Depth Information
ters. Finally, the lines are written to the LCD with the serial
write
methods. The cursor
does not have to be reset between each line because after writing the first, it will be ready
in the second line.
Lastly, the bandwidth adjust knob is modeled in the
BandwidthKnob
class. This class
uses the
ADC
module and is also implemented as a thread. The analog input will always
produce a value when read, and we will ratio this value to the available bandwidth for the
bridge. The
Adafruit
library normalizes the values from
0.0
to
1.0
; so, when the
knob is at its midpoint, the call to
ADC.read(pin)
should return
0.5
. There is some
jitter in the analog signal, and we don't need a fine resolution on the bandwidth. Sampling
once every second should suffice since we don't expect the knob to change frequently.
We'll round up to the next whole number, which will give the knob ten discrete settings.
The
run
method will report back to the caller, via a message queue, if the
volume
setting
has changed. The calling thread can then update the Tor bridge bandwidth limits accord-
ingly.
This code sample shows you how to set up the ADC on BBB with the
Adafruit
library
and how to pass that result to a waiting thread with a message queue:
import Adafruit_BBIO.ADC as ADC
import threading
from time import sleep
from math import ceil, floor
import Queue
class BandwidthKnob(threading.Thread):
def __init__(self, pin, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.pin = pin
self.setup_adc()
self.kill = False
self.prev_value = -1
self.q = Queue.Queue()
def setup_adc(self):
'Load the Adafruit device tree fragment for ADC pins'
ADC.setup()