Skip to content
Snippets Groups Projects
Commit 19ee20ef authored by jdkruege's avatar jdkruege
Browse files

Added in a singleton mailbox where services can deliver messages. Worked on gyro service.

Message has been revamped to handle data and get it for askers, the mailbox will be used to ship messages between services and users.
parent fa58b146
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
<ClCompile Include="Communications.cpp" /> <ClCompile Include="Communications.cpp" />
<ClCompile Include="GyroService.cpp" /> <ClCompile Include="GyroService.cpp" />
<ClCompile Include="LSM9DS0.cpp" /> <ClCompile Include="LSM9DS0.cpp" />
<ClCompile Include="Mailbox.cpp" />
<ClCompile Include="Message.cpp" /> <ClCompile Include="Message.cpp" />
<ClCompile Include="Sensors.cpp" /> <ClCompile Include="Sensors.cpp" />
<ClCompile Include="Server.cpp" /> <ClCompile Include="Server.cpp" />
...@@ -28,6 +29,7 @@ ...@@ -28,6 +29,7 @@
<ClInclude Include="Communications.h" /> <ClInclude Include="Communications.h" />
<ClInclude Include="GyroService.h" /> <ClInclude Include="GyroService.h" />
<ClInclude Include="LSM9DS0.h" /> <ClInclude Include="LSM9DS0.h" />
<ClInclude Include="Mailbox.h" />
<ClInclude Include="Message.h" /> <ClInclude Include="Message.h" />
<ClInclude Include="Sensors.h" /> <ClInclude Include="Sensors.h" />
<ClInclude Include="Server.h" /> <ClInclude Include="Server.h" />
......
...@@ -45,6 +45,9 @@ ...@@ -45,6 +45,9 @@
<ClCompile Include="GyroService.cpp"> <ClCompile Include="GyroService.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Mailbox.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Communications.h"> <ClInclude Include="Communications.h">
...@@ -74,6 +77,9 @@ ...@@ -74,6 +77,9 @@
<ClInclude Include="GyroService.h"> <ClInclude Include="GyroService.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Mailbox.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="Makefile.txt"> <Text Include="Makefile.txt">
......
#include "GyroService.h" #include "GyroService.h"
GyroService::GyroService(int id, double freq) : Service(id, "Gyro", freq) void GyroService::run()
{ {
sensor = LSM9DS0::instance(); while (1)
{
// Sample the registers to figure out X, Y, and Z
short X = _sensor->read_G(0x29) && _sensor->read_G(0x28);
short Y = _sensor->read_G(0x2B) && _sensor->read_G(0x2A);
short Z = _sensor->read_G(0x2D) && _sensor->read_G(0x2C);
// Create a message to send with our name, the name of who it needs to go to, and the data to be carried
Message* msg = new Message("Gyro Service", "User", { (double)X, (double)Y, (double)Z });
// Add our msg into the mail to be sent
_mailbox->send(msg);
// Sleep 5 seconds
sleep(5);
}
} }
void GyroService::stop()
{
GyroService::~GyroService() }
GyroService::GyroService(int id, double freq) : Service(id, "Gyro", freq)
{ {
// Get the instances for the sensor and the mailbox we need to use
_sensor = LSM9DS0::instance();
_mailbox = Mailbox::instance();
_thread = new thread(&run);
} }
void GyroService::run() GyroService::~GyroService()
{ {
short X = sensor.read_G(0x29) && sensor.read_G(0x28);
short Y = sensor.read_G(0x2B) && sensor.read_G(0x2A);
short Z = sensor.read_G(0x2D) && sensor.read_G(0x2C);
} }
#pragma once #pragma once
#include "Service.h" #include "Service.h"
#include "LSM9DS0.h" #include "LSM9DS0.h"
#include "Mailbox.h"
class GyroService : class GyroService :
public Service public Service
...@@ -9,10 +10,13 @@ public: ...@@ -9,10 +10,13 @@ public:
GyroService(int id, double freq); GyroService(int id, double freq);
~GyroService(); ~GyroService();
void start();
void run(); void run();
void stop();
private: private:
LSM9DS0 sensor; LSM9DS0* _sensor;
Mailbox* _mailbox;
thread* _thread;
}; };
#include "Mailbox.h"
Mailbox::Mailbox()
{
}
Mailbox::~Mailbox()
{
}
#pragma once
#include "Message.h"
class Mailbox
{
public:
static Mailbox* instance();
Mailbox();
~Mailbox();
bool send(Message *msg);
};
#include "Message.h" #include "Message.h"
Message::Message() Message::Message(string sender, string destination, list<double> data)
{ {
_sender = sender;
_destination = destination;
_data = data;
} }
string Message::getSender()
{
return _sender;
}
string Message::getDestination()
{
return _destination;
}
Message::~Message() list<double> Message::getData()
{ {
return _data;
} }
...@@ -9,12 +9,12 @@ using namespace std; ...@@ -9,12 +9,12 @@ using namespace std;
class Message class Message
{ {
public: public:
Message(); Message(string sender, string destination, list<double> data);
~Message(); ~Message();
string getSender(); string getSender();
string getDestination(); string getDestination();
list<double> getData();
ostream& operator>>(ostream& os); ostream& operator>>(ostream& os);
...@@ -23,7 +23,7 @@ private: ...@@ -23,7 +23,7 @@ private:
string _sender; string _sender;
string _destination; string _destination;
list<double> data; list<double> _data;
}; };
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <list> #include <list>
#include <thread>
using namespace std; using namespace std;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment