Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions software/libraries/Chat_System/chat_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "chat_system.h"

LCD lcd;
IR_COM ir_com;

User::User(HardwareSerial* printer)
: printer_(printer) {}

void User::ShowMessage(String text) {
Serial.println(text);
if (text.length() > 16) {
text = text.substring(0, 14) + "..";
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(previous_line_);
lcd.setCursor(0,1);
lcd.print(text);
previous_line_ = text;
}

void User::Init() {
Serial.begin(9600);
lcd.begin(16,2);
// Useful?
hs.begin();
}

bool User::HasMessageToSend() {
if (available_) {
return true;
}
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') {
available_ = true;
return true;
} else {
current_reading_line_ += c;
}
}
return false;
}

String User::GetMessageToSend() {
String result;
if (available_) {
result = current_reading_line_;
current_reading_line_ = "";
available_ = false;
}
return result;
}

void IRInterface::Init() {
ir_com.begin();
}

void IRInterface::Send(const Message& message) {
String text = message.name + ":" + message.text;
for (int i = 0; i < text.length(); ++i) {
ir_com.write(text[i]);
}
ir_com.write('\0');
}

bool IRInterface::ReceivedMessage() {
if (available_) {
return true;
}
while (ir_com.available() > 0) {
char c = ir_com.read();
if (c == '\0') {
available_ = true;
return true;
} else {
current_message_ += c;
}
}
return false;
}

Message IRInterface::GetReceivedMessage() {
Message result;
if (available_) {
int i = 0;
while (i < current_message_.length() && current_message_[i] != ':') {
result.name += current_message_[i];
++i;
}
result.text = current_message_.substring(i + 1, current_message_.length());
current_message_ = "";
available_ = false;
}
return result;
}
67 changes: 67 additions & 0 deletions software/libraries/Chat_System/chat_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef __CHAT_SYSTEM_HACKERLNG_SHIELD_H__
#define __CHAT_SYSTEM_HACKERLNG_SHIELD_H__

#include <IR_COM.h>
#include <Wire.h>
#include <MCP23017.h>
#include <Hackerling_Shield.h>
#include <HardwareSerial.h>
#include <MCP23008.h>
#include <LCD.h>

// Represents the user of the arduino. Allows to read the messages he wrote and
// show him messages on the computer screen.
// Uses the Serial to communicate with the user.
class User {
public:
// Creates an user object.
// params:
// * printer: pointer to the arduino Serial.
User(HardwareSerial* printer);

// Initializes the user. Need to be call before calling any other function.
void Init();

// Shows a message to the user screen.
void ShowMessage(String text);

// Returns yes if the user wrote a message that is waiting to be sent.
bool HasMessageToSend();

// Get the message the user wrote.
String GetMessageToSend();

private:
String current_reading_line_;
bool available_;
String previous_line_;
HardwareSerial* printer_;
};

struct Message {
String name;
String text;
};

// Allows to communicate with other shields though infra red.
class IRInterface {
public:
// Initializes the IR interface. Has to be called before any other function
// call.
void Init();

// Returns true if a message from another user has been received.
bool ReceivedMessage();

// Get the message that has been received from another user.
Message GetReceivedMessage();

// Send a message to other users by infra red.
void Send(const Message& message);

private:
String current_message_;
bool available_;
};

#endif // __CHAT_SYSTEM_HACKERLNG_SHIELD_H__
44 changes: 44 additions & 0 deletions software/projects/chat_system/chat_system.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <IR_COM.h>
#include <Wire.h>
#include <MCP23017.h>
#include <Hackerling_Shield.h>
#include <HardwareSerial.h>
#include <MCP23008.h>
#include <LCD.h>

#include <chat_system.h>

User user(&Serial);
IRInterface ir_interface;
String my_name;

void setup() {
// Initialises the user and IR interface.
ir_interface.Init();
user.Init();
// Initializes username.
my_name = "Adrien";
// Show a welcome message to the user.
user.ShowMessage("Welcome to the");
user.ShowMessage("chat system");
}

void loop() {
if (ir_interface.ReceivedMessage()) {
Message message = ir_interface.GetReceivedMessage();
user.ShowMessage(message.name + ": " + message.text);
// Optional improvement.
// Add forwarding of message to allow more than 2 users to join the chat.
}

if (user.HasMessageToSend()) {
String line = user.GetMessageToSend();
// Show to the user his own message.
user.ShowMessage("you: " + line);
// Initializes a message and send it.
Message message;
message.name = my_name;
message.text = line;
ir_interface.Send(message);
}
}