Unit RPISENSEHAT

From Ultibo.org
Jump to: navigation, search

Return to Unit Reference


Description


Raspberry Pi Sense HAT Driver unit

The Sense HAT 8x8 LED matrix is presented in Ultibo as an 8x8 pixel framebuffer. This allows full access to the display at both the individual pixel level and also using the higher level console functions to display text and graphics on the matrix.

The framebuffer device supports rotation around the full 360 degrees by supplying the required rotation value in the framebuffer properties when calling FramebufferAllocate() or by calling the Sense HAT specific function RPiSenseFramebufferSetRotation().

As per the official Python libraries, the default rotation (FRAMEBUFFER_ROTATION_0) gives correct viewing when the HDMI port is facing downwards.

The Sense HAT joystick appears as a keyboard device and the pressed buttons are received as key presses that represent the Left, Right, Up, Down and Enter keys.

Constants



RPiSenseHat specific constants RPISENSE_*
RPISENSE_SIGNATURE = $EAEBECED;  


Framebuffer RPISENSE_FRAMEBUFFER_*
RPISENSE_FRAMEBUFFER_DESCRIPTION = 'Raspberry Pi Sense HAT Framebuffer'; Description of RPiSense framebuffer device
 
RPISENSE_PHYSICAL_WIDTH = 8;  
RPISENSE_PHYSICAL_HEIGHT = 8;  


Joystick RPISENSE_JOYSTICK_*
RPISENSE_JOYSTICK_DESCRIPTION = 'Raspberry Pi Sense HAT Joystick'; Description of RPiSense joystick device
 
