Skip to content

Commit

Permalink
Serial Communication
Browse files Browse the repository at this point in the history
Adds serial communication and debugging between microcontrollers
  • Loading branch information
TekuConcept committed Mar 22, 2017
1 parent 3a22c67 commit 615a629
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 58 deletions.
8 changes: 8 additions & 0 deletions ArduinoController/Debugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _DEBUGGER_
#define _DEBUGGER_

#define DMSG(x) Serial1.print(x)
#define DHEX(x) Serial1.print(x,HEX)
#define DMSGN(x) Serial1.println(x)

#endif
2 changes: 2 additions & 0 deletions ArduinoController/IController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define ITHRUSTER_H

#include "Arduino.h"
#include "SerialTools.h"
#include "PinMap.h"

class IController {
public:
Expand Down
28 changes: 28 additions & 0 deletions ArduinoController/PinMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef PIN_MAP_H
#define PIN_MAP_H

#define KILLSWITCH_PIN 50

#define LED_STAT_PIN 22
#define LED_CTRL_PIN 52

#define LIGHTS_PIN 45

#define MOVE_PIN 13
#define STRAFE_PIN 12
#define DIVE_PIN 11

#define YAW_PIN 9
#define PITCH_PIN 8
#define ROLL_PIN 7

#define ESC_S1_PIN 36
#define ESC_S2_PIN 39
#define ESC_S3_PIN 41
#define ESC_S4_PIN 47
#define ESC_S5_PIN 49
#define ESC_S6_PIN 53

#define VOLT_PIN 10

#endif
145 changes: 145 additions & 0 deletions ArduinoController/SerialTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Created by TekuConcept on 8/3/2016.
*/
#ifndef SERIAL_TOOLS_H
#define SERIAL_TOOLS_H

#include "Debugger.h"

/**
* Extend the functionality of the Serial object,
* providing various useful type value streams.
*/
class SerialTools{
public:

/* - - - - - - - - - - - - - - - - - - - - - - - - - - *\
* READ FUNCTIONS *
\* - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* reading strings is not yet implemented here */
static uint8_t readByte() {
uint8_t result = 0;
readData((char*)&result, sizeof(uint8_t));
return result;
}

static float readFloat() {
float result = 0;
readData((char*)&result, sizeof(float));
return result;
}

static double readDouble() {
double result = 0;
readData((char*)&result, sizeof(double));
return result;
}

static int32_t readInt() {
int result = 0;
readData((char*)&result, sizeof(int32_t));
return result;
}

static uint32_t readUInt() {
uint32_t result = 0;
readData((char*)&result, sizeof(uint32_t));
return result;
}

static int16_t readShort() {
int16_t result = 0;
readData((char*)&result, sizeof(int16_t));
return result;
}

static uint16_t readUShort() {
uint16_t result = 0;
readData((char*)&result, sizeof(uint16_t));
return result;
}

static void readData(char* ptr, int size) {
while(Serial.available() < size);
for(int i = 0; i < size; i++) {
ptr[i] = Serial.read();
DHEX((uint8_t)ptr[i]);
}
DMSGN();
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - *\
* WRITE FUNCTIONS *
\* - - - - - - - - - - - - - - - - - - - - - - - - - - */

/**
* Writes a null-terminated string to the Serial's
* out buffer.
*/
static void writeString(const char* data, int size) {
for(int i = 0; i < size; i++) {
Serial.print(data[i]);
}
Serial.print('\0');
}

static void writeByte(uint8_t out) {
writeData((char*)&out, sizeof(uint8_t));
}

static void writeFloat(float out) {
writeData((char*)&out, sizeof(float));
}

static void writeDouble(double out) {
writeData((char*)&out, sizeof(double));
}

static void writeInt(int32_t out) {
writeData((char*)&out, sizeof(int32_t));
}

static void writeUInt(uint32_t out) {
writeData((char*)&out, sizeof(uint32_t));
}

static void writeShort(int16_t out) {
writeData((char*)&out, sizeof(int16_t));
}

static void writeUShort(uint16_t out) {
writeData((char*)&out, sizeof(uint16_t));
}

static void writeData(char* ptr, int size) {
for(int i = 0; i < size; i++){
Serial.print(ptr[i]);
}
}

static void printHex(uint8_t c) {
printHexChar((uint8_t)((c&0xF0) >> 4));
printHexChar((uint8_t)(c&0x0F));
}

private:
SerialTools(){}
static void printHexChar(uint8_t val) {
char res = '0';
if(val < 10)
Serial.print(val);
else
switch(val-10) {
case 0: res = 'A'; break;
case 1: res = 'B'; break;
case 2: res = 'C'; break;
case 3: res = 'D'; break;
case 4: res = 'E'; break;
case 5: res = 'F'; break;
}
Serial.print(res);
}
};

#endif
54 changes: 38 additions & 16 deletions BoneCentral/Peripherals/Peripherals.SerialAdaptors/include/Serial.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
//
// Created by Nathan Copier on 1/28/2016.
// Refactored by TekuConcept on 8/3/2016.
//

#ifndef PERIPHERALS_SERIAL_H
#define PERIPHERALS_SERIAL_H

#include <fstream>
#define DUE

#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <memory>
#include <cstdlib>
#include <thread>
#include <mutex>
#include <json.h>
#include <ScriptLogger.h>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>

using json = nlohmann::json;
#define LOG(x) std::cerr << x

class Serial {
private:
std::shared_ptr<std::ifstream> input_;
std::shared_ptr<std::ofstream> output_;
std::shared_ptr<std::ScriptLogger> logger_;
static std::mutex serialLock_;
bool arduinoInitialized_;
int fd;

void configure();
void ackno();

public:
Serial();
void writeByte(unsigned short byteValue);
void writeShort(unsigned short shortValue);
json readJson();
char readChar();
void initializeArduino();
bool getArduinoInitialized();
Serial(std::string device);
~Serial();

std::string readString();
unsigned char readByte();
float readFloat();
double readDouble();
int readInt();
unsigned int readUInt();
short readShort();
unsigned short readUShort();
void readData(char* ptr, size_t size);

void writeFloat(float value);
void writeDouble(double value);
void writeInt(int value);
void writeUInt(unsigned int value);
void writeShort(short value);
void writeUShort(unsigned short value);
void writeByte(unsigned char value);
void writeData(char* ptr, size_t size);
};


#endif //PERIPHERALS_SERIAL_H
#endif //PERIPHERALS_SERIAL_H
Loading

0 comments on commit 615a629

Please sign in to comment.