A not so clean working i2c implementation

master
Kerem Yollu 4 years ago
parent 515e9055d0
commit ae81a1f11d

@ -0,0 +1,13 @@
#ifndef _EXECUTEBASH_H_
#define _EXECUTEBASH_H_
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
std::string execBash(const char* cmd);
#endif //_EXECUTEBASH_H_

@ -0,0 +1,16 @@
#include "i2c_driver.h"
#include "systemCall.h"
I2C_Driver::I2C_Driver()
{
}
std::string I2C_Driver::readByte(const uint8_t& address, const uint8_t& reg)
{
std::string addrs = std::to_string(address);
std::string regs = std::to_string(reg);
std::string command = "i2cget -y 1 " + addrs + " " + regs;
char* c = const_cast<char*>(command.c_str());
return execBash(c);
}

@ -0,0 +1,24 @@
#include <stdint.h>
#include <iostream>
#include <string>
class I2C_Driver
{
public:
I2C_Driver();
std::string readByte(const uint8_t& address, const uint8_t& reg);
uint16_t readWord(const uint8_t& address, const uint8_t& reg);
bool writeByte(const uint8_t& address, const uint8_t& reg, const uint8_t& data); // retuns 0 when a sucsessful transation ocures
bool writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data); // retuns 0 when a sucsessful transation ocures
private:
/* unsigned char device_address;
unsigned char device_reg;
unsigned char send_buffer[100];
unsigned char recieve_buffer[100];
unsigned char blocks;
*/
};

@ -8,11 +8,20 @@
*/
#include <iostream>
#include <stdint.h>
#include "errorHandling.h"
#include "commandManager.h"
#include "systemCall.h"
#include "i2c_driver.h"
ErrorHandler errorHandle;
CommandManager commander;
I2C_Driver i2cDriver;
int initPlatform()
@ -24,7 +33,7 @@ int initPlatform()
void dummy()
{
std::cout << "HElllooo from the the other side signed adele" << std::endl;
std::cout<< i2cDriver.readByte(0x40,0x00) <<std::endl;
}
int main(int argc, char *argv[])
@ -34,11 +43,12 @@ int main(int argc, char *argv[])
std::cout << "Main" << std::endl;
//errorHandle.addNewError(-34,"Test eroor 1");
//errorHandle.handleError(-34);
//commander.addNewCommand("test", "The test command for testing the test", dummy);
//commander(argv[1]);
commander.addNewCommand("test", "The test command for testing the test", dummy);
commander.addNewCommand("i2c", "The test command for testing the test", dummy);
commander(argv[1]);
return 1;
}

Binary file not shown.

@ -0,0 +1,15 @@
#include "systemCall.h"
std::string execBash(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}

@ -0,0 +1,14 @@
#ifndef _SYSTEMCALL_H_
#define _SYSTEMCALL_H_
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
// Ide from : https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
std::string execBash(const char* cmd);
#endif // _SYSTEMCALL_H_
Loading…
Cancel
Save