libtinylor
Single-file header library for controlling Light-O-Rama hardware
Loading...
Searching...
No Matches
tinylor.c File Reference

Protocol encoding functions implementation. More...

#include "tinylor.h"
Include dependency graph for tinylor.c:

Functions

void lor_set_channel (lor_req_s *req, lor_channel c)
 Configures the request to use a single, absolute channel.
 
void lor_set_channels (lor_req_s *req, lor_channel first, const unsigned short cbits)
 Configures the request to use a channel set, in the form of a 16-bit bitset of channels, "starting at" the first channel offset used.
 
void lor_set_unit (lor_req_s *req, const lor_unit u)
 Configures the request to be sent to the specified unit. A unit is required for all requests. A magic value of 0xFF may be used to broadcast the request to all units.
 
int lor_set_effect (lor_req_s *req, const lor_effect e, const lor_effect_args_u *args)
 Configures the request to apply the specified effect. The effect may require additional arguments, which are provided in the args field as a union. If provided, the args field is copied into the request at which point the caller may safely discard the original args. Any non-NULL arguments will be copied into the request, even if the effect does not require them. This allows future expansion of usable effects without modifying the behavior of the function.
 
void lor_set_intensity (lor_req_s *req, const lor_intensity i)
 Configures the request to set the intensity of the lights to the provided value.
 
void lor_set_fade (lor_req_s *req, const lor_intensity start, const lor_intensity end, const lor_decisec ds)
 Configures the request to fade from one intensity to another over a specified duration in deciseconds.
 
static lor_channel_format lor_get_cset_format (const lor_channel_set *const cset)
 Determines the compressed format of the channel set for encoding.
 
static int lor_encode_cset (unsigned char *const b, const lor_channel_set *const cset)
 Encodes a channel set into a buffer.
 
static int lor_encode_decis (unsigned char *b, const lor_decisec ds)
 Encodes a decisecond value into a 2-byte buffer.
 
static int lor_encode_effect (unsigned char *const b, const lor_effect e, const lor_effect_args_u *const d)
 Encodes an effect into a buffer, including any required arguments for the effect type.
 
size_t lor_write (unsigned char *b, const size_t bs, const lor_req_s *r, const size_t rs)
 Encodes and writes up to rs requests to the provided buffer b as binary data. Requests are first encoded into a scratch buffer to ensure the buffer has enough space to hold the encoded data. The function will attempt to write as many requests as possible to the buffer, up to the provided request count.
 
lor_intensity lor_get_intensity (const unsigned char b)
 Encodes a [0,0xFF] value into a roughly equivalent LOR intensity value (precision is lossy) that is compatible with the protocol.
 

Detailed Description

Protocol encoding functions implementation.

Function Documentation

◆ lor_encode_cset()

static int lor_encode_cset ( unsigned char *const b,
const lor_channel_set *const cset )
static

Encodes a channel set into a buffer.

Parameters
bThe buffer to write the channel set to.
csetThe channel set to encode.
Returns
The number of bytes written to the buffer.
Note
Caller is responsible for ensuring buffer is at least 3 bytes in size.
Here is the caller graph for this function:

◆ lor_encode_decis()

static int lor_encode_decis ( unsigned char * b,
const lor_decisec ds )
static

Encodes a decisecond value into a 2-byte buffer.

Parameters
bThe buffer to write the decisecond value to.
dsThe decisecond value to encode.
Returns
The number of bytes written to the buffer.
Note
Caller is responsible for ensuring buffer is at least 2 bytes in size.
Here is the caller graph for this function:

◆ lor_encode_effect()

static int lor_encode_effect ( unsigned char *const b,
const lor_effect e,
const lor_effect_args_u *const d )
static

Encodes an effect into a buffer, including any required arguments for the effect type.

Parameters
bThe buffer to write the effect to.
eThe effect to encode.
dOptional effect arguments to encode.
Returns
The number of bytes written to the buffer.
Note
Caller is responsible for ensuring buffer is large enough to hold the encoded effect and arguments.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lor_get_cset_format()

static lor_channel_format lor_get_cset_format ( const lor_channel_set *const cset)
static

Determines the compressed format of the channel set for encoding.