RPISENSE_JOYSTICK_KEYMAP:array[0..4] of Word = (
SCAN_CODE_DOWN_ARROW,  
SCAN_CODE_RIGHT_ARROW,  
SCAN_CODE_UP_ARROW,  
SCAN_CODE_ENTER,  
SCAN_CODE_LEFT_ARROW  


RPiSenseHat register value RPISENSE_FB*
RPISENSE_FB = $00;  
RPISENSE_WAI = $F0;  
RPISENSE_VER = $F1;  
RPISENSE_KEYS = $F2;  
RPISENSE_EE_WP = $F3;  


RPiSenseHat gamma reset value RPISENSE_GAMMA_*
RPISENSE_GAMMA_VALUES_DEFAULT = 0;  
RPISENSE_GAMMA_VALUES_LOW = 1;  
RPISENSE_GAMMA_VALUES_USER = 2;  


Type definitions



RPi sense

PRPiSense = ^TRPiSense;

TRPiSense = record

Signature:LongWord; Signature for entry validation
I2C:PI2CDevice; I2C device
GPIO:PGPIODevice; GPIO device
Framebuffer:PFramebufferDevice; Framebuffer device
Joystick:PKeyboardDevice; Joystick (Keyboard) device

RPi sense gamma

PRPiSenseGamma = ^TRPiSenseGamma;

TRPiSenseGamma = array[0..31] of Byte;

   

RPi sense framebuffer

PRPiSenseFramebuffer = ^TRPiSenseFramebuffer;

TRPiSenseFramebuffer = record

Framebuffer Properties
Framebuffer:TFramebufferDevice;  
RPiSense Properties
I2C:PI2CDevice; The I2C device the device is connected to
Width:LongWord; Framebuffer Width in Pixels (Virtual)
Height:LongWord; Framebuffer Height in Pixels (Virtual)
Rotation:LongWord; Framebuffer Rotation (eg FRAMEBUFFER_ROTATION_180)
DirtyY1:LongWord; First line of dirty region (or Height - 1 if none dirty)
DirtyY2:LongWord; Last line of dirty region (or 0 if none dirty)
Ready:LongBool; If True timer should be enabled during Mark operation
Blank:LongBool; If True then display is currently blanked
Lock:TMutexHandle; Lock for dirty region redraw
Timer:TTimerHandle; Handle for dirty region redraw timer
FrameRate:LongWord; Frame rate for display refresh (in Frames Per Second)
Gamma:TRPiSenseGamma; Current gamma values
GammaUser:TRPiSenseGamma; User gamma values
PixelData:array[0..63] of Word; Pixel data for framebuffer update
BlockData:array[0..192] of Byte; Block data for display update

RPi sense joystick

PRPiSenseJoystick = ^TRPiSenseJoystick;

TRPiSenseJoystick = record

Keyboard Properties
Keyboard:TKeyboardDevice;  
RPiSense Properties
I2C:PI2CDevice; The I2C device the device is connected to
GPIO:PGPIODevice; The GPIO device the device is connected to
Pin:LongWord; The GPIO pin used to signal joystick events (GPIO_PIN_23)
Trigger:LongWord; The GPIO trigger to detect joystick events (GPIO_TRIGGER_RISING)
PreviousKeys:LongInt; The keys pressed on the last GPIO event callback
Statistics Properties
CallbackCount:LongWord; Number of callback requests received by the device


Public variables



RPiSenseHat specific variables

RPISENSE_I2C_DEVICE:String = 'I2C0';
RPISENSE_GPIO_DEVICE:String = 'GPIO0';
RPISENSE_FRAMEBUFFER_ROTATION:LongWord = FRAMEBUFFER_ROTATION_0;
RPISENSE_FRAMEBUFFER_WIDTH:LongWord = RPISENSE_PHYSICAL_WIDTH;
RPISENSE_FRAMEBUFFER_HEIGHT:LongWord = RPISENSE_PHYSICAL_HEIGHT;
RPISENSE_I2C_ADDRESS:Word = $46;
RPISENSE_LSM9DS1_MAGN_ADDRESS:Word = $1C;
RPISENSE_LSM9DS1_ACCEL_ADDRESS:Word = $6A;
RPISENSE_LPS25H_PRESS_ADDRESS:Word = $5C;
RPISENSE_HTS221_HUMID_ADDRESS:Word = $5F;


Function declarations



Initialization functions

procedure RPiSenseInit;
Description: Initialize the RPiSenseHat unit and parameters
Note Called only during system startup


function RPiSenseStart(const I2CDevice,GPIODevice:String; Rotation,Width,Height:LongWord):THandle;
Description: Start the RPiSenseHat driver and register the Framebuffer and Joystick devices
I2C The name of the I2C device that the Sense HAT is connected to
GPIO The name of the GPIO device that the Sense HAT is connected to
Rotation The rotation value of the framebuffer (eg FRAMEBUFFER_ROTATION_180)
Width The width of the framebuffer in pixels (Virtual width only, the Physical width is fixed at 8 pixels)
Height The height of the framebuffer in pixels (Virtual height only, the Physical height is fixed at 8 pixels)
Return The handle of the RPiSenseHat on success or INVALID_HANDLE_VALUE on failure
Note This function will be called during startup if the parameter RPISENSE_AUTOSTART is True


function RPiSenseStop(Handle:THandle):Boolean;
Description: Stop the RPiSenseHat driver and deregister the Framebuffer and Joystick devices
Handle The handle of the RPiSenseHat or INVALID_HANDLE_VALUE for the default
Return True if completed or False on failure


RPiSenseHat framebuffer functions

function RPiSenseFramebufferCreate(I2C:PI2CDevice; const Name:String; Rotation,Width,Height:LongWord):PFramebufferDevice;
Description: Create, register and allocate a new RPiSenseHat Framebuffer device which can be accessed using the framebuffer API
I2C The I2C device that the RPiSenseHat is connected to
Name The text description of this device which will show in the device list (Optional)
Rotation The rotation value of the framebuffer (eg FRAMEBUFFER_ROTATION_180)
Width The width of the framebuffer in pixels (Virtual width only, the Physical width is fixed at 8 pixels)
Height The height of the framebuffer in pixels (Virtual height only, the Physical height is fixed at 8 pixels)
Return Pointer to the new Framebuffer device or nil if the framebuffer device could not be created


function RPiSenseFramebufferDestroy(Framebuffer:PFramebufferDevice):LongWord;
Description: Release, deregister and destroy an RPiSenseHat Framebuffer device created by this driver}
Framebuffer The Framebuffer device to destroy
Return ERROR_SUCCESS if completed or another error code on failure


function RPiSenseFramebufferAllocate(Framebuffer:PFramebufferDevice; Properties:PFramebufferProperties):LongWord;
Description: Implementation of FramebufferDeviceAllocate API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDeviceAllocate instead.


function RPiSenseFramebufferRelease(Framebuffer:PFramebufferDevice):LongWord;
Description: Implementation of FramebufferDeviceRelease API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDeviceRelease instead.


function RPiSenseFramebufferBlank(Framebuffer:PFramebufferDevice; Blank:Boolean):LongWord;
Description: Implementation of FramebufferDevicBlank API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDevicBlank instead.


