The digital functionality currently supported by the PiSoC Python API
Class: | Provides functionality for use of the GPIO on the PiSoC as a digital input or output, with a number of different drive mode configurations. |
---|---|
Example: | Define a DigitalPin object in the following way: >>>My_input = DigitalPin(port,pin,'input')
>>>My_output = DigitalPin(port,pin,'output')
|
Note
Modification of which pins are available should be easy. Just add new pins to you PiSoC schematic, give them an input and an output, and then name them as GPIO_PORT_PIN. After you do this, confirm that it was done correctly by using the info qualifier when constructing the PiSoC class.
Method: | __init__ |
||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description: | Constructs and initializes a DigitalPin object |
||||||||||||||
Parameters: |
|
||||||||||||||
Returns: | None |
Note
GPIO pins unavailable to the firmware will raise a ValueError. Verify valid port and pin assignments using the info qualifier when PiSoC is constructed.
Method: | Configure | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Parameters: | config (str) – Drive mode of the pin
|
||||||||||||||
Returns: | None |
Method: | Read |
---|---|
Description: | Determines the state of the digital pin on the PiSoC |
Parameters: |
|
Returns: | boolean value (True/False) which indicates the state of the DigitalPin as HIGH/LOW, respectively |
Method: | Toggle |
---|---|
Description: | Toggles the state of the specified output |
Returns: | None |
Method: | Write |
---|---|
Description: | Writes a new value to the DigitalPin |
Parameters: | val (int) – Value to be written. val should be 1 or 0 for writing the output HIGH or LOW, respectively. |
Returns: | None |
Method: | get_gpio_bitmap |
---|---|
Description: | Calculates an n-bit integer between 0 and \(2^n - 1\) which represents the state of each available GPIO between 0 and n, where n is the number of GPIO available. The pins are sorted 0 - n according to their port and pin numbers. The lowest pin number on the lowest port number is equal to pin 0, and the highest pin number on the highest port number is equal to pin n. Use this when minimizing data transfers is desired. |
Returns: | an integer value between 0 and \(2^n - 1\) where n is the number of GPIO available. The binary representation of this number gives information on each pin state. The kth bit will give the state of the kth available pin. Bit 0 is the LSB and Bit n is the MSB. This result can be provided to the instance method Read() as a bitmap to abstract the calculation of True/False. |
Method: | get_port_state |
---|---|
Description: | Calculates an 8-bit value which represents the state of each pin on this pins associated port. Use this when minimizing data transfers is desired. |
Returns: | an integer value between 0 and 255. The binary representation of this number gives information on each pin state. The nth bit will give the state of the nth pin on this port. Bit 0 is the LSB and Bit 7 is the MSB. This result can be provided to the instance method Read() as a bitmap to abstract the calculation of True/False. |
Class: | This class provides functionality for use of the PWM components available on the PiSoC. |
---|---|
Example: | Define PWM objects in the following way: >>> My_PWM = PWM(0)
|
Method: | __init__ |
---|---|
Description: | Constructs and initializes PWM object |
Parameters: |
|
Returns: | None |
Method: | ClearFIFO |
---|---|
Description: | Clears the capture FIFO of any previously captured data. Here PWM_ReadCapture() is called until the FIFO is empty |
Returns: | None |
Method: | GetClockDivider |
---|---|
Description: | Gets the most recently confirmed clock divider value, which is used to determine the clocking frequency of the PWM |
Returns: | 16-bit integer value representing the current clock divider mapped to the source clock for this PWM. |
Method: | GetClocks |
---|---|
Description: | Calculates the actual clock rate of the PWM’s based on the most recently confirmed clock divider value |
Parameters: | precision (int) – Optional parameter. Specifies how many decimal places the result should be calculated to. |
Returns: | Floating point value which represents the source clock frequency for this PWM in Hertz. |
Method: | GetDutyCycle |
---|---|
Description: | Calculates the current duty cycle based on the current period and comparison values |
Parameters: | precision (int) – Optional parameter. The number of decimal places which the result should be calculated to. Defaults to 2. |
Returns: | A floating point value which is representative of the percentage of time, between 0 and 100%, that the PWM signal is on, relative to the total length of its period |
Method: | GetFrequency |
---|---|
Description: | Calculates the current PWM wave frequency based on the current clock rate and period value |
Parameters: | precision (int) – Optional parameter. The number of decimal places which the result should be calculated to. Defaults to 2. |
Returns: | A floating point value, which is representative of the current PWM signal frequency in Hz |
Method: | GetMIDI |
---|---|
Description: | Calculates what MIDI note corresponds to the current PWM frequency |
Returns: | The integer MIDI note, as defined by the MIDI standard to correspond with a specific musical note, which most closely resembles the PWM frequency |
Method: | ReadCompare |
---|---|
Description: | Determines the comparison value, in counts, currently set by the PWM component |
Returns: | Integer value representing the count value at which the PWM signal will change states during any given cycle, relative to the period. |
Example: | If the period is set to 100, and the comparison value is set to 20, the PWM signal will be HIGH for 20% of its period, and then LOW for the remaining 80% |
Method: | ReadPeriod |
---|---|
Description: | Determines the period value, in counts, currently set by the PWM component |
Returns: | Integer value representing the PWM period in counts |
Method: | SetClockDivider |
---|---|
Description: | Sets a clock divider to be mapped to the desired PWM’s clock source |
Parameters: | divider (int) – An integer between 0 and 65535 which the PWM clock will be divided by |
Returns: | None |
Warning
Changing the clock’s divider, and consequently its frequency, will affect any other PWMs which share that clock
Method: | SetClocks |
---|---|
Description: | Attempts to set the PWM Clock rate to a desired frequency using an appropriate clock divider. |
Parameters: | frequency (float) – A frequency in Hz which represents the desired clock rate
|
Returns: | None |
Warning
Method: | SetDutyCycle |
---|---|
Description: | Simplifies the process of setting a meaningful comparison value by calculating it based on a desired duty cycle then setting it to the PWM hardware. |
Parameters: | duty_cycle (float) – value between 0 and 100 which indicates the percentage of time that the PWM should be HIGH during one period
|
Returns: | None |
Method: | SetFrequency |
---|---|
Description: | Attempts to set the PWM wave frequency to a desired rate by calculating an appropriate period value and/or clock rate. It will try to maintain the clock rate, unless it is impossible without compromising the duty cycle too severely, in which case a warning will be issued. |
Parameters: |
|
Returns: | None |
Method: | SetMIDI |
---|---|
Description: | Generates a PWM wave frequency the corresponds to the specified midi note |
Parameters: |
|
Returns: | None |
Method: | Sleep |
---|---|
Description: | Stops the PWM operation, puts the PWM component into it’s lowest power state, and saves the current configuration |
Returns: | None |
Method: | Start |
---|---|
Description: | Starts component operation. Sets the initVar variable, calls the PWM_Init function, and then calls the PWM_Enable function |
Returns: | None |
Method: | Stop |
---|---|
Description: | Disables component operation |
Returns: | None |
Method: | Wakeup |
---|---|
Description: | Restores and enables the most recently saved configuration |
Returns: | None |
Method: | WriteCompare |
---|---|
Description: | Writes the comparison value used by the PWM hardware to determine the point in the PWM cycle at which the state will change, relative to the period. |
Parameters: | cmp (int) – The length, in counts, which defines the switching point relative to the PWM period * The value must not be greater than 65535 (for 16-bit mode, which is the default) * for 8-bit mode it must not be greater than 255 * The comparison value must be less than or equal to the period |
Returns: | None |
Method: | WritePeriod |
---|---|
Description: | Writes the period value used by the PWM hardware |
Parameters: | period (int) – The length, in counts, which defines how long a PWM Cycle will be
|
Returns: | None |
Method: | is_running |
---|---|
Description: | Checks if the PWM component is currently operational |
Returns: | boolean result which evaluates to True if the PWM component is operational, or False if it is not |
Class: | This class provides tools for easy manipulation of standard Servo Motors using PWM. |
---|---|
Example: | Define Servo objects in any the following ways: >>> My_simple_servo = Servo(0)
>>> My_calibrated_servo = Servo(1, min_pulse = 1.0, max_pulse = 2.0)
>>> My_descriptive_servo = Servo(7, min_pulse = 1.0, max_pulse = 2.0, min_angle = 0, max_angle = 180)
|
Method: | |
---|---|
Description: | Creates a servo object with the given parameter set |
Parameters: |
|
Returns: | None |
Note
A servo with channel n, will make PWM(n) unnavailable, since the servo controller is implemented using that PWM object. For fine control over that PWM, you can expose the internal use of the PWM instance using My_servo.servo_PWM, and then you can use any of the PWM methods using My_servo.servo_PWM.method(). This is advised against though, because servos are very particular about the construction of their data signals. If you change the wrong parameter of the PWM signal, you might damage the motor.
Method: | ChangeAngles |
---|---|
Description: | Changes the angle range that defines the minimum and maximum positions of the motor |
Parameters: |
|
Returns: | None |
Method: | ReadAngle |
---|---|
Description: | Calculates the current angle of the servomotor, linearized relative to the provided maximum and minimum angles |
Returns: | A value representative of the angle that the servo motor is held, according to the scale set within the class. |
Method: | ReadPulse |
---|---|
Description: | Calculates the current pulse width of the PWM which is driving the servomotor |
Returns: | A pulse duration in milliseconds which characterizes the state of the PWM signal being applied to the servomotor |
Method: | SetAngle |
---|---|
Description: | Calculates a pulse width based on the given angular position and the current min/max configuration parameters, then calls SetPulse() to set the position of the servo |
Parameters: | angle (float) – The angle to which the servo should be set, linearized relative to the defined minimum and maximum angles |
Returns: | None |
Method: | SetPulse |
---|---|
Description: | Sets a servo position based on a pulse width in ms |
Parameters: | pulse_ms (float) – A pulse width in milliseconds, which will be applied to the signal wire of the Servo at 50 Hz. Normal values are between 0.8 and 2.3, but they must be between the defined min_pulse and max_pulse values |
Returns: | None |
Method: | Start |
---|---|
Description: | Starts component operation. |
Returns: | None |
Method: | Stop |
---|---|
Description: | Stops the servo object by terminating the PWM channel that drives it |
Returns: | None |
Note
This may cause the servo to move slightly out of position
Method: | is_running |
---|---|
Description: | Checks to see if the Servo component is currently operational |
Returns: | A boolean variable which evaluates to True if the PWM component is operational, or False if it is not |
Class: | The Tone class provides an easy way to integrate musical tones into your programs by setting Notes or frequencies which can be played through a piezo buzzer. |
---|---|
Example: | Define a Tone object in any the following way: >>> Note = Tone(0)
|
Method: | __init__ |
Description: | Constructs and initializes the Tone object. |
Parameters: | channel (int) – Corresponds to the output pin of the Tone signal. Uses the same numbering convention as PWM |
Returns: | None |
Method: | GetFrequency |
---|---|
Description: | Calculates the current Tone frequency based on known parameters of it’s driving PWM |
Parameters: | precision (int) – Optional parameter. The number of decimal places which the result should be calculated to. Defaults to 2. |
Returns: | None |
Method: | GetMIDI |
---|---|
Description: | Calculates what MIDI note corresponds to the current Tone frequency |
Returns: | The Midi note, as defined by the Midi standard to correspond with a specific musical note |
Method: | GetVelocity |
---|---|
Description: | Calculates the volume of the Tone and adjusts it to be consistent with the MIDI velocity standard |
Returns: | The integer Tone velocity between 0 and 127, as defined by the Midi standard. |
Method: | GetVolume |
---|---|
Description: | Calculates the current volume |
Parameters: | max_volume (int) – Optional parameter. Scales the volume result according to a scale of 0 to max_volume. Default is 10. |
Returns: | Integer value reprsenting the current volume between 0 and max_volume |
Method: | SetFrequency |
---|---|
Description: | Attempts to set the Tone frequency to a desired rate by calculating an appropriate period value and/or clock rate for it’s driving PWM |
Parameters: |
|
Returns: | None |
Method: | SetMIDI |
---|---|
Description: | Generates a Tone frequency which corresponds to the specified MIDI note |
Parameters: |
|
Returns: | None |
Method: | SetNote |
---|---|
Description: | Sets the Tone frequency to that of a desired musical note |
Parameters: |
|
Returns: | None |
Method: | SetVelocity |
---|---|
Description: | Adjusts the volume of the note to correspond with the MIDI velocity standard |
Parameters: | velocity (int) – The desired velocity. Valid input is between 0 and 127, as defined by the MIDI standard |
Returns: | None |
Method: | SetVolume |
---|---|
Description: | Adjusts the volume of the note logarithmically according to the provided scale. |
Parameters: |
|
Returns: | None |
Method: | Sleep |
---|---|
Description: | Stops the Tone operation, puts the driving PWM component into it’s lowest power state, and saves the current configuration |
Returns: | None |
Method: | Start |
---|---|
Description: | Starts component operation. |
Returns: | None |
Method: | Stop |
---|---|
Description: | Stops the tone by terminating the PWM channel that drives it |
Returns: | None |
Method: | Wakeup |
---|---|
Description: | Restores and enables the most recently saved Tone configuration |
Returns: | None |
Method: | is_running |
---|---|
Description: | Checks to see if the Tone’s PWM component is currently operational |
Returns: | None |
Class: | This class provides functionality for use of an Arduino NeoPixels shield on an PiSoC through Python. |
---|---|
Example: | Create a NeoPixelShield object in the following way: >>> shield = NeoPixelShield()
|
Method: | __init__ |
Description: | Defines the register address for the striplight controller used by the NeoPixels, and it defines 140 colors as class attributes, named according to their standardized HTML and CSS names |
Returns: | None |
Method: | Clear |
---|---|
Description: | Clears the entire display. |
Returns: | None |
Method: | ClearColumn |
---|---|
Description: | Clears all pixels within a specified row |
Parameters: | column (int) – Column which contains the desired pixel to be set. Valid entries are 0-7. |
Returns: | None |
Method: | ClearPixel |
---|---|
Description: | Clears the given pixel at location (x,y) = (row, column) |
Parameters: | |
Returns: | None |
Method: | ClearRow |
---|---|
Description: | Clears all pixels within the specified row. |
Parameters: | row (int) – Row which contains the desired pixel to be set. Valid entries are 0-4. |
Returns: | None |
Method: | DrawColumn |
---|---|
Description: | Sets the pixels in a specified column to a desired color. |
Parameters: |
|
Returns: | None |
Method: | DrawRow |
---|---|
Description: | Sets the pixels in a specified row to a desired color. |
Parameters: |
|
Returns: | None |
Method: | Fill |
---|---|
Description: | Fills the entire display with a specified color. |
Parameters: | color (int) – A 24-bit number representative of a BRG value
|
Returns: | None |
Method: | RGB_to_hex |
---|---|
Description: | Converts RGB content to the needed BRG Hex value taken by the NeoPixelShield device |
Parameters: | RGB (list) – A list or tuple containing three elements, each of which is limited to one byte (0-255)
|
Returns: | A hex value which can be used as a valid color input for SetPixel(), DrawRow(), DrawColumn(), Fill(), and Stripe(). |
Example Usage:
>>> from pisoc import *
>>> shield = NeoPixelShield()
>>> shield.Start()
>>> shield.SetBrightness(3)
>>> purple = shield.RGB_to_hex([255, 0, 255]) #purple = Full Red, No Green, Full Blue
>>> shield.Fill(purple)
Method: | SetBrightness |
---|---|
Description: | Preserves the color of each pixel already drawn or to be drawn to the shield, but adjusts the brightness. |
Parameters: | brightness (int) – Integer value in the range of 0 - 5. A value of 0 will clear the display, and a value of 5 indicates maximum brigthness. |
Returns: | None |
Method: | SetPixel |
---|---|
Description: | Sets the given pixel at location (x,y) = (row, column), to a color defined by a 24 bit Blue-Red-Green (BRG) value (8-bits each) |
Parameters: |
|
Returns: | None |
Method: | Start |
---|---|
Description: | Powers up and enables the needed hardware for the NeoPixels component |
Returns: | None |
Method: | Stop |
---|---|
Description: | Powers down and disables the needed hardware for the NeoPixels component |
Returns: | None |
Method: | Stripe |
---|---|
Description: | Draws a line of specified length in a desired color, starting from the first pixel, and extending as far as the 40th (last) pixel. It will wrap around rows if required. |
Parameters: |
|
Returns: | None |
Method: | is_running |
---|---|
Description: | Checks to see if the NeoPixels display is currently powered up |
Returns: | A boolean variable which evaluates to True if the NeoPixels display is running, or False if it is not |
Class: | This class provides functionality for use of ultrasonic range finder devices that use standard GPIO pulse width sonic time-of-flight measurement protocols |
---|---|
Example: |
|
Method: | __init__ |
---|---|
Description: | Initializes an object by describing which pin the echo will be measured on. Optionally, provide a trigger pin if the ranger is a 4 or 5 pin form factor. Also optionally provide a trigger pulse width and timeout value in milliseconds, or a maximum frequency in Hz. |
Parameters: |
|
Returns: | None |
Method: | ReadCentimeters |
---|---|
Description: | Uses ReadMeters() and then converts the result to centimeters. |
Parameters: |
|
Returns: | The distance, in centimeters, between the ultrasonic ranger and the pinged object |
Method: | ReadInches |
---|---|
Description: | Uses ReadCentimeters() and then converts the result to inches |
Parameters: |
|
Returns: | The distance, in inches, between the ultrasonic ranger and the pinged object |
Method: | ReadMeters |
---|---|
Description: | Uses ReadRaw() to get a raw time value in microseconds, and then calculates the distance between the ranger and the pinged object in meters |
Parameters: |
|
Returns: | The distance, in meters, between the ultrasonic ranger and the pinged object |
Method: | ReadRaw |
---|---|
Description: | Gets a raw value from the PiSoC, which is representative of how many microseconds the rangers echo pin was held high |
Returns: | The length of time, in microseconds, that it took for the ping to echo back to the ultrasonic ranger. |
Method: | SetDelay |
---|---|
Description: | Sets the length of the trigger pulse, in microseconds, which will be used to tell the device to send out a ping |
Parameters: | delay_us (int) – Amount of time, in microseconds, that the PiSoC will hold its trigger pulse high. This is a 6 bit value, and so it must be between 0 and 63 |
Returns: | None |
Note
This is handled in __init__(), and so this value should be set in the class constructor. This method should only be called under unique circumstances.
Method: | SetTimeout |
---|---|
Description: | Sets the timeout length in microseconds. If the PiSoC is still waiting for a completed response after this amount of time, it will terminate its current process immediately return the result |
Parameters: | timeout_us (int) – Amount of time, in microseconds, that the PiSoC will wait for a completed response. Valid inputs are between 1 and 65535. |
Returns: | None |
Note
This is handled in __init__(), and so this value should be set in the class constructor. This method should only be called under unique circumstances.