Crate kc11b04

Crate kc11b04 

Source
Expand description

Driver in Rust for the KC11B04 4-button analog keypad.

A 3-pin module using a voltage divider circuit and analog pin to sense a key press.

KC11B04 image

§Wiring notes

The voltage on the VCC pin should correspond with the reference voltage (maximum value) your ADC is configured to read.

For example the default on an Arduino Uno is that it matches the operating voltage of 5V. But the ADC can be configured to use an external reference with the AREF pin. In this case VCC should match AREF.

§Example

use kc11b04::{Key, KC11B04, MAP_10BIT};

let mut adc = /* Configure your ADC using its HAL */
let mut ad_pin = /* Set the pin connected to AD as analog input */

// Create the keypad driver. Taking ownership of the pin,
// providing a map that matches the resolution of your ADC.
let mut keypad = KC11B04::new(ad_pin, MAP_10BIT);

// Somewhere within loop { }
// Read current key state.
match keypad
	.key_state(&mut adc)
	.expect("Problem reading ADC channel")
{
	Some(Key::K4) => { /* K4 key being pressed */ }
	Some(Key::K3) => { /* K3 key being pressed */ }
	Some(Key::K2) => { /* K2 key being pressed */ }
	Some(Key::K1) => { /* K1 key being pressed */ }
	None => { /* Either nothing, or multiple keys pressed */ }
}

§MSRV & Stability

This crate compiles on stable Rust 1.60 and up. It might still work on older rust versions, but this isn’t ensured. Upgrading the MSRV is considered SemVer breaking.

Re-exports§

pub use mapping::KeyMap;

Modules§

mapping
This module maps keys to their expected ADC readings.

Macros§

map_from_max
Defines a KeyMap based on the max reading of the ADC and optional margin factor.

Structs§

KC11B04
KC11B04 analog keypad driver. Constructed with KC11B04::new.

Enums§

Key
A named key on the KC11B04 module.

Constants§

MAP_8BIT
KeyMap for 8bit ADCs with a maximum reading of 255.
MAP_10BIT
KeyMap for 10bit ADCs with a maximum reading of 1023.
MAP_12BIT
KeyMap for 12bit ADCs with a maximum reading of 4095.
MAP_12BIT_SIGNED
KeyMap for signed 12bit ADCs with a maximum positive reading of 2047.
MAP_16BIT
KeyMap for 16bit ADCs with a maximum reading of 65535.
MAP_16BIT_SIGNED
KeyMap for signed 16bit ADCs with a maximum positive reading of 32767.
MAP_24BIT
KeyMap for 24bit ADCs with a maximum reading of 16777215.