function RPiSenseFramebufferMark(Framebuffer:PFramebufferDevice; X,Y,Width,Height,Flags:LongWord):LongWord;
Description: Implementation of FramebufferDeviceMark API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDeviceMark instead.

Marks full lines only, X and Width are ignored for RPiSenseHat Framebuffer


function RPiSenseFramebufferCommit(Framebuffer:PFramebufferDevice; Address:PtrUInt; Size,Flags:LongWord):LongWord;
Description: Implementation of FramebufferDeviceCommit API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDeviceCommit instead.


function RPiSenseFramebufferSetOffset(Framebuffer:PFramebufferDevice; X,Y:LongWord; Pan:Boolean):LongWord;
Description: Implementation of FramebufferDeviceSetOffset API for RPiSenseHat Framebuffer
Note Not intended to be called directly by applications, use FramebufferDeviceSetOffset instead.


function RPiSenseFramebufferGetGamma(Framebuffer:PFramebufferDevice; var Gamma:TRPiSenseGamma):LongWord;
Description: Get the current gamma values from the RPiSenseHat framebuffer
Framebuffer The Framebuffer device to get from
Gamma An array of gamma values returned on completion
Return ERROR_SUCCESS if completed or another error code on failure


function RPiSenseFramebufferSetGamma(Framebuffer:PFramebufferDevice; const Gamma:TRPiSenseGamma):LongWord;
Description: Set the current gamma values for the RPiSenseHat framebuffer
Framebuffer The Framebuffer device to set for
Gamma An array of gamma values to set
Return ERROR_SUCCESS if completed or another error code on failure


function RPiSenseFramebufferResetGamma(Framebuffer:PFramebufferDevice; Value:LongWord):LongWord;
Description: Reset the current gamma values for the RPiSenseHat framebuffer to Default, Low or User
Framebuffer The Framebuffer device to set for
Value The gamma values to set (0 = Default / 1 = Low / 2 = User)
Return ERROR_SUCCESS if completed or another error code on failure


function RPiSenseFramebufferSetRotation(Framebuffer:PFramebufferDevice; Rotation:LongWord):LongWord;
Description: To be documented
Note None documented


procedure RPiSenseFramebufferUpdateDisplay(Framebuffer:PRPiSenseFramebuffer);
Description: Timer function for RPiSenseHat framebuffer dirty region redraw
Note Not intended to be called directly by applications


RPiSenseHat joystick functions

function RPiSenseJoystickCreate(I2C:PI2CDevice; GPIO:PGPIODevice; const Name:String):PKeyboardDevice;
Description: Create, register and attach a new RPiSenseHat Joystick device which can be accessed using the keyboard API
I2C The I2C device that the RPiSenseHat is connected to
GPIO The GPIO device that the RPiSenseHat is connected to
Name The text description of this device which will show in the device list (Optional)
Return Pointer to the new Joystick (Keyboard) device or nil if the device could not be created


function RPiSenseJoystickDestroy(Joystick:PKeyboardDevice):LongWord;
Description: Detach, deregister and destroy a RPiSenseHat Joystick device created by this driver
Joystick The Joystick (Keyboard) device to destroy
Return ERROR_SUCCESS if completed or another error code on failure


procedure RPiSenseJoystickCallback(Joystick:PRPiSenseJoystick; Pin,Trigger:LongWord);
Description: Callback function for the RPiSenseHat Joystick device, called on a worker thread when the registered GPIO event is triggered by a rising edge on the specified pin
Note Not intended to be called directly by applications


RPiSenseHat helper functions

function RPiSenseRegRead(I2C:PI2CDevice; Address:Word; Reg:Byte):LongInt;
Description: Read a byte from an RPiSenseHat register
I2C The I2C device the RPiSenseHat is connected to
Address The I2C address of the RPiSenseHat
Reg The register to read
Return The register value on success or -1 on failure


function RPiSenseBlockWrite(I2C:PI2CDevice; Address:Word; Data:PByte; Size:Integer):LongWord;
Description: Write a block of data to the RPiSenseHat
I2C The I2C device the RPiSenseHat is connected to
Address The I2C address of the RPiSenseHat
Data Pointer to the data block to be written
Size Size of the data block in bytes
Return ERROR_SUCCESS if completed or another error code on failure


Return to Unit Reference