Skip to content

Commit

Permalink
added logger class
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheppard committed Aug 26, 2024
1 parent 8a6a30c commit f762afb
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
8 changes: 0 additions & 8 deletions include/DebugSerial.h

This file was deleted.

5 changes: 3 additions & 2 deletions include/DistanceSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#define DISTANCESENSOR_H

#include <Arduino.h>
#include "Logger.h"

#define TRIGGER_INTERVAL 500
#define ECHO_INPUT 18
#define TRIGGER_OUTPUT 19
#define ECHO_INPUT 18 // PC4
#define TRIGGER_OUTPUT 19 // PC5

class DistanceSensor
{
Expand Down
21 changes: 21 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef SOFTWARESERIAL_H
#define SOFTWARESERIAL_H

#include <Arduino.h>
#include <SoftwareSerial.h>

#define SOFTWARE_SERIAL_RX 7
#define SOFTWARE_SERIAL_TX 8

class Logger
{
private:
static SoftwareSerial *softwareSerial;
static bool isDefaultSerial;

public:
static void init(bool isDefaultSerial);
static void debug(String message);
};

#endif
8 changes: 4 additions & 4 deletions include/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "DebugSerial.h"
#include "Logger.h"

// Definicja pinów do komunikacji z modułem Bluetooth
#define RX_BT 0 // Pin RX modułu Bluetooth (do odbioru danych)
#define TX_BT 1 // Pin TX modułu Bluetooth (do wysyłania danych)
#define RX_BT 2 // Pin RX modułu Bluetooth (do odbioru danych)
#define TX_BT 3 // Pin TX modułu Bluetooth (do wysyłania danych)

class Bluetooth
{
Expand Down Expand Up @@ -40,7 +40,7 @@ class Bluetooth

if (dataComplete)
{
Serial.println(this->data);
Logger::debug("BT: " + this->data);
callback(this->data);

this->data = "";
Expand Down
10 changes: 5 additions & 5 deletions src/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Car::turnLEDOn()

void Car::goForward()
{
Serial.println("Jade do przodu");
Logger::debug("Jade do przodu");

digitalWrite(ENGINE_INPUT_1, LOW);
digitalWrite(ENGINE_INPUT_2, HIGH);
Expand All @@ -65,7 +65,7 @@ void Car::goForward()

void Car::goBackward()
{
Serial.println("Jade do tylu");
Logger::debug("Jade do tylu");

digitalWrite(ENGINE_INPUT_1, HIGH);
digitalWrite(ENGINE_INPUT_2, LOW);
Expand All @@ -77,7 +77,7 @@ void Car::goBackward()

void Car::turnLeft()
{
Serial.println("Jade w lewo");
Logger::debug("Jade w lewo");

digitalWrite(ENGINE_INPUT_1, LOW);
digitalWrite(ENGINE_INPUT_2, HIGH);
Expand All @@ -89,7 +89,7 @@ void Car::turnLeft()

void Car::turnRight()
{
Serial.println("Jade w prawo");
Logger::debug("Jade w prawo");

digitalWrite(ENGINE_INPUT_1, LOW);
digitalWrite(ENGINE_INPUT_2, LOW);
Expand All @@ -101,7 +101,7 @@ void Car::turnRight()

void Car::stop()
{
Serial.println("Halt!");
Logger::debug("Halt!");

digitalWrite(ENGINE_INPUT_1, LOW);
digitalWrite(ENGINE_INPUT_2, LOW);
Expand Down
3 changes: 0 additions & 3 deletions src/DebugSerial.cpp

This file was deleted.

1 change: 0 additions & 1 deletion src/DistanceSensor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "DistanceSensor.h"
#include "DebugSerial.h"

DistanceSensor::DistanceSensor() : distance(-1)
{
Expand Down
24 changes: 24 additions & 0 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Logger.h"

SoftwareSerial *Logger::softwareSerial = nullptr;
bool Logger::isDefaultSerial = true;

void Logger::init(bool isDefaultSerial)
{
Logger::isDefaultSerial = isDefaultSerial;
Logger::softwareSerial = new SoftwareSerial(SOFTWARE_SERIAL_RX, SOFTWARE_SERIAL_TX);
Logger::softwareSerial->begin(9600);
Serial.begin(9600);
}

void Logger::debug(String message)
{
if (Logger::isDefaultSerial)
{
Serial.println(message);
}
else
{
Logger::softwareSerial->println(message);
}
}
8 changes: 3 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Arduino.h>
#include "Car.h"
#include "DebugSerial.h"
#include "Logger.h"
#include "DistanceSensor.h"
#include "bluetooth.h"

Expand All @@ -10,10 +10,8 @@ void setup()
{
car.begin();

Serial.begin(9600);
Serial.begin(9600);

Serial.println("Wlaczono");
Logger::init(false);
Logger::debug("Wlaczono");
}

void loop()
Expand Down

0 comments on commit f762afb

Please sign in to comment.