analog.py

The analog functionality currently supported by the RPiSoC API

Analog Input Pins

class analog.analogPin(PIN)
Class:

Provides functionality for use of the general purpose analog input pins exposed on the RPiSoC

Example:

Define an analogPin object in the following way:

My_analog_pin = analogPin(PIN)
Method __init__
Description Constructs and initializes an analogPin object for general purpose analog input
Parameters int PIN
PIN
The analog pin number - This directly corresponds to the pins connected to your sequenced SAR ADC, where 0 is the top pin
  • The default version (V1.2) comes with 8 CapSense pins, and so 0-7 are valid arguments
  • For the default version, PIN 0-7 are relative to Port 3. So PIN = 0 an analog pin on Port 3 Pin 0 (P3[0]), and PIN = 7 is on Port 3 Pin 7 (P3[7])
Returns No return
Read()
Method Read
Description Reads the calculated digital value after analog to digital conversion
Parameters None
Returns int counts
counts
  • Digital value after an A/D conversion
  • The voltage which is represented by this value will depend on the resolution set by SetResolution()
  • Default resolution is 12, so \(V = 5.0*\frac{counts}{2^{12} - 1}\)
ReadVolts(PRECISION=2, COUNTS=None)
Method ReadVolts
Description Reads the specified analog pin and converts that digital reading to a voltage in Volts, or converts a given count value to Volts
Optional Parameters
  • int PRECISION
  • int COUNTS
PRECISION
The number of decimal points to be included in the returned result
  • Defaults to 2 if no parameter is given
COUNTS
The count value which should be converted to an analog voltage
  • Only provide this parameter if you only want to convert the given count value, but not get a new one. No data request will be sent to the RPiSoC if this argument is provided
  • If no parameter is provided, this method will get a digital value from the RPiSoC, and then convert it
Returns float volts
volts The converted digital value into a representative analog voltage.
SetOffset(counts)
Method SetOffset
Description Sets an offset on the ADC which is used for the voltage conversion. It will subtract the given offset before making the conversion
Parameters int counts
counts
Digital value which will be subtracted from any digital result before its conversion to a voltage takes place
  • For example, if a digital value of 255 is to be converted to a voltage, and the offset has been provided by this method at any point as 10, the result will be the conversion of 245 instead of 255
Returns No return
SetResolution(resolution)
Method SetResolution
Description
  • This function sets the resolution of ALL analogPin objects.
  • The resolution is defaulted to 12, so this method is only needed if this needs to be changed
Parameters int resolution
resolution
An integer value, in bits, that represents the new resolution of the SAR ADC
  • Valid arguments are 8, 10, or 12 only
Returns No return

Capacitive Sensing (CapSense)

class analog.CapSense(PIN, THRESHOLD=0)
Class:

Provides functionality for use of CapSense buttons on the RPiSoC

Example:

Define a CapSense object in the following way:

CapSense(0)
Method __init__
Description Initializes a Capsense object for use as a touch sensor
Parameters
  • list PIN
Optional Parameters
  • list THRESHOLD
PIN
The capsense pin number. PIN n directly corresponds to the pin which you assign to Capsense_BTN_N in your rpisoc firmware
  • The default version (V1.2) comes with 8 CapSense pins, and so 0-7 are valid arguments
  • The default version has CapSense pins 0-5 on Port 4, pins 0-5. PIN 6 is on P0[5] and PIN 7 is on P0[6]
THRESHOLD
The number of counts, between 0 and 255, which will be added to your calibrated baseline value to determine if a button has been touched
  • By default, this argument is 0, which means there is no THRESHOLD being used
  • 0 means that the baseline value itself will be used as a comparison value. Generally, baseline + THRESHOLD is used
  • The threshold may need to be high if the button data is noisy. Try printing the results of readRaw() to get an understanding of the output behavior as you touch the button, this will help determine a suitable value
Returns No return
Notes
  • THRESHOLD is only needed if using isTouched() to read sensors, which is the suggested method. Read() is only valid when SmartSense is used in your PSoC Creator firmware
    • The default version uses no tuning method, because the calibration is manually handled seperately
  • If SmartSense is used as a tuning method in your PSoC Creator software, for instance if you want to implement sliding capability, you will need to connect a 2.2 nF capacitor to P4[6]

