Math and Logic

Description

Basic mathematical and logical operations

This resource provides basic operations not supported by the programming language itself. This includes:

  • Increment a variable, saturating at a specified value
  • Increment a variable, wrapping to 0 at a specified value
  • Swap endianess in a 16-bit variable (8 lsbs and 8 msbs are swapped)

Examples

Endianess Conversion and Ring Buffer Position Update

// Read the sensor value, LSB first and then MSB in a 16-bit SPI RX operation
U16 temp;
spiCfg(SPI_POL0_PHA0, 6);
spiBegin(AUXIO_SPI_CSN_SENSOR);
spiTx8bit(0x42);
spiRx16bit(temp);
spiEnd(AUXIO_SPI_CSN_SENSOR);

// Swap endianess, store to the ring buffer and increment the ring buffer position
U16 n = state.bufferPos;
utilSwapEndianess(temp; output.pBuffer[n]);
utilIncrAndWrap(n, BUFFER_SIZE; state.bufferPos);

Histogram Update

U16 value = ...;

// Find the correct histogram bin counter
U16* pBinCounter;
if (value > 1000) {
    pBinCounter = #output.highCount;
} else {
    pBinCounter = #output.lowCount;
}

// Increment the histogram bin counter, saturating at 50000
utilIncrAndSat(*pBinCounter, 50000; *pBinCounter);

Procedures Overview

Name Brief description
utilIncrAndSat() Increments a variable, saturating at the specified value More …
utilIncrAndWrap() Increments a variable, wrapping to 0 at the specified value. More …
utilSwapEndianess() Swaps the most and least significant bytes in a variable. More …

Constants

None.

Global Variables

None.

Procedures

utilIncrAndSat

Prototype: utilIncrAndSat(value, #sat; incrValue)

Increments a variable, saturating at the specified value

Parameter value(s)

  • value : Initial value
  • #sat : Saturation value

Return value(s)

  • incrValue : Incremented value

utilIncrAndWrap

Prototype: utilIncrAndWrap(value, #wrap; incrValue)

Increments a variable, wrapping to 0 at the specified value.

Parameter value(s)

  • value : Initial value
  • #wrap : Wrap/modulo value

Return value(s)

  • incrValue : Incremented value

utilSwapEndianess

Prototype: utilSwapEndianess(initialValue; swappedValue)

Swaps the most and least significant bytes in a variable.

Parameter value(s)

  • initialValue : Initial value

Return value(s)

  • swappedValue : Swapped value