Skip to content

Commit

Permalink
Merge pull request #352 from pimoroni/motor-and-encoder
Browse files Browse the repository at this point in the history
Added support for motors and encoders
  • Loading branch information
Gadgetoid authored May 16, 2022
2 parents aa75a7b + 717ad90 commit cbf1aba
Show file tree
Hide file tree
Showing 141 changed files with 13,056 additions and 36 deletions.
43 changes: 43 additions & 0 deletions common/pimoroni_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#define PIMORONI_I2C_DEFAULT_INSTANCE i2c0
#define PIMORONI_SPI_DEFAULT_INSTANCE spi0

// Macro to return a value clamped between a minimum and maximum
#ifndef CLAMP
#define CLAMP(a, mn, mx) ((a)<(mx)?((a)>(mn)?(a):(mn)):(mx))
#endif


namespace pimoroni {
static const unsigned int PIN_UNUSED = INT_MAX; // Intentionally INT_MAX to avoid overflowing MicroPython's int type

Expand Down Expand Up @@ -53,6 +59,11 @@ namespace pimoroni {
ACTIVE_HIGH = 1
};

enum Direction {
NORMAL_DIR = 0,
REVERSED_DIR = 1,
};

inline uint32_t millis() {
return to_ms_since_boot(get_absolute_time());
}
Expand All @@ -74,4 +85,36 @@ namespace pimoroni {
162, 163, 165, 167, 169, 170, 172, 174, 176, 178, 179, 181, 183, 185, 187, 189,
191, 193, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220,
222, 224, 227, 229, 231, 233, 235, 237, 239, 241, 244, 246, 248, 250, 252, 255};

struct pin_pair {
union {
uint8_t first;
uint8_t a;
uint8_t positive;
uint8_t phase;
};
union {
uint8_t second;
uint8_t b;
uint8_t negative;
uint8_t enable;
};

pin_pair() : first(0), second(0) {}
pin_pair(uint8_t first, uint8_t second) : first(first), second(second) {}
};

struct bool_pair {
union {
bool first;
bool a;
};
union {
bool second;
bool b;
};

bool_pair() : first(false), second(false) {}
bool_pair(bool first, bool second) : first(first), second(second) {}
};
}
3 changes: 3 additions & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_subdirectory(bme68x)
add_subdirectory(bmp280)
add_subdirectory(bme280)
add_subdirectory(button)
add_subdirectory(pid)
add_subdirectory(plasma)
add_subdirectory(rgbled)
add_subdirectory(icp10125)
Expand All @@ -29,4 +30,6 @@ add_subdirectory(hub75)
add_subdirectory(uc8151)
add_subdirectory(pwm)
add_subdirectory(servo)
add_subdirectory(encoder)
add_subdirectory(motor)
add_subdirectory(vl53l5cx)
11 changes: 11 additions & 0 deletions drivers/analogmux/analogmux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace pimoroni {
gpio_init(en_pin);
gpio_set_dir(en_pin, GPIO_OUT);
}

if(muxed_pin != PIN_UNUSED) {
gpio_set_input_enabled(muxed_pin, true);
}
}

void AnalogMux::select(uint8_t address) {
Expand Down Expand Up @@ -77,4 +81,11 @@ namespace pimoroni {
pull_downs &= ~(1u << address);
}
}

bool AnalogMux::read() {
if(muxed_pin != PIN_UNUSED) {
return gpio_get(muxed_pin);
}
return false;
}
}
1 change: 1 addition & 0 deletions drivers/analogmux/analogmux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace pimoroni {
void select(uint8_t address);
void disable();
void configure_pulls(uint8_t address, bool pullup, bool pulldown);
bool read();

private:
uint addr0_pin;
Expand Down
1 change: 1 addition & 0 deletions drivers/encoder/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include(encoder.cmake)
16 changes: 16 additions & 0 deletions drivers/encoder/encoder.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(DRIVER_NAME encoder)
add_library(${DRIVER_NAME} INTERFACE)

target_sources(${DRIVER_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/encoder.cpp
)

pico_generate_pio_header(${DRIVER_NAME} ${CMAKE_CURRENT_LIST_DIR}/encoder.pio)

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
hardware_pio
)
Loading

0 comments on commit cbf1aba

Please sign in to comment.