Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusCavini committed May 16, 2024
0 parents commit 5d4f8d8
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"string": "cpp"
}
}
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
46 changes: 46 additions & 0 deletions lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
57 changes: 57 additions & 0 deletions lib/alarmSystem/alarmSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <Arduino.h>
#include "displaySystem.h"
#include "alarmSystem.h"
#include "stateMachine.h"

#define SOUND_SPEED 0.034 //cm/us

const int trigPin = 18;
const int echoPin = 5;


long duration;
float distanceCm;

void alarm_init(){
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
digitalWrite(trigPin, LOW);
}

float measureLevel(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
return distanceCm;
}

int lastLevel = HIGH;

void checkLevel(){
float dist = measureLevel();
int newLevel;
if(dist > 10){
newLevel = HIGH;
}else{
newLevel = LOW;
}

if(newLevel != lastLevel){
if(newLevel == HIGH){
addEvent(LEVEL_GOES_UP);
}else{
addEvent(LEVEL_GOES_DOWN);
}
}

lastLevel = newLevel;
}
11 changes: 11 additions & 0 deletions lib/alarmSystem/alarmSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef ALARM_SYSTEM_INCLUDED
#define ALARM_SYSTEM_INCLUDED




void alarm_init();
float measureLevel();
void checkLevel();

#endif
46 changes: 46 additions & 0 deletions lib/diplaySystem/displaySystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "displaySystem.h"

LiquidCrystal_I2C lcd(0x27, 16, 2);

void display_init(){
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
}

void displayShow(char string[], int row, int column){
lcd.setCursor(row,column);
lcd.print(string);
}

void displayTime(int H, int M, int S){
char hour[3];
char min[3];
char sec[3];

if(H<10){
sprintf(hour, "0%d", H);
}else{
sprintf(hour, "%d", H);
}

if(M<10){
sprintf(min, "0%d", M);
}else{
sprintf(min, "%d", M);
}

if(S<10){
sprintf(sec, "0%d", S);
}else{
sprintf(sec, "%d", S);
}


char timeFormat[9];
sprintf(timeFormat, "%s:%s:%s", hour, min, sec);
lcd.print(timeFormat);
}
8 changes: 8 additions & 0 deletions lib/diplaySystem/displaySystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef DISPLAY_SYSTEM_INCLUDED
#define DISPLAY_SYSTEM_INCLUDED

void display_init();
void displayShow(char string[], int row, int column);
void displayTime(int H, int M, int S);

#endif
87 changes: 87 additions & 0 deletions lib/stateMachine/stateMachine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <Arduino.h>
#include "displaySystem.h"
#include "alarmSystem.h"
#include "stateMachine.h"

//Matriz que relaciona estado atual e evento com o próximo estado
int stateTransitionMatrix[STATES_QNT][EVENTS_QNT];

//Matriz que relaciona o estado atual com a saída (Moore)
int stateOutputsMatrix[STATES_QNT];

//Inicialização das Matrizes e mapeamento das relações
void stateMachine_init(){
for(int i=0; i<STATES_QNT; i++){
for(int j=0; j<EVENTS_QNT; j++){
stateTransitionMatrix[i][j] = i;
stateOutputsMatrix[i] = NO_OUTPUT;
}
}

stateTransitionMatrix[IDLE][LEVEL_GOES_DOWN] = LOW_LEVEL;
stateOutputsMatrix[IDLE] = DISPLAY_IDLE;

stateTransitionMatrix[LOW_LEVEL][LEVEL_GOES_UP] = IDLE;
stateOutputsMatrix[LOW_LEVEL] = DISPLAY_LOW_LEVEL;
}

//Consulta a matriz de transição e retorna o próximo estado
int getNextState(int state, int event){
return stateTransitionMatrix[state][event];
}

//Consulta a matriz de saídas e retorna a saída
int getOutput(int state){
return stateOutputsMatrix[state];
}

//Executa uma ação de saída (ex: display, motores, led)
void handleOutput(int output){
switch (output)
{
case DISPLAY_IDLE:
displayShow("ESTADO IDLE", 0,0);
break;
case DISPLAY_LOW_LEVEL:
displayShow("NIVEL BAIXO!",0,0);
break;

default:
break;
}
}


#define MAX_EVENTS 50
int EventsQueue[MAX_EVENTS];

void eventQ_init(){
for(int i=0; i<MAX_EVENTS; i++){
EventsQueue[i]= NO_EVENT;
}
}

int eventCount = 0;

void addEvent(int event){
int i;
for(i=0; i<eventCount; i++);
EventsQueue[i] = event;
eventCount ++;
}

int getEvent(){
if (eventCount == 0) {
return NO_EVENT; // No events in the queue
} else {
int firstEvent = EventsQueue[0]; // Get the first event
// Shift elements to the left to remove the first event
for (int i = 0; i < eventCount - 1; i++) {
EventsQueue[i] = EventsQueue[i + 1];
}
EventsQueue[eventCount - 1] = NO_EVENT; // Clear the last element
eventCount--; // Decrement the event count
return firstEvent; // Return the dequeued event
}

}
30 changes: 30 additions & 0 deletions lib/stateMachine/stateMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef STATE_MACHINE_INCLUDE
#define STATE_MACHINE_INCLUDED

#define STATES_QNT 2
#define EVENTS_QNT 3

//DEFINES DOS NUMEROS RELATIVOS A CADA ESTADO
#define IDLE 0
#define LOW_LEVEL 1

//DEFINES DOS NUMEROS RELATIVOS A CADA EVENTO
#define NO_EVENT 0
#define LEVEL_GOES_DOWN 1
#define LEVEL_GOES_UP 2

//DEFINES DOS NUMEROS RELATIVOS AS SAIDAS
#define NO_OUTPUT 0
#define DISPLAY_IDLE 1
#define DISPLAY_LOW_LEVEL 2

void stateMachine_init();
int getNextState(int state, int event);
int getOutput(int state);
void handleOutput(int output);
void eventQ_init();
int getEvent();
void addEvent(int event);


#endif
15 changes: 15 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = marcoschwartz/LiquidCrystal_I2C@^1.1.4
Loading

0 comments on commit 5d4f8d8

Please sign in to comment.