BA / SerialCom

Fork of OmniWheels by Gustav Atmel

SerialServerSend.cpp

Committer:
gustavatmel
Date:
2018-05-01
Revision:
1:9c5af431a1f1

File content as of revision 1:9c5af431a1f1:

/*
 * SerialServer.cpp
 * Copyright (c) 2017, ZHAW
 * All rights reserved.
 *
 *  Created on: 05.06.2017
 *      Author: Marcel Honegger
 */

#include <vector>
#include <mbed.h>
#include "SerialServerSend.h"


using namespace std;

const float SerialServerSend::PERIOD = 0.001f;     // the period of the timer interrupt, given in [s]
/**
 * Create a serial server object.
 

 
SerialServer::SerialServer(RawSerial& serial, StateMachine& stateMachine) : serial(serial), stateMachine(stateMachine), thread(osPriorityRealtime, STACK_SIZE) {
*/
SerialServerSend::SerialServerSend(RawSerial& serial) : serial(serial), thread(osPriorityRealtime, STACK_SIZE) { 
    // initialize local values
    
    input.clear();
    output.clear();
    
    // start thread and ticker timer interrupt service routine
    
    thread.start(callback(this, &SerialServerSend::run));
    ticker.attach(callback(this, &SerialServerSend::sendSignal), PERIOD);
}

/**
 * Delete the serial server object and release all allocated resources.
 */
SerialServerSend::~SerialServerSend() {
    
    ticker.detach();
}

/**
 * This method is called by the ticker timer interrupt service routine.
 * It sends a signal to the thread to make it run again.
 */
void SerialServerSend::sendSignal() {
    
    thread.signal_set(signal);
}

/**
 * This <code>run()</code> method contains an infinite loop with the run logic.
 */
void SerialServerSend::run() {
    
    Serial pc(PA_2, PA_3); // tx, rx
    pc.baud(9600);
    
    while (true) {
        
        // wait for the periodic signal
        
        thread.signal_wait(signal);

        if (output.size() == 0){
            output += "Hello nicolas\n";
        } 
       
        // transmit output
        while (serial.writeable() && (output.size() > 0)) {
            //pc.printf("Send To P: %s\r\n",output[0]);

            serial.putc(output[0]);
            output.erase(0, 1);
        }
    }
    
}