Math Utils and Macros - AlbertGBarber/PixelSpork GitHub Wiki

Jump to:

Intro:

This page lists various helper math functions and general macros. Unlike other utility function sets, these functions are not part of a namespace, so you can just call them directly. Keep in mind that FastLED already provides a large number of math functions, with the functions on this page filling in any gaps.

You can also find these functions in "mathUtilsPS.h" and "mathUtilsPS.cpp".

Functions:

  • uint16_t mod16PS(uint16_t num1, uint16_t num2);
    

    A fast mod for unsigned 16 bit numbers. Mods num1 by num2, ie num1 % num2. The function is based on the FastLED "mod8" function.

  • uint16_t addMod16PS(uint16_t num1, uint16_t num2, uint16_t modNum);
    

    A fast "addition and mod" for unsigned 16 bit numbers, a very common occurrence in the Pixel Spork. Adds num1 to num2 and mods the result by modNum, ie (num1 + num2) % modNum. This is very useful for incrementing values while constraining them to a maximum value. The function is based on the FastLED "addmod8" function. Note that the sum of num1 and num2 shouldn't be greater than the uint16_t maximum, 65535. This is unlikely at the sorts of scales Pixel Spork is expected to operate on, so I don't usually check for it in my code, but it should be noted nonetheless.

  • uint8_t clamp8PS(int16_t in, uint8_t min, uint8_t max);
    

    Clamps an unsigned 8 bit input to be between the supplied min and max. In other words, if the input falls outside of the range of min to max, it will either be set to min or max according to which side of the range it falls on. If the input is within the range, it is returned unchanged. Note that the input is an int16_t to allow for negative numbers, but the clamp range is for unsigned 8 bit numbers, ie 0 - 255.

  • uint16_t clamp16PS(int32_t in, uint16_t min, uint16_t max);
    

    Same as clamp8 above, but for unsigned 16 bit inputs. Note that the input is an int32_t to allow for negative numbers, but the clamp range is for unsigned 16 bit numbers, ie 0 - 65535.

  • uint16_t qSubD_PS(uint16_t x, uint16_t y);
    

    Digital unsigned subtraction, subtracting y from x. If y > x, the result is y, otherwise the result is 0. For unsigned 8 or 16 bit numbers.

  • uint16_t qSubA_PS(uint16_t x, uint16_t y);
    

    Analog unsigned subtraction, subtracting y from x. If y > x, the result is x - y, otherwise the result is 0. For unsigned 8 or 16 bit numbers.

Macros:

Note that, although using a macro feels like using a function, macros are not functions, they are snippets of code that are executed at compile time, replacing the macro with its output. The output can be a fixed value, such as the size of an array, or an entire program expression where the macro fills in the inputs. You can read more about macros here.

  • SIZE(x);
    

    Returns the length of an array,x. Works for multidimensional arrays, but returns the length of the outer most dimension for the input. ie for an array x[2][3], SIZE(x) returns 2, while SIZE(x[0]) returns 3.

  • maxPS(a, b);
    

    Returns the largest of the two arguments, a and b.

  • minPS(a, b);
    

    Returns the smallest of the two arguments, a and b.