Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added I2c::getDevices() #38

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
using namespace std;

enum GPIO_MAP_PIN {
GPIO2 = 2, GPIO14 = 14,
GPIO3 = 3, GPIO15 = 15,
GPIO2 = 2, GPIO14 = 14,
GPIO3 = 3, GPIO15 = 15,
GPIO4 = 4, GPIO18 = 18,
GPIO17 = 17, GPIO23 = 23,
GPIO27 = 27, GPIO24 = 24,
GPIO22 = 22, GPIO25 = 25,
GPIO10 = 10, GPIO8 = 8,
GPIO9 = 9, GPIO7 = 7,
GPIO11 = 11, GPIO12 = 12,
GPIO5 = 5, GPIO16 = 16,
GPIO6 = 6, GPIO20 = 20,
GPIO13 = 13, GPIO21 = 21,
GPIO17 = 17, GPIO23 = 23,
GPIO27 = 27, GPIO24 = 24,
GPIO22 = 22, GPIO25 = 25,
GPIO10 = 10, GPIO8 = 8,
GPIO9 = 9, GPIO7 = 7,
GPIO11 = 11, GPIO12 = 12,
GPIO5 = 5, GPIO16 = 16,
GPIO6 = 6, GPIO20 = 20,
GPIO13 = 13, GPIO21 = 21,
GPIO19 = 19,
GPIO26 = 26,
};
Expand Down
26 changes: 26 additions & 0 deletions src/i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <linux/i2c-dev.h>
#include <unistd.h>
#include "i2c.h"
#include <regex>
#include <filesystem>

I2c::I2c(const char * deviceName)
{
Expand Down Expand Up @@ -129,3 +131,27 @@ uint16_t I2c::readBlock(uint8_t command, uint8_t size, uint8_t * data)
return result;
}

std::vector<std::pair<std::string, uint8_t>> I2c::getDevices(uint8_t start /*=0*/, uint8_t end /*128*/)
{
std::vector<std::pair<std::string, uint8_t>> i2cDevices;
std::regex i2cPattern("/dev/i2c-\\d+");
for (const auto& entry : std::filesystem::directory_iterator("/dev")) {
std::string path = entry.path().string();
if (std::regex_match(path, i2cPattern)) {
int f = open(path.c_str(), O_RDWR);
if(f == -1) continue;
for(uint8_t address = 0; address <= end; ++address) {
int result = ioctl(f, I2C_SLAVE, address);
if(result == 0) {
int res = i2c_smbus_read_byte(f);
if (res >0)
{
i2cDevices.push_back(std::make_pair(path, address));
}
}
}
close(f);
}
}
return i2cDevices;
}
3 changes: 3 additions & 0 deletions src/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class I2c {
addressSet(address);
return tryReadByte(command);
}

static std::vector<std::pair<std::string, uint8_t>> getDevices(uint8_t start = 0, u_int8_t end = 128);

private:
int fd;
};
Expand Down
4 changes: 2 additions & 2 deletions src/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LED {
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
}

void setWS2801(int numLed, vector<ofColor> colors, int BRIGHTNESS){
void setWS2801(int numLed, vector<ofColor>& colors, int BRIGHTNESS){
int a;
uint8_t buffer0[1], buffer1[4];
srand(time(NULL));
Expand Down Expand Up @@ -105,7 +105,7 @@ class LED {
}
}

void setAPA102(int numLed, vector<ofColor> colors, int BRIGHTNESS){
void setAPA102(int numLed, vector<ofColor>& colors, int BRIGHTNESS){
int a;
uint8_t buffer0[1], buffer1[4];
srand(time(NULL));
Expand Down
2 changes: 2 additions & 0 deletions src/ofxGPIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ using namespace LogHighLight;
#include "mcp.h"
#include "font.h"
#include "readata.h"

/* only openframeworks */
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
#include "google_image.h"
#include "gpio_state.h"
#endif
/* end OF */

/* core high level */
#include "led.h"
#include "oled.h"
Expand Down