Read()
Method Read
Description
  • Determines the state of the capsense button, as determined by PSoC SmartSensing. This will need to be calibrated in PSoC Creator for optimal results
  • Use isTouched() for more general application
Parameters None
Returns bool state
state A True or False value when the output is determined to be touched or not touched, respectively
Sleep()
Method Sleep
Description Prepares the component for the device entering a low-power mode. Disables Active mode power template bits of the sub components used within CapSense, saves nonretention registers, and resets all sensors to an inactive state
Parameters None
Returns No return
Side Effects This will affect all CapSense sensors, use it only if this is your intention
Start()
Method Start
Description
  • Initializes registers and enables active mode power template bits of the subcomponents used within CapSense
  • Establishes a CapSense baseline, which is used as a comparison value in isTouched()
  • The button should not be touched when this method is called, otherwise the baseline value will be calibrated with a touched value. It will only set the baseline if it sees two identical readings in a row
Parameters None
Returns No return
Stop()
Method Stop
Description Disables component interrupts, and calls CapSense_ClearSensors() to reset all sensors to an inactive state
Parameters None
Returns No return
Side Effects This will affect all CapSense sensors, use it only if this is your intention
Wakeup()
Method Wakeup
Description Restores CapSense configuration and nonretention register values after the device wake from a low power mode sleep mode
Parameters None
Returns No return
Side Effects This will affect all CapSense sensors, use it only if this is your intention
isRunning()
Method isRunning
Description Checks to see if the CapSense component and all subcomponents are currently operational
Parameters None
Returns bool __running
__running A private boolean variable which evaluates to True if CapSense is active, or False if it is not
isTouched()
Method isTouched
Description Uses the calibrated baseline and provided threshold value to determine if the requested CapSense button is being touched. This is the suggested method for touch detection.
Parameters None
Returns bool state
state A True or False value when the output is determined to be touched or not touched, respectively
readRaw()
Method readRaw
Description
  • Gives an 8-bit raw value from the capsense raw data array, which represents capacitance detection and is thus correlated with touch data
  • This is used to generate a baseline in Start()
  • Use this to calibrate touch buttons by deciding an approriate THRESHOLD as described in __init__()
Parameters None
Returns int val
state The raw value on the capsense button, which can be used to determine a touch event

ADCs (advanced)

Use these for fine control of your analog input. For general analog input needs, analogPin is almost always sufficient

class analog.ADC(c_type)
Class:

The ADC class provides functionality for using the Delta Sigma ADC and two Succesive Approximation ADC’s on the RPiSoC.

Example:

Define ADC objects in the following way:

My_DELSIG    = ADC('DELSIG')
My_SAR       = ADC('SAR0')
My_other_SAR = ADC('SAR1')
Method __init__
Description Constructs and initializes an ADC object for use of a SAR or DELSIG ADC
Parameters string c_type
c_type
The type of ADC to be used. Valid arguments are:
  • ‘DELSIG’- This is for the Delta sigma ADC, which has resolution up to 20 bits, but a slower conversion rate
  • ‘SAR0’ or ‘SAR1’ - These are for the two Successive Approximation ADCs, which has resolution of 8, 10, or 12 bits, but with a very fast conversion rate
Returns No return
Note None of the ADCs are included by default in V1.2, so this class will be unavailable. You are able to add them by simply placing them in your schematic on your RPiSoC program and reprogramming the board
CountsTo_Volts(counts)
Method CountsTo_Volts
Description Converts the ADC output to a Voltage, taking into account any set gains or offsets
Parameters int counts
counts Digital value to be converted to it’s analog equivalent
Returns float volts
volts The resulting voltage after conversion of counts
GetResult()
Method GetResult
Description Gets the result of a conversion which has been completed. StartConvert() must be called prior to this function, and StopConvert() must be called after
Parameters None
Returns int counts
counts Digital value which represents the result of the most recent AD conversion
IsEndConversion()
Method IsEndConversion
Description Checks to see if the ADC is done converting
Parameters None
Returns int counts
counts
  • Digital value which represents the result of the most recent AD conversion, if it is complete
  • 0 if conversions are not complete
