From 4e56894f03b073a73f96741957bdb85fb0cb8493 Mon Sep 17 00:00:00 2001 From: Adrien Sulpice Date: Wed, 6 Aug 2014 21:37:30 -0700 Subject: [PATCH] Adding basic chat system to the hackerling circuit. Added a library in software/libraries/Chat_System. Example of usage in software/projects/chat_system/chat_system.ino --- .../libraries/Chat_System/chat_system.cpp | 96 +++++++++++++++++++ software/libraries/Chat_System/chat_system.h | 67 +++++++++++++ software/projects/chat_system/chat_system.ino | 44 +++++++++ 3 files changed, 207 insertions(+) create mode 100644 software/libraries/Chat_System/chat_system.cpp create mode 100644 software/libraries/Chat_System/chat_system.h create mode 100644 software/projects/chat_system/chat_system.ino diff --git a/software/libraries/Chat_System/chat_system.cpp b/software/libraries/Chat_System/chat_system.cpp new file mode 100644 index 0000000..7991150 --- /dev/null +++ b/software/libraries/Chat_System/chat_system.cpp @@ -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; +} diff --git a/software/libraries/Chat_System/chat_system.h b/software/libraries/Chat_System/chat_system.h new file mode 100644 index 0000000..a392623 --- /dev/null +++ b/software/libraries/Chat_System/chat_system.h @@ -0,0 +1,67 @@ +#ifndef __CHAT_SYSTEM_HACKERLNG_SHIELD_H__ +#define __CHAT_SYSTEM_HACKERLNG_SHIELD_H__ + +#include +#include +#include +#include +#include +#include +#include + +// 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__ diff --git a/software/projects/chat_system/chat_system.ino b/software/projects/chat_system/chat_system.ino new file mode 100644 index 0000000..6239f47 --- /dev/null +++ b/software/projects/chat_system/chat_system.ino @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +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); + } +}