Added AnalogMux to C++

This commit is contained in:
ZodiusInfuser 2022-03-25 23:41:44 +00:00
parent 1b79e85be7
commit f9a1979d8d
5 changed files with 122 additions and 0 deletions

View File

@ -1,4 +1,5 @@
add_subdirectory(analog)
add_subdirectory(analogmux)
add_subdirectory(esp32spi)
add_subdirectory(ioexpander)
add_subdirectory(ltp305)

View File

@ -0,0 +1 @@
include(analogmux.cmake)

View File

@ -0,0 +1,11 @@
set(DRIVER_NAME analogmux)
add_library(${DRIVER_NAME} INTERFACE)
target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp
)
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib)

View File

@ -0,0 +1,80 @@
#include "analogmux.hpp"
#include <cstdio>
namespace pimoroni {
AnalogMux::AnalogMux(uint addr0_pin, uint addr1_pin, uint addr2_pin,
uint en_pin, uint muxed_pin)
: addr0_pin(addr0_pin), addr1_pin(addr1_pin), addr2_pin(addr2_pin)
, en_pin(en_pin), muxed_pin(muxed_pin), pull_ups(0), pull_downs(0) {
gpio_init(addr0_pin);
gpio_set_dir(addr0_pin, GPIO_OUT);
max_address = 0b001;
if(addr1_pin != PIN_UNUSED) {
gpio_init(addr1_pin);
gpio_set_dir(addr1_pin, GPIO_OUT);
max_address = 0b011;
if(addr2_pin != PIN_UNUSED) {
gpio_init(addr2_pin);
gpio_set_dir(addr2_pin, GPIO_OUT);
max_address = 0b111;
}
}
if(en_pin != PIN_UNUSED) {
gpio_init(en_pin);
gpio_set_dir(en_pin, GPIO_OUT);
}
}
void AnalogMux::select(uint8_t address) {
if(address <= max_address) {
bool to_pull_up = (pull_ups & (1u << address));
bool to_pull_down = (pull_downs & (1u << address));
if((muxed_pin != PIN_UNUSED) && !(to_pull_up || to_pull_down)) {
gpio_disable_pulls(muxed_pin);
}
gpio_put(addr0_pin, address & 0b001);
if(addr1_pin != PIN_UNUSED) {
gpio_put(addr1_pin, address & 0b010);
}
if(addr2_pin != PIN_UNUSED) {
gpio_put(addr2_pin, address & 0b100);
}
if(en_pin != PIN_UNUSED) {
gpio_put(en_pin, true);
}
if((muxed_pin != PIN_UNUSED) && (to_pull_up || to_pull_down)) {
gpio_set_pulls(muxed_pin, to_pull_up, to_pull_down);
}
sleep_us(10); // Add a delay to let the pins settle before taking a reading
}
}
void AnalogMux::disable() {
if(en_pin != PIN_UNUSED) {
gpio_put(en_pin, false);
}
}
void AnalogMux::configure_pulls(uint8_t address, bool pullup, bool pulldown) {
if(address <= max_address) {
if(pullup)
pull_ups |= (1u << address);
else
pull_ups &= ~(1u << address);
if(pulldown)
pull_downs |= (1u << address);
else
pull_downs &= ~(1u << address);
}
}
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
#include "pico/stdlib.h"
#include "common/pimoroni_common.hpp"
namespace pimoroni {
class AnalogMux {
public:
AnalogMux(uint addr0_pin, uint addr1_pin = PIN_UNUSED, uint addr2_pin = PIN_UNUSED,
uint en_pin = PIN_UNUSED, uint muxed_pin = PIN_UNUSED);
void select(uint8_t address);
void disable();
void configure_pulls(uint8_t address, bool pullup, bool pulldown);
private:
uint addr0_pin;
uint addr1_pin;
uint addr2_pin;
uint en_pin;
uint muxed_pin;
uint max_address;
uint8_t pull_ups;
uint8_t pull_downs;
};
}