Parameters
csetThe channel set to determine the format of.
Returns
The format byte header of the channel set for protocol encoding.
Here is the caller graph for this function:

◆ lor_get_intensity()

lor_intensity lor_get_intensity ( unsigned char b)

Encodes a [0,0xFF] value into a roughly equivalent LOR intensity value (precision is lossy) that is compatible with the protocol.

Note
This is a default implementation of the lor_intensity_fn type. It operates via a known truth table from protocol documentation. Other or custom implementations may be ideal for your specific use case.
Parameters
bThe byte value to convert.
Returns
The scaled intensity value.

◆ lor_set_channel()

void lor_set_channel ( lor_req_s * req,
lor_channel c )

Configures the request to use a single, absolute channel.

Parameters
reqThe request to configure.
cThe single, absolute channel to apply the effect to, less than 1024.

◆ lor_set_channels()

void lor_set_channels ( lor_req_s * req,
lor_channel first,
unsigned short cbits )

Configures the request to use a channel set, in the form of a 16-bit bitset of channels, "starting at" the first channel offset used.

Note
Any bits within the set that (once re-aligned to a 16-bit boundary) exceed the 16-bit window, will be disregarded. Pre-aligned 16-bit bitsets are recommended for best results. You may set the request's cset offset and cbits fields directly.
Parameters
reqThe request to configure.
firstThe first channel in the set.
cbitsThe 16-bit bitset of channels to apply the effect to.
Here is the caller graph for this function:

◆ lor_set_effect()

int lor_set_effect ( lor_req_s * req,
lor_effect e,
const lor_effect_args_u * args )

Configures the request to apply the specified effect. The effect may require additional arguments, which are provided in the args field as a union. If provided, the args field is copied into the request at which point the caller may safely discard the original args. Any non-NULL arguments will be copied into the request, even if the effect does not require them. This allows future expansion of usable effects without modifying the behavior of the function.

Parameters
reqThe request to configure.
eThe effect to apply.
argsThe effect arguments, required if effect is LOR_SET_INTENSITY, LOR_FADE, LOR_PULSE, or LOR_SET_DMX_INTENSITY. Should likely be NULL for other effect types.
Returns
0 on success, -1 for invalid arguments.
Here is the caller graph for this function:

◆ lor_set_fade()

void lor_set_fade ( lor_req_s * req,
lor_intensity start,
lor_intensity end,
lor_decisec ds )

Configures the request to fade from one intensity to another over a specified duration in deciseconds.

Note
Equivalent to calling lor_set_effect with LOR_FADE and effect arguments set to the provided start and end intensities and duration.
Parameters
reqThe request to configure.
startThe starting intensity.
endThe ending intensity.
dsThe duration of the fade in deciseconds.
Here is the call graph for this function:

◆ lor_set_intensity()

void lor_set_intensity ( lor_req_s * req,
lor_intensity i )

Configures the request to set the intensity of the lights to the provided value.

Note
Equivalent to calling lor_set_effect with LOR_SET_INTENSITY and effect arguments set to the provided intensity.
Parameters
reqThe request to configure.
iThe intensity to use. This value should likely pass through a intensity conversion function before being passed to this function.
Here is the call graph for this function:

◆ lor_set_unit()

void lor_set_unit ( lor_req_s * req,
lor_unit u )

Configures the request to be sent to the specified unit. A unit is required for all requests. A magic value of 0xFF may be used to broadcast the request to all units.

Parameters
reqThe request to configure.
uThe unit to send the request to, or 0xFF to broadcast to all units.
Here is the caller graph for this function:

◆ lor_write()

size_t lor_write ( unsigned char * b,
size_t bs,
const lor_req_s * r,
size_t rs )

Encodes and writes up to rs requests to the provided buffer b as binary data. Requests are first encoded into a scratch buffer to ensure the buffer has enough space to hold the encoded data. The function will attempt to write as many requests as possible to the buffer, up to the provided request count.

Returns
If all requests were written, the number of bytes written to the buffer is returned. If the buffer is too small to hold all requests, the number of bytes required to hold at least one additional request is returned.
Here is the call graph for this function:
Here is the caller graph for this function: