Skip to content

Commit

Permalink
Initial Commit. Adding local repo to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
architkithania committed Jan 15, 2020
0 parents commit caf2265
Show file tree
Hide file tree
Showing 16 changed files with 2,471 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CDTimerClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
//
//

#include "CDTimerClass.h"


CDTimerClass CDTimer;

bool CDTimerClass::Start(long duration)
{
m_Start = millis();
m_time = duration;
return true;
}

bool CDTimerClass::Start()
{
return Start(0);
}

bool CDTimerClass::isDue(long duration)
{
if (millis() >= m_Start + m_time + duration)
return true;
else
return false;
}
long CDTimerClass::getTime()
{
return millis() - m_Start;
}
bool CDTimerClass::isDue()
{
return isDue(0);
}

39 changes: 39 additions & 0 deletions CDTimerClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Timer.h
//This Timer has two types.
//1. Count down timer:
// Use Start(duration) to set count down and isDue to check wheather timer due or not.
//2. Normal Timer:
// User Start() to start the timer, and isDue(duration) to check the duration.

#ifndef _CDTIMER_h
#define _CDTIMER_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

class CDTimerClass
{
protected:
unsigned long m_Start; // set start millis
unsigned long m_time = 0; // set count down value.

public:
//Method 1.
bool Start(long duration);
bool isDue();
//Method 2.
bool Start();
bool isDue(long duration);
long getTime();


};

// one Object as a sample.
extern CDTimerClass CDTimer;

#endif

42 changes: 42 additions & 0 deletions FSMClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
//
//

#include "FSMClass.h"

FSMClass FSM;

void FSMClass::init(fpState S)
{
this->transit(S);
}

void FSMClass::transit(fpState S)
{
m_fState = S; // change function pointer.
m_transited = true;
m_timer.Start();
}

void FSMClass::run()
{
if (m_transited == true)
m_doTask = true;
m_transited = false;
m_fState(); // do task when function pointer change.
m_doTask = false;
}

bool FSMClass::isDue(long duration)
{
return m_timer.isDue(duration);
}
bool FSMClass::doTask()
{
return m_doTask;
}

long FSMClass::getTime()
{
return m_timer.getTime();
}
65 changes: 65 additions & 0 deletions FSMclass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// FSM.h
// Usage:
// 1. Declare a Finite state machine object : (code) FSMClass FSMSample
// 2. Create function as first state: (code) void FSMSampleState1(bool task)
// 2. Create function as second state: (code) void FSMSampleState2(bool task)
// 2.1 Take a look of the function sample
// 2. Initialize FSM in setup() function : (code) FSMSample.init(FSMSampleState1);
// 3. Execute the FSM in run() function : FSMSample.run();

#ifndef _FSM_h
#define _FSM_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

#include "CDTimerClass.h"

typedef void(*fpState) (void);

class FSMClass
{
protected:
fpState m_fState; //Function pointer
bool m_transited; //Set to true when funtion pointer transited.
bool m_doTask;
CDTimerClass m_timer;
public:
void init(fpState S); // get initizlise function pointer.
void transit(fpState S); // transite state(change function pointer).
void run(); // run the function pointer.
bool isDue(long duration); // check timer with duration.
bool doTask();
long getTime();
};

extern FSMClass FSM;
/*
//2.1 Take a State function sample
FSMClass FSMSample;
void FSMSampleState1(bool task)
{
if (task) // this is required.
{
// Put what you want to do here.
}
//set condition to leave this state
if (FSMSample.getTime() == 1000) // 1000ms = 1second
FSMSample.transit(FSMSampleState2)
}
void FSMSampleState2(bool task)
{
if (task) // this is required.
{
// Put what you want to do here.
}
//set condition to leave this state
if (FSMSample.getTime() == 2000) // 2000ms = 2second
FSMSample.transit(FSMSampleState1)
}
*/
#endif

28 changes: 28 additions & 0 deletions MainDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _MAINDEF_h
#define _MAINDEF_h
#include "Arduino.h"

#define D0 0
#define D1 1
#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#define D8 8
#define D9 9
#define D10 10
#define D11 11
#define D12 12
#define D13 13

#include "UltrasoundSensorClass.h"
#include "FSMClass.h"
#include "MotorClass.h"
#include "TM1637DisplayClass.h"
#include "SensorClass.h"
#include "ServoClass.h"
#include "OutputClass.h"

#endif
41 changes: 41 additions & 0 deletions MotorClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "Arduino.h"
#include "MotorClass.h"

MotorClass::MotorClass(int Spin, int Dpin)
{
m_DPin = Dpin;
m_SPin = Spin;
m_Speed = 0;
setProperties(255, -255);
pinMode(m_DPin, OUTPUT);
pinMode(m_SPin, OUTPUT);
setSpeed(0);
}

void MotorClass::setSpeed(int SPEED)
{
m_Speed = SPEED;
if(m_Speed>m_maxSpeed)
m_Speed = m_maxSpeed;
if (m_Speed < m_minSpeed)
m_Speed = m_minSpeed;
if (m_Speed>=0)
{
analogWrite(m_SPin, m_Speed);
digitalWrite(m_DPin, LOW);
}else
{
analogWrite(m_SPin, 255+m_Speed);
digitalWrite(m_DPin, HIGH);
}
}
void MotorClass::setProperties(int maxSPEED, int minSPEED)
{
m_maxSpeed = maxSPEED; m_minSpeed = minSPEED;
setSpeed(m_Speed);
}

int MotorClass::getSpeed()
{
return m_Speed;
}
21 changes: 21 additions & 0 deletions MotorClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _Motor_h
#define _Motor_h
#include "Arduino.h"

class MotorClass
{
protected:
int m_DPin; // Direction Pin
int m_SPin; // Speed Pin
int m_maxSpeed;
int m_minSpeed;
int m_Speed;

public:
MotorClass(int Dpin, int Spin);
void setSpeed(int SPEED);
void setProperties(int maxSPEED, int minSPEED);
int getSpeed();
};

#endif
63 changes: 63 additions & 0 deletions OutputClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Arduino.h"
#include "OutputClass.h"

OutputClass::OutputClass(int pin)
{
m_Pin = pin;
m_Value = 0;
pinMode(m_Pin, OUTPUT);
digitalWrite(m_Pin, LOW);
}

bool OutputClass::isValue()
{
switch (m_Pin) {
case 3:
case 5:
case 6:
case 9:
case 10:
case 11:
return true;
break;
default:
return false;
break;
}
}

bool OutputClass::getHiLow()
{
if(m_Value ==0)
return false;
return true;
}
int OutputClass::getValue()
{
return m_Value;
}

void OutputClass::setHiLow(bool b)
{
if(b == false)
{
digitalWrite(m_Pin, LOW);
m_Value = 0;
}else
{
digitalWrite(m_Pin, HIGH);
m_Value = 1;
}
}

bool OutputClass::setValue(int i)
{
if (isValue())
{
m_Value = i;
analogWrite(m_Pin, m_Value);
return true;
}
return false;
}

21 changes: 21 additions & 0 deletions OutputClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _OutputClass_h
#define _OutputClass_h
#include "Arduino.h"


class OutputClass
{
protected:
int m_Pin; //
int m_Value;
bool isValue();

public:
OutputClass(int pin);
bool getHiLow();
int getValue();
void setHiLow(bool b);
bool setValue(int i);
};

#endif
Loading

0 comments on commit caf2265

Please sign in to comment.