Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature test usart into release #12

Merged
merged 6 commits into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 66 additions & 43 deletions .travis.yml
@@ -1,55 +1,78 @@
dist: trusty
sudo: required
language: cpp

notifications:
email: false

branches:
only:
- master
- release
only:
- master
- release

addons:
apt_packages:
- lib32bz2-1.0
- lib32ncurses5
- lib32z1
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise
packages:
- gcc-7
- g++-7



before_script:
- wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/8-2018q4/gcc-arm-none-eabi-8-2018-q4-major-linux.tar.bz2? -O /tmp/gcc-arm-none-eabi.tar.bz2
- tar -xjf /tmp/gcc-arm-none-eabi.tar.bz2
- export PATH=$PATH:$PWD/gcc-arm-none-eabi-8-2018-q4-major/bin/
- g++ -v
- gcc -v
- arm-none-eabi-g++ -v
apt_packages:
- lib32bz2-1.0
- lib32ncurses5
- lib32z1
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise
packages:
- gcc-7
- g++-7

cache:
# cache the files for 3 hours
timeout: 10800

directories:
# cache the arm-none-eabi location
- $HOME/arm-none-eabi

install:
- cd ..
- git clone https://github.com/R2D2-2019/R2D2-build.git
- cd R2D2-build
- git submodule update --init --recursive
- cd ..
- sudo unlink /usr/bin/gcc && sudo ln -s /usr/bin/gcc-7 /usr/bin/gcc # Change symlinks of gcc to gcc-7
- sudo unlink /usr/bin/g++ && sudo ln -s /usr/bin/g++-7 /usr/bin/g++
- export PATH=$PATH:/usr/bin/g++:/usr/bin/gcc
- sudo cp -r /usr/bin/gcc /bin/gcc
- sudo cp -r /usr/bin/g++ /bin/g++


# clone git repo to the $HOME folder
- git clone --recursive -j8 https://github.com/R2D2-2019/R2D2-build.git $HOME/r2d2

# make installer executable
- chmod +x $HOME/r2d2/programs/travis-ci/install_arm-eabi-gcc.sh

# run installer
- $HOME/r2d2/programs/travis-ci/install_arm-eabi-gcc.sh

# add arm-eabi-gcc to path
- export PATH=$PATH:$HOME/arm-none-eabi/bin/

# Change symlinks of gcc to gcc-7
- sudo unlink /usr/bin/gcc && sudo ln -s /usr/bin/gcc-7 /usr/bin/gcc
- sudo unlink /usr/bin/g++ && sudo ln -s /usr/bin/g++-7 /usr/bin/g++

# link to /bin/ location
- sudo ln -s /usr/bin/gcc /bin/gcc
- sudo ln -s /usr/bin/g++ /bin/g++

jobs:
include:
- script:
- cd R2D2-build/modules
- cp -r $TRAVIS_BUILD_DIR $PWD/test_module
- cd test_module/code
- make build
- make clean
include:
- script:
# copy the currently building repo into a build_module
- cp -r $TRAVIS_BUILD_DIR $HOME/r2d2/modules/build_module

# go into the modules folder of the build system
- cd $HOME/r2d2/modules/build_module/test

# build the module
- make build
- make run
- make clean

- script:
# copy the currently building repo into a build_module
- cp -r $TRAVIS_BUILD_DIR $HOME/r2d2/modules/build_module

# go into the modules folder of the build system
- cd $HOME/r2d2/modules/build_module/code

# build the module
- make build
- make clean
2 changes: 2 additions & 0 deletions code/headers/hardware_usart.hpp
@@ -1,6 +1,7 @@
#pragma once

#include <hwlib.hpp>
#include <stdint.h>
#include <queue.hpp>
#include <uart_ports.hpp>
#include <usart_connection.hpp>
Expand Down Expand Up @@ -73,5 +74,6 @@ namespace r2d2 {
/// @brief returns available data in buffer
/// @return amount of uint8_t's in buffer
unsigned int available() override;

};
}; // namespace r2d2
41 changes: 0 additions & 41 deletions code/headers/test_usart.cpp

This file was deleted.

29 changes: 20 additions & 9 deletions code/headers/test_usart.hpp
Expand Up @@ -2,22 +2,21 @@
#include <usart_connection.hpp>
#include <uart_ports.hpp>
#include <queue.hpp>
#include <hwlib.hpp>
#include <vector>
#include <string>

namespace r2d2 {

class test_usart_c : public usart_connection_c {
private:
unsigned int boudrate;
uart_ports_c usart_port;
queue_c<uint8_t, 250> input_buffer;
queue_c<uint8_t, 250> receive_buffer;


public:

test_usart_c(unsigned int baudrate, uart_ports_c usart_port);
test_usart_c();

/// @brief does not actualy disable anything
/// @brief does not actualy enable anything
void enable() override;

/// @brief does not actualy disable anyting
Expand All @@ -36,16 +35,28 @@ namespace r2d2 {
uint8_t receive() override;

///@brief returns true if char is available
///@return bool always true
///@return bool false if queue is empty, true if not
bool char_available() override;

///@brief returns receive()
///@return char uint8_t from recieve
char getc() override;

///@brief returns 1
///@return unsigned int always 1
///@return unsigned int the amount of bytes in queue
unsigned int available() override;


/// @brief sets a string the test usart will return
void set_receive_string(const std::string &str);

/// @brief sets bytes the test usart will return
/// @param bytes vector of bytes
void set_receive_bytes(const std::vector<uint8_t> &bytes);

/// @brief sets one byte the test usart will return
/// @param byte to be returned in receive
void add_receive_byte(const uint8_t byte);

};
};
};
5 changes: 3 additions & 2 deletions code/headers/usart_connection.hpp
Expand Up @@ -3,7 +3,8 @@
/// @author Patrick Dekker

#pragma once
#include <hwlib.hpp>

#include <stdint.h>

namespace r2d2 {

Expand Down Expand Up @@ -50,4 +51,4 @@ namespace r2d2 {
virtual unsigned int available() =0;
};

};
};
1 change: 1 addition & 0 deletions code/main.cpp
Expand Up @@ -8,5 +8,6 @@ int main(void) {
WDT->WDT_MR = WDT_MR_WDDIS;
hwlib::wait_ms(1000);
hwlib::cout << "this works on arduino";
auto hwusart = r2d2::hardware_usart_c(9600, r2d2::uart_ports_c::uart1);
itzandroidtab marked this conversation as resolved.
Show resolved Hide resolved
// nothing of use here
}
57 changes: 57 additions & 0 deletions code/src/test_usart.cpp
@@ -0,0 +1,57 @@
#include <test_usart.hpp>

namespace r2d2 {

test_usart_c::test_usart_c(){}

void test_usart_c::enable(){
//Not needed in test implementation
}

void test_usart_c::disable(){
//Not needed in test implementation
}

bool test_usart_c::send(const uint8_t c) {
return true;
//Not needed in test implementation
}

void test_usart_c::putc(char c) {
send(c);
}

uint8_t test_usart_c::receive() {
return receive_buffer.copy_and_pop();
}

bool test_usart_c::char_available() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between char_available() and available()? seems a bit redundant they return the exact same atm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char_available returns true if char is available, available returns the amound of bytes available

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But both char_available() and available() return !receive_buffer.empty() that is unintended then i guess

return !receive_buffer.empty();
}

char test_usart_c::getc() {
return receive();
}

unsigned int test_usart_c::available() {
return !receive_buffer.empty();
}

void test_usart_c::set_receive_string(const std::string &str) {
receive_buffer.clear();
for(char c : str){
add_receive_byte(c);
}
}

void test_usart_c::set_receive_bytes(const std::vector<uint8_t> &bytes){
receive_buffer.clear();
for(auto &byte : bytes){
add_receive_byte(byte);
}
}

void test_usart_c::add_receive_byte(const uint8_t byte){
receive_buffer.push(byte);
}
};
4 changes: 2 additions & 2 deletions test/Makefile
Expand Up @@ -9,10 +9,10 @@
#############################################################################

# source files in this project (main.cpp is automatically assumed)
SOURCES := $(wildcard ../code/src/*.cpp)
SOURCES := ../code/src/test_usart.cpp

# header files in this project
HEADERS := $(wildcard ../code/headers/*.hpp)
HEADERS := ../code/headers/test_usart.hpp ../code/headers/uart_ports.hpp

# other places to look for files for this project
SEARCH := ../code/headers ../code/src
Expand Down
60 changes: 58 additions & 2 deletions test/main.cpp
@@ -1,4 +1,60 @@
#include "ostream"

#include <ostream>
#include <string>
#define CATCH_CONFIG_MAIN
#include <catch.hpp>

#include <test_usart.hpp>
#include <uart_ports.hpp>

TEST_CASE("test_usart_c sends","[test_usart_c]"){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a test with the set_receive_string() and the set_receive_bytes()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were added in 85fdce9

auto test_usart = r2d2::test_usart_c();

REQUIRE(test_usart.send(0xAA));
}

TEST_CASE("test_usart_c chars availiable ", "[test_usart_c]"){
auto test_usart = r2d2::test_usart_c();

REQUIRE(!test_usart.char_available());

test_usart.add_receive_byte(0x24);

REQUIRE(test_usart.char_available() == true);
}

TEST_CASE("test_usart_c receives", "[test_usart_c]"){
auto test_usart = r2d2::test_usart_c();
test_usart.add_receive_byte(0x25);

REQUIRE(test_usart.receive() == 0x25);

}

TEST_CASE("test_usart_c is empty after receiving", "[test_usart_c]"){
auto test_usart = r2d2::test_usart_c();
test_usart.add_receive_byte(0x25);
test_usart.receive();
REQUIRE(!test_usart.available());
}

TEST_CASE("set_receive_string()","[test_usart_c]"){
auto test_usart = r2d2::test_usart_c();
std::string str1 = "this is a test string";
std::string str2;
test_usart.set_receive_string(str1);
while(test_usart.char_available()){
str2 += test_usart.getc();
}
REQUIRE(str1 == str2);
}

TEST_CASE("set_receive_bytes()","[test_usart_c]"){
auto test_usart = r2d2::test_usart_c();
std::vector<uint8_t> vec1 = {0xAA, 0x25, 0x01, 0x56};
std::vector<uint8_t> vec2;
test_usart.set_receive_bytes(vec1);
while(test_usart.char_available()){
vec2.push_back(test_usart.receive());
}
REQUIRE(vec1 == vec2);
}