Read()
Method Read
Description Simplifies the reading process by starting conversion, waiting for conversion to complete, stopping conversion, and returing the result, when called. It is only valid for the DELSIG
Parameters None
Returns int counts
counts Digital value which represents the result of the most recent AD conversion
SetBufferGain(gain)
Method SetBufferGain
Description Sets the input buffer gain
Parameters int gain
gain
The number by which the ADC output will be multiplied after conversion
  • Valid arguments are 1, 2, 4, or 8
Returns No return
Side Effects Increasing the gain will lower the buffer bandwidth
SetGain(gain)
Method SetGain
Description Sets the ADC gain in counts per volt, which will be applied before voltage conversion
Parameters int gain
gain Output gain in counts per volt to be applied before conversion
Returns No return
Warning gain is set by default by the reference and input range settings. It should only be used to further calibrate the ADC with a known input or if an external reference is used
SetOffset(offset)
Method SetOffset
Description Sets an offset on the ADC which is used for the voltage conversion. It will subtract the given offset before making the conversion
Parameters int counts
counts
Digital value which will be subtracted from any digital result before its conversion to a voltage takes place
  • For example, if a digital value of 255 is to be converted to a voltage, and the offset has been provided by this method at any point as 10, the result will be the conversion of 245 instead of 255
Returns No return
SetResolution(resolution)
Method SetResolution
Description
  • This function sets the resolution of the SAR ADCs only. This method is unavailable for the DELSIG ADC
  • The resolution is defaulted to 12, so this method is only needed if this needs to be changed
Parameters int resolution
resolution
An integer value, in bits, that represents the new resolution of the SAR ADC
  • Valid arguments are 8, 10, or 12 only
Returns No return
Side Effects
  • The ADC resolution cannot be changed during a conversion cycle. The recommended bestpractice is to stop conversions with Stop() before changint the resolution, and then restarting with Start()
  • If you call SetResolution() during a conversion, the resolution will not change until the current conversion is complete, and data will not be available in the new resolution for another 6+resolution clock cycles
Sleep()
Method Sleep
Description Checks to see if the component is enabled, then it stops the ADC and saves the current configuration for later use
Parameters None
Returns No return
Start()
Method Start
Description Sets the initVar variable on the RPiSoC, calls the ADC_Init() function, and then calls the ADC_Enable() function. This function configures and powers up the ADC, but does not start conversions
Parameters None
Returns No return
StartConvert()
Method StartConvert
Description Forces the ADC to initialize a conversion. This is handled internally for the Delsig ADC, using the Read() function, but it is kept seperate for the SAR. However, It can also be used for the Delsig
Parameters None
Returns No return
Stop()
Method Stop
Description Disables and powers down the ADC
Parameters None
Returns No return
StopConvert()
Method StopConvert
Description Forces the ADC to end conversion. This is handled internally for the Delsig ADC, using the Read() function, but it is kept seperate for the SAR. However, It can also be used for the Delsig
Parameters None
Returns No return
Wakeup()
Method Wakeup
Description Restores and enables the most recently saved configuration of the ADC
Parameters None
Returns No return
isRunning()
Method isRunning
Description Checks to see if the ADC component and all subcomponents are currently operational
Parameters None
Returns bool __running
__running A private boolean variable which evaluates to True if the ADC is active, or False if it is not `

Analog Output

Use these to generate analog voltages, currents, or waveforms.

Voltage DAC (VDAC)

class analog.VDAC(channel)
Class:

The VDAC class provides functionality for using the VDAC’s available on the RPiSoC.

Example:

Define VDAC objects in the following way:

My_VDAC       = VDAC(0)
My_other_VDAC = VDAC(1)
Method __init__
Description Constructs and initializes a VDAC object
Parameters
  • int channel
channel
Determines which IDAC is to be utilized
  • 0 for the first VDAC; output is on P0[1] by default
  • 1 for the second VDAC; not available by default
Returns No return
SetRange(Range)
Method SetRange
Description Sets the full scale range for the VDAC
Parameters string Range
Range
Choices are HIGH or LOW
  • HIGH: Sets full scale range to 4.080V
  • LOW: Sets full scale range to 1.020V
Returns No return
SetSpeed(speed)
Method SetSpeed
Description Sets the DAC speed, and consequently the power level
Parameters string speed
speed
HIGH or LOW for high speed or low speed, respectively
  • HIGH speed puts the DAC in high power mode
  • LOW speed puts the DAC in low power mode
Returns No return
SetValue(value)
Method SetValue
Description Sets a digital value to be converted into an analog voltage and output by the VDAC
Parameters int value
value
Digital number between 0 and 255
  • A value of 0 will correspond to an output of the lowest possible voltage output
  • A value of 255 will output the full scale value, as selected with SetRange()
  • All values between 0 and 255 are linearized according to the full scale range
Returns No return
SetVoltage(volts)
Method SetVoltage
Description Sets a voltage in Volts to be output on the specified VDAC
Parameters float volts
amps A number between 0 and the full scale range, as selected by SetRange(). This will be converted to an analog voltage and output.
Returns No return
Sleep()
Method Sleep
Description Checks to see if the component is enabled, then it stops the VDAC and saves the current configuration for later use
Parameters None
Returns No return
Start()
Method Start
Description Enables and powers up the VDAC
Parameters None
Returns No return
Stop()
Method Stop
Description Powers down VDAC to lowest power state and disables output
Parameters None
Returns No return
Wakeup()
Method Wakeup
Description Restores and enables the most recently saved configuration of the VDAC
Parameters None
Returns No return
isRunning()
Method isRunning
Description Checks to see if the DAC component and all subcomponents are currently operational
Parameters None
Returns bool __running
__running A private boolean variable which evaluates to True if the DAC is active, or False if it is not `

Current DAC (IDAC)

class analog.IDAC(channel)
Class:

The IDAC class provides functionality for using the IDAC’s available on the RPiSoC.

Example:

Define IDAC objects in the following way:

My_IDAC       = IDAC(0)
My_other_IDAC = IDAC(1)
Method __init__
Description Constructs and initializes an IDAC object
Parameters
  • int channel
channel
Determines which IDAC is to be utilized
  • 0 for the first IDAC; output is on P0[7] by default
  • 1 for the second IDAC; not available by default
Returns No return
SetCurrent(amps)
Method SetCurrent
Description Sets a current in milliamps to be output on the specified IDAC
Parameters float amps
amps A number between 0 and the full scale range, as selected by SetRange(). This will be converted to an analog current and output.
Returns No return
SetPolarity(polarity)
Method SetPolarity
Description Sets the DAC output polarity
Parameters string polarity
polarity SOURCE or SINK
Returns No return
SetRange(mode)
Method SetRange
Description Sets the full scale range for the IDAC
Parameters int mode
mode
Choices are 0, 1, or 2
  • 0: Sets full scale range to 31.875 uA
  • 1: Sets full scale range to 255 uA
  • 2: Sets full scale range to 2.04 mA
Returns No return
SetSpeed(speed)
Method SetSpeed
Description Sets the DAC speed, and consequently the power level
Parameters string speed
speed
HIGH or LOW for high speed or low speed, respectively
  • HIGH speed puts the DAC in high power mode
  • LOW speed puts the DAC in low power mode
Returns No return
SetValue(value)
Method SetValue
Description Sets a digital value to be converted into an analog current and output by the IDAC
Parameters int value
value
Digital number between 0 and 255
  • A value of 0 will correspond to an output of the lowest possible current output
  • A value of 255 will output the full scale value, as selected with SetRange()
  • All values between 0 and 255 are linearized according to the full scale range
