Skip to content

Commit

Permalink
I2C and Memory modules added
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Feb 28, 2024
1 parent 3400171 commit 9054355
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/config/config_sample.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

// This is the configuration file for the robot
// Fill all the required the parameters and
Expand Down
13 changes: 12 additions & 1 deletion src/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
#include <Arduino.h>

// Load the configuration details
// This is excluded from git VSC since it may contains credentials and secrets
#include "config/config.h"

// pin map
#include "config/pins.h"
#include "config/pins.h"

// Global variables
#include "config/global_variables.h"

// *** Utilities ************************************************

#include "utilities/i2c/i2c.h"
#include "utilities/memory/memory.h"

// *** Modules **************************************************
12 changes: 12 additions & 0 deletions src/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <Arduino.h>
#include "define.h"

int ROBOT_ID;

// This is to hide non-test related source code.
// https://docs.platformio.org/en/latest/plus/unit-testing.html
#ifndef UNIT_TEST
Expand All @@ -14,7 +16,17 @@ void setup()
Serial.begin(115200);
Serial.println("PeraSwarm Robot v5");

memory.begin(); // NOTE: This should be run as the first thing.

// This commands should be run 'ONLY' at the first run to assign a ID for robot
// memory.setupRobotWithId(RobotId)
// memory.setupRobotWithId(0);
ROBOT_ID = memory.getRobotId();

pinMode(PIN_LED_INBUILT, OUTPUT);

// Scan available I2C devices
i2c_scan();
}

void loop()
Expand Down
27 changes: 27 additions & 0 deletions src/utilities/i2c/i2c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "i2c.h"

void i2c_scan()
{
Serial.println("I2C scanner. Scanning ...");
byte count = 0;

Wire.begin();
Serial.println("...");
for (byte i = 8; i < 120; i++)
{
// Serial.println (i);
Wire.beginTransmission(i);
if (Wire.endTransmission() == 0)
{
Serial.print("Found address: ");
Serial.print(i, DEC);
Serial.print(" (0x");
Serial.print(i, HEX);
Serial.println(")");
count++;
delay(1); // maybe unneeded?
} // end of good response
} // end of for loop

Serial.printf("Done.......\nFound %d device(s)\n\n", count);
}
6 changes: 6 additions & 0 deletions src/utilities/i2c/i2c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <Arduino.h>
#include <Wire.h>

void i2c_scan();
3 changes: 3 additions & 0 deletions src/utilities/memory/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "memory.h"

SW_Memory memory;
5 changes: 5 additions & 0 deletions src/utilities/memory/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "./src/robot_memory.h"

extern SW_Memory memory;
82 changes: 82 additions & 0 deletions src/utilities/memory/src/robot_memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @brief Swarm Robot Memory Manager Library
* @author Nuwan Jaliyagoda, Pasan Tennakoon, Dilshani Karunarathna
* @version 1.0.0
* @url N/A
*
* ------------------------------------------------------------------------------
*/

#include <Arduino.h>
#include "EEPROM.h"
#include "robot_memory.h"

#include "config/pins.h"
#include "config/definitions.h"

#define MIN_ROBOT_ID 0
#define MAX_ROBOT_ID 31

#define EEPROM_SIZE 10

SW_Memory::SW_Memory()
{ // Memory constructor
}

void SW_Memory::begin()
{
this->displayMemoryDetails();
EEPROM.begin(EEPROM_SIZE);
Serial.println(F(">> Memory\t:enabled"));
}

void SW_Memory::displayMemoryDetails()
{
Serial.printf("\n\nThe program uses %d kB and has free space of %d kB\n\n", ESP.getSketchSize() / 1000, ESP.getFreeSketchSpace() / 1000);
}

int SW_Memory::getRobotId()
{ // TODO: implement a variable as cache for reduce the number of memory read occurances
return (int)this->read(EEPROM_ROBOT_ID);
}

boolean SW_Memory::getMemoryStatus()
{
return (this->read(EEPROM_PROGRAMMED) == 1);
}

uint8_t SW_Memory::read(int adr)
{
return EEPROM.read(adr);
}
void SW_Memory::write(uint16_t address, uint8_t data)
{ // address: [0-511], data: [0-255]
EEPROM.write(address, data);
delay(200);
EEPROM.commit();
}
void SW_Memory::test()
{ // A dummy Function for now
}

void SW_Memory::setupRobotWithId(uint8_t id)
{
this->setRobotId(id);
}

void SW_Memory::setRobotId(uint8_t id)
{
if (id >= MIN_ROBOT_ID && id <= MAX_ROBOT_ID)
{
EEPROM.write(EEPROM_ROBOT_ID, id);
delay(200);
EEPROM.write(EEPROM_PROGRAMMED, 1);
delay(200);
EEPROM.commit();
Serial.printf("Mem_Done\t: %d is written as Robot Id\n", id);
}
else
{
Serial.printf("Mem_Error\t: %d is an invalid Robot Id\n", id);
}
}
46 changes: 46 additions & 0 deletions src/utilities/memory/src/robot_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @brief Swarm Robot Memory Manager Library
* @author Nuwan Jaliyagoda, Pasan Tennakoon, Dilshani Karunarathna
* @version 1.0.0
* @url N/A
*
* ------------------------------------------------------------------------------
*/

#ifndef SW_Memory_h
#define SW_Memory_h

#include <Arduino.h>

class SW_Memory
{
public:
SW_Memory();

int getRobotId();
int8_t getErrorCorrection(uint8_t id);

void begin();
void test();

void setupRobotWithId(uint8_t id);

void setRobotId(uint8_t id);

boolean getMemoryStatus();

// REM: Better to keep following 2 private
uint8_t read(int adr);
void write(uint16_t address, uint8_t data);

enum addr
{
EEPROM_PROGRAMMED,
EEPROM_ROBOT_ID
};

private:
void displayMemoryDetails();
};

#endif

0 comments on commit 9054355

Please sign in to comment.