initGPIO(pinNr:byte, mode:byte)
Initializes the selected GPIO (analog pinMode) with one of the following modes:
OUTPUT
INPUT
INPUT_PULLUP
INPUT_PULLDOWN
Available pinNr constants:
C_PIN_01 (20*)
C_PIN_02 (25*)
C_PIN_03 (26*)
C_PIN_04 (32)
C_PIN_05 (33)
C_PIN_06 (34*)
C_PIN_07 (35*)
C_PIN_MISO (8)
C_PIN_MOSI (5)
C_PIN_SCK (7)
C_PIN_SDA (21*)
C_PIN_SCL (22*)
writeGPIO(pinNr:byte, state:bool)
Sets the state of a GPIO previously initialized as OUTPUT.
initGPIO(32, OUTPUT)
writeGPIO(32, 1) # HIGH
writeGPIO(32, 0) # LOW
readGPIO(pinNr:byte)->bool
Reads the state of a GPIO previously initialized as INPUT(_PULLUP/DOWN).
initGPIO(32, INPUT_PULLDOWN)
readGPIO(32) # false (0)
readGPIO(32) # true (1) if connected to 3.3V
readADC(pinNr:byte, nSamples:long)->int
Reads the analog value of either pinNr 32 or 33 multiple times (nSamples) and averages it.
The set reference voltage is ~2600mV (11dB attenuation).
The returned value ranges between 0 and 4095 (12bit).
readADC(32, 100)
readADC(33, 1000)
writeDAC(pinNr:byte, value:byte)
Only Oxocard Connect
Outputs an analog voltage signal between 0 (0) and 3.3V (255) on the selected GPIO (C_PIN_02 or C_PIN_03).
writeDAC(C_PIN_02, 127)
writePWM(pinNr:byte, dutyCycleValue:long)
Outputs a PWM signal with 500Hz and defined dutyCycle value from 0 (0%) to 4096 (100% at 12bit) on the selected GPIO.
writePWM(C_PIN_01, 2048) # 50%
setPWMFrequency(freq:long)
Sets the PWM frequency used: 10… 9765 Hz (13bit) or 19… 19531 Hz (12bit) or 38… 39062 Hz (11bit) etc. (Default: 500Hz)
setPWMFrequency(1000)
setPWMDutyCycleResolution(res:byte)
Sets the used PWM resolution: 0… 20bit (Default: 12bit)
setPWMDutyCycleResolution(12)
checkI2CAddress(slaveAddr:byte)->bool
Returns true if a device with defined slaveAddr was found and false otherwise.
writeI2CByte(slaveAddr:byte, byteAddr:int, data:byte)
Writes a byte of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
writeI2CByte(0x4C, 0x08, 0x13) # accel sample rate = 100Hz
writeI2CInt(slaveAddr:byte, byteAddr:int, data:int)
Writes an integer (2 bytes) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
writeI2CInt(0x2C, 0x05, 0x1337)
writeI2CLong(slaveAddr:byte, byteAddr:int, data:long)
Writes a long (4 bytes) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
writeI2CLong(0x2C, 0x05, 0x42133742)
writeI2C(slaveAddr:byte, byteAddr:int, size:int, data:byte[])
Writes an arbitrary number of bytes (size) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
data = [0x11, 0x22, 0x33]
writeI2C(0x2C, 0x05, 3, data)
readI2CByte(slaveAddr:byte, byteAddr:int)->byte
Reads a byte of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
If byteAddr is -1, the data is read directly after transfering the slaveAddr.
readI2CByte(0x4C, 0x18) # accel ID => 164
readI2CInt(slaveAddr:byte, byteAddr:int)->int
Reads an integer (2 bytes) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
If byteAddr is -1, the data is read directly after transfering the slaveAddr.
readI2CInt(0x4C, 0x0D) # accel raw X axis (16bit)
readI2CLong(slaveAddr:byte, byteAddr:int)->long
Reads a long (4 bytes) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
If byteAddr is -1, the data is read directly after transfering the slaveAddr.
ir = readI2CLong(0x52, 0x0A) # IR value (3 bytes + 1 unused)
ir = ir & 0x00FFFF # masking of the first 3 bytes
readI2C(slaveAddr:byte, byteAddr:int, size:int)
Reads an arbitrary number of bytes (size) of a defined I2C slave (7bit slaveAddr) and register (byteAddr).
If byteAddr is -1, the data is read directly after transfering the slaveAddr.
With the getI2C functions the read data can be returned afterwards.
readI2C(0x4C, 0x0D, 6) # accel raw X, Y & Z axis
xRaw = getI2CInt()
yRaw = getI2CInt()
zRaw = getI2CInt()
getI2CByte()->byte
Returns a byte previously read with readI2C().
The internal readBufferIndex is incremented by 1.
getI2CInt()->int
Returns an integer previously read with readI2C().
The internal readBufferIndex is incremented by 2.
getI2CLong()->long
Returns a long previously read with readI2C().
The internal readBufferIndex is incremented by 4.
setI2CReadBufferIndex(index:int)
Sets the internal readBufferIndex to the desired value.
This allows a previously read value to be read again.
readI2C(0x4C, 0x0D, 6) # accel raw X, Y & Z axis
xRaw = getI2CInt()
setI2CReadBufferIndex(0)
xRaw = getI2CInt()
yRaw = getI2CInt()
zRaw = getI2CInt()
setI2CByteAddrSize(size:byte)
Sets the data type size of the byteAddr used for I2C functions. This is mostly 1 (0… 255), but can also be 2 (0… 65535) for certain devices.
getI2CByteAddrSize()->byte
Returns the data type size of the byteAddr used at the moment.
setI2CBigEndian()
Use this function to set the int or long I2C functions to big endian.
An 16 bit value 0xAABB will equal to the array [0xAA, 0xBB] and vice versa
setI2CLittleEndian()
Use this function to set the int or long I2C functions to little endian.
An 16 bit value 0xAABB will equal to the array [0xBB, 0xAA] and vice versa
initDigitalLeds(pinNr:byte, ledsCount:byte, ledType:byte)
Initializes the selected pin so that a defined number of digital LEDs (e.g. NeoPixels) of the desired type can be set.
LED types:
C_LED_TYPE_WS2812
C_LED_TYPE_WS2812B
C_LED_TYPE_WS2812B_MINI
C_LED_TYPE_WS2813
C_LED_TYPE_WS2814
C_LED_TYPE_SK6812
C_LED_TYPE_SK6812W (for RGBW LED)
initDigitalLeds(C_PIN_01, 12, C_LED_TYPE_WS2812B)
setDigitalLedMode(mode:byte)
This parameter can be used to change the order of the colors. setDigitalLedMode(C_LED_MODE_RGB) sets the normal RGB mode, setDigitalLedMode(C_LED_MODE_GRB) is used if the RGB LEDs have swapped red and green.
setDigitalLed(ledNr:byte, r:byte, g:byte, b:byte)
Sets the color value (RGB) of the selected digital LED.
setDigitalLed(0, 50, 255, 10)
setDigitalLed2(ledNr:byte, r:byte, g:byte, b:byte, w:byte)
Sets the color value (RGBW) of the selected digital LED.
setDigitalLed2(0, 0, 0, 0, 255)
applyDigitalLeds()
Outputs the color values previously defined with setDigitalLed() on the pin selected with initDigitalLeds().
applyDigitalLeds()
deinitDigitalLeds()
Used to remove the digital LED interface if no longer used
initDigitalLeds(C_PIN_01, 25, C_LED_TYPE_SK6812) # Init LEDs
# Set LEDs
# ...
deinitDigitalLeds() # Remove digital LEDs
initUART(pinNrTx:byte, pinNrRx:byte, baudrate:long)
Initializes the UART interface.
Example:
# UART echo example
initUART(C_PIN_04, C_PIN_05, 115200) # TX, RX, BAUDRATE
#configUART(C_UART_DATA_BITS_8, C_UART_PARITY_DISABLE, C_UART_STOP_BITS_1)
def onDraw():
len = getUARTDataLength()
if len > 0:
data = readLineUART()
#data = readUART(len)
writeLineUART(data) # send the same data back
#writeUART(len, data)
clear()
drawText(10, 10, data)
update()
print(data)
delay(10)
if getButton():
if returnToMenu():
return
initUARTHalfDuplex(pinNrTx:byte, pinNrRx:byte, baudrate:long)
Initializes the UART interface in half duplex mode.
Example:
# UART echo example
initUARTHalfDuplex(C_PIN_04, C_PIN_05, 115200) # TX, RX, BAUDRATE
#configUART(C_UART_DATA_BITS_8, C_UART_PARITY_DISABLE, C_UART_STOP_BITS_1)
def onDraw():
len = getUARTDataLength()
if len > 0:
data = readLineUART()
#data = readUART(len)
writeLineUART(data) # send the same data back
#writeUART(len, data)
clear()
drawText(10, 10, data)
update()
print(data)
delay(10)
if getButton():
if returnToMenu():
return
configUART(byte dataBits, byte parity, byte stopBits)
Optional UART configurations.
Available dataBits:
C_UART_DATA_BITS_5
C_UART_DATA_BITS_6
C_UART_DATA_BITS_7
C_UART_DATA_BITS_8 // default
Available parity options:
C_UART_PARITY_DISABLE // default
C_UART_PARITY_EVEN
C_UART_PARITY_ODD
Available stopBits:
C_UART_STOP_BITS_1 // default
C_UART_STOP_BITS_1_5
C_UART_STOP_BITS_2
getUARTDataLength()->int
Returns the received data length.
readUART(size:int)->byte[256]
Reads a defined length in bytes and returns it as a byte array.
Note:
A maximum of 256 bytes can be returned per function call.
readLineUART()->byte[256]
Reads an entire line (up to ‘\n’) and returns it as a byte array.
Note:
A maximum of 256 bytes can be returned per function call.
writeUART(size:int, data:byte[])
Writes any number of bytes (size).
data = [0x11, 0x22, 0x33]
writeUART(3, data)
writeLineUART(data:byte[])
Write a string and append ‘\n’ at the end.
initSPI(mode:byte, speed:long)
Initializes the SPI interface.
Available modes:
C_SPI_MODE_0 // CPOL=0; CPHA=0
C_SPI_MODE_1 // CPOL=0; CPHA=1
C_SPI_MODE_2 // CPOL=1; CPHA=0
C_SPI_MODE_3 // CPOL=1; CPHA=1
Example:
initSPI(C_SPI_MODE_3, 1000000)
initGPIO(C_PIN_01, OUTPUT)
writeGPIO(C_PIN_01, 1) # resetCS
def onDraw():
len = 2
txData = [0x00, 0x00]
writeGPIO(C_PIN_01, 0) # setCS
writeSPI(len, txData)
readSPI(1)
writeGPIO(C_PIN_01, 1) # resetCS
rxData = getSPIByte()
print(rxData)
delay(1000)
if getButton():
if returnToMenu():
return
writeSPI(size:int, data:byte[])
Writes any number of bytes (size).
data = [0x11, 0x22, 0x33]
writeSPI(3, data)
readSPI(size:int)
Reads any number of bytes (size) and saves them in the internal SPI read buffer.
The read data can then be returned using the getSPI functions.
readSPI(7)
myByte = getSPIByte()
myInt = getSPIInt()
myLong = getSPILong()
transferSPI(size:int, txData:byte[])
Transfers and reads any number of bytes (size) and saves them in the internal SPI read buffer.
The read data can then be returned using the getSPI functions.
txData = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
transferSPI(7, txData)
myByte = getSPIByte()
myInt = getSPIInt()
myLong = getSPILong()
getSPIByte()->byte
Returns a byte previously read with readSPI().
The internal readBufferIndex is increased by 1.
getSPIInt()->int
Returns an integer previously read with readSPI().
The internal readBufferIndex is increased by 2.
getSPILong()->long
Returns a long previously read with readSPI().
The internal readBufferIndex is increased by 4.
setSPIReadBufferIndex(index:int)
Sets the internal readBufferIndex to the desired value.
This allows a previously read value to be read again.
initPulseCounter(pinNr:byte)
The pulse counter can be used to count pulses on the specified pin. The initPulseCounter function is used to initialize and start the counter for a pin.
initPulseCounter(C_PIN_06)
The value can be read out with the readPulseCounter function.
const ENCODER_PIN = C_PIN_06 # Pin to use with the counter
initPulseCounter(ENCODER_PIN)
while true:
value = readPulseCounter(ENCODER_PIN)
clear()
drawText(10, 10, "Value " + value)
update()
The counter can count up to 32767 and then starts again at 0.
If the counter needs to be reset manually, the resetPulseCounter function can be used.
Related functions:
readPulseCounter(pinNr:byte)
The readPulseCounter function can be used to read out the current value of the pluse counter.
value = readPulseCounter(C_PIN_06)
Related functions:
resetPulseCounter(pinNr:byte)
The pulse counter can be reset to 0 using the resetPulseCounter function.
resetPulseCounter(C_PIN_06)
Related functions:
initIrNecTx(pinNr:byte)
We support the standard NEC communication protocol for infrared transmissions. The functions can both send and receive data. Standard IR breakout boards can be used as hardware.
The initIrNecTx and sendIrNecData functions are provided for sending. The protocol allows an address and a data value (2 bytes each) to be transmitted:
initIrNecTx(C_PIN_01)
clear()
drawTextCentered(120, 100, “Press button”)
drawTextCentered(120, 140, “to send”)
update()
while true:
b = getButtons()
if(b.middle):
# send NEC addr data
sendIrNecData(0xFC00, 0x7F80)
sendIrNecData(addr:int, cmd:int)
Sends data with the NEC protocol. Read more on “initIrNecTx”
deinitIrNecTx()
Used to remove the NEC sender if no longer used
initIrNecTx(C_PIN_01) # Init NEC sender
# Send NEC data
# ...
deinitIrNecTx() # Remove if no longer used
initIrNecRx(pinNr:byte)
We support the standard NEC communication protocol for infrared transmissions. The functions can both send and receive data. Standard IR breakout boards can be used as hardware.
The initIrNecRx and readIrNecData functions are required for receiving. The function returns the received data as a byte array.
initIrNecRx(C_PIN_06)
while true:
delay(50)
res = readIrNecData() # returns [addr H bite, addr L byte, cmd H byte, cmd L byte]
clear()
drawTextCentered(120, 40, “Address:”)
drawTextCentered(120, 80, “0x” + byteToHex(res[0]) + byteToHex(res[1]))
drawTextCentered(120, 140, “Command:”)
drawTextCentered(120, 180, “0x” + byteToHex(res[2]) + byteToHex(res[3]))
update()
readIrNecData()->byte[4]
Receives data with the NEC protocol. Read more on “initIrNecRx”
deinitIrNecRx()
Used to remove the NEC receiver if no longer used
initIrNecRx(C_PIN_01) # Init NEC receiver
# Receive NEC data
# ...
deinitIrNecRx() # Remove if no longer used