Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from EESC-USP-TUPA/issue-3
Browse files Browse the repository at this point in the history
Issue 3
  • Loading branch information
AndreyCortez authored Feb 23, 2022
2 parents 7614e44 + d0f3b6b commit 1ee2837
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions I2C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "I2C.h"

I2C::I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay){
this->handler = handler;
this->endereco = endereco;
this->delay = delay;
}


I2C::~I2C() {
HAL_I2C_DeInit(this->handler);
}


uint8_t *I2C::ler(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho) {
uint8_t *dados = (uint8_t*)malloc(tamanho * sizeof(uint8_t));
HAL_I2C_Mem_Read(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay);
return dados;
}


bool I2C::escrever(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho, uint8_t* dados) {
return (HAL_I2C_Mem_Write(this->handler, this->endereco, registrador, tam_registrador, dados, tamanho, this->delay) == HAL_OK);
}
22 changes: 22 additions & 0 deletions I2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef I2C_H
#define I2C_H

#include "main.h"
#include <stdlib.h>
#include <stdint.h>

class I2C {
private:
uint32_t delay;
I2C_HandleTypeDef *handler;
uint16_t endereco;

public:
I2C(I2C_HandleTypeDef* handler, uint16_t endereco, uint32_t delay);
~I2C();
uint8_t *ler(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho);
bool escrever(uint16_t tam_registrador, uint16_t registrador, uint16_t tamanho, uint8_t* dados);

};

#endif

0 comments on commit 1ee2837

Please sign in to comment.