rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
knob.c File Reference

The poll transactions and the angle steps of the rotary knob, see knob.h. More...

#include "knob.h"
#include <stddef.h>

Functions

uint16_t knob_decode (uint8_t hi, uint8_t lo)
 The datasheet's angle, 0..16383, rising counter-clockwise seen from the chip's top: hi read from KNOB_REG_ANGLE_HI, lo from KNOB_REG_ANGLE_LO.
uint16_t knob_clockwise (uint16_t angle)
 angle mirrored, rising clockwise seen from the top: 0 stays 0.
bool knob_attach (knob_t *k, unsigned addr)
 Take addr as the sensor's address and forget the reference.
int knob_poll_addr (const knob_t *k)
 The address the next poll reads, or -1 for a knob that is not attached: then no transaction is made.
bool knob_poll_probe_first (const knob_t *k)
 Whether the next poll probes the address before its reads: an attached knob with no reference.
void knob_reset (knob_t *k)
 Forget the reference: the next usable reading moves nothing.
int knob_feed (knob_t *k, bool usable, uint16_t raw)
 Feed one poll.
int knob_poll_done (knob_t *k, bool answered, const uint8_t bytes[KNOB_POLL_READS])
 End one poll.
float knob_span_fraction (int steps, int scale_deg)
 Fraction of a slider's span that steps moves it when the span is scale_deg degrees of knob.

Variables

const uint8_t knob_poll_regs [KNOB_POLL_READS]

Detailed Description

The poll transactions and the angle steps of the rotary knob, see knob.h.

SPDX-License-Identifier: MIT

Function Documentation

◆ knob_attach()

bool knob_attach ( knob_t * k,
unsigned addr )

Take addr as the sensor's address and forget the reference.

An address the panel board answers at (panel_i2c.h) is refused: the knob stays detached, knob_poll_addr() names no address and nothing is ever sent for it. Returns whether the address was taken.

Parameters
kThe knob to attach.
addrThe 7-bit sensor address.
Returns
true when the address was taken.

◆ knob_clockwise()

uint16_t knob_clockwise ( uint16_t angle)

angle mirrored, rising clockwise seen from the top: 0 stays 0.

Parameters
angleThe angle, 0 to 16383.
Returns
The mirrored angle, 0 to 16383.

◆ knob_decode()

uint16_t knob_decode ( uint8_t hi,
uint8_t lo )

The datasheet's angle, 0..16383, rising counter-clockwise seen from the chip's top: hi read from KNOB_REG_ANGLE_HI, lo from KNOB_REG_ANGLE_LO.

Bits 1 and 0 of lo are ignored.

Parameters
hiThe byte read from KNOB_REG_ANGLE_HI.
loThe byte read from KNOB_REG_ANGLE_LO.
Returns
The 14-bit angle, 0 to 16383.

◆ knob_feed()

int knob_feed ( knob_t * k,
bool usable,
uint16_t raw )

Feed one poll.

usable false (no answer) forgets the reference. Returns the signed steps since the last usable reading, positive for a rising raw, taking the short way across 16383 to 0; zero for the first reading after a reset, and for a step over KNOB_MAX_STEP, which becomes the new reference.

Parameters
kThe knob.
usablefalse for no answer, which forgets the reference.
rawThe angle, 0 to 16383.
Returns
The signed steps since the last usable reading.

◆ knob_poll_addr()

int knob_poll_addr ( const knob_t * k)

The address the next poll reads, or -1 for a knob that is not attached: then no transaction is made.

Parameters
kThe knob.
Returns
The address, or -1 for a knob that is not attached.

◆ knob_poll_done()

int knob_poll_done ( knob_t * k,
bool answered,
const uint8_t bytes[KNOB_POLL_READS] )

End one poll.

answered: every read of knob_poll_regs answered, and bytes[i] is what knob_poll_regs[i] gave. Returns the steps, positive clockwise seen from the top; a poll that was not answered, or a knob that is not attached, returns 0 and forgets the reference. A poll whose two 0x03 bytes differ returns 0 and keeps the reference.

Parameters
kThe knob.
answeredtrue when every read of knob_poll_regs answered.
bytesThe bytes read, one per knob_poll_regs entry.
Returns
The steps, positive clockwise seen from the top; 0 when the poll is not answered.

◆ knob_poll_probe_first()

bool knob_poll_probe_first ( const knob_t * k)

Whether the next poll probes the address before its reads: an attached knob with no reference.

A probe that is not acknowledged ends the poll as not answered, with no read made.

Parameters
kThe knob.
Returns
true when the next poll probes the address first.

◆ knob_reset()

void knob_reset ( knob_t * k)

Forget the reference: the next usable reading moves nothing.

Parameters
kThe knob to reset.

◆ knob_span_fraction()

float knob_span_fraction ( int steps,
int scale_deg )

Fraction of a slider's span that steps moves it when the span is scale_deg degrees of knob.

scale_deg outside the setting's range is clamped to it.

Parameters
stepsThe signed steps moved.
scale_degDegrees of knob per full slider span.
Returns
The fraction of the slider span that steps moves it.

Variable Documentation

◆ knob_poll_regs

const uint8_t knob_poll_regs[KNOB_POLL_READS]
Initial value:
= {
}
#define KNOB_REG_ANGLE_HI
Angle[13:6], bits 7..0.
Definition knob.h:85
#define KNOB_REG_ANGLE_LO
Angle[5:0] in bits 7..2; bits 1..0 not used.
Definition knob.h:87