Returns No return
Sleep()
Method Sleep
Description Checks to see if the component is enabled, then it stops the DAC and saves the current configuration for later use
Parameters None
Returns No return
Start()
Method Start
Description Sets the initVar variable on the RPiSoC, calls the IDAC8_Init() function and then calls the IDAC8_Enable() function. Enables and powers up the IDAC
Parameters None
Returns No return
Stop()
Method Stop
Description Powers down IDAC to lowest power state and disables output
Parameters None
Returns No return
Wakeup()
Method Wakeup
Description Restores and enables the most recently saved configuration of the DAC
Parameters None
Returns No return
isRunning()
Method isRunning
Description Checks to see if the DAC component and all subcomponents are currently operational
Parameters None
Returns bool __running
__running A private boolean variable which evaluates to True if the DAC is active, or False if it is not `

Wave DAC

class analog.WaveDAC
Class:

The WaveDAC class provides functionality for using the WaveDAC available on the RPiSoC.

Example:

Define WaveDAC objects in the following way:

My_Wave = WaveDAC()
Method __init__
Description Constructs and initializes an WaveDAC object
Parameters None
Returns No return
GenerateWave(waveType)
Method GenerateWave
Description
  • Provides functionality for generating a specific waveform
  • It uses the class attributes amplitude and dcB, which define the waveforms peak voltage and the DC Bias, respectively
  • amplitude and dcB are defaulted to 4V amplitude and 0V DC Bias, unless modifed by using setAmplitude() and setdcBias()
Parameters string waveType
waveType
The output behavior of the WaveDAC, Valid choices are:
  • SINE, SQUARE, TRIANGLE, or SAWTOOTH
Returns No return
Note V1.3 intends to introduce inputting custom waveforms
GetFrequency()
Method GetFrequency
Description Calculates the actual wave frequency based on the most recently confirmed clock divider value
Parameters None
Returns float freq
freq The actual frequency of the wave, not a requested frequency
SetFrequency(frequency)
Method SetFrequency
Description Sets an approximate frequency by using an appropriate clock divider so that the resulting wave is as close to the desired frequency as possible
Parameters float frequency
frequency
A frequency in Hz that will be representative of the output wave rate
  • Valid range is between 0.46 and 2500
Returns No return
SetSpeed(speed)
Method SetSpeed
Description Sets the DAC speed to one of the defined settings
Parameters string speed
speed
HIGH or LOW for high speed or low speed, respectively
  • HIGH: Highest power and fastest slew rate
  • LOW: Lowest active power and slowest slew rate
Returns No return
SetValue(val)
Method SetValue
Description Sets a digital value to be converted into a voltage and output by the WaveDAC.
Parameters int val
val Digital number between 0 and 255
Returns No return
Warning The WaveDAC should be stopped before using this function
Sleep()
Method Sleep
Description Checks to see if the component is enabled, then it stops the DAC and saves the current configuration for later use
Parameters None
Returns No return
Start()
Method Start
Description Performs all of the required initialization for the component and enables power to the block
Parameters None
Returns No return
StartClock()
Method StartClock
Description Restarts the WaveDAC clock after it has been stopped
Parameters None
Returns No return
Stop()
Method Stop
Description Powers down the WaveDAC to its lowest power state and disables output
Parameters None
Returns No return
StopClock()
Method StopClock
Description Stops the WaveDAC clock so that a value can be set without interference by the clock
Parameters None
Returns No return
Wakeup()
Method Wakeup
Description Restores and enables the most recently saved configuration of the DAC
Parameters None
Returns No return
isRunning()
Method isRunning
Description Checks to see if the DAC component and all subcomponents are currently operational
Parameters None
Returns bool __running
__running A private boolean variable which evaluates to True if the DAC is active, or False if it is not `
setAmplitude(amplitude)
Method setAmplitude
Description Modifies the class attribute associated with peak voltage of a generated wave
Parameters float amplitude
amplitude A peak amplitude, in Volts, between 0 and 4.08
Returns No return
Warning This method will become deprecated in V1.3. There will be an alternate way of accomplishing this
setdcBias(dcBias)
Method setdcBias
Description Modifies the class attribute associated with DC Bias of a generated wave
Parameters float dcBias
dcBias A DC bias, in Volts, between 0 and 4.08
Returns No return
Warning This method will become deprecated in V1.3. There will be an alternate way of accomplishing this

Table Of Contents

Previous topic

digital.py

Next topic

Example Projects!

This Page