1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/examples/riot_and_cpp/main.cpp

109 lines
3.3 KiB
C++
Raw Normal View History

2014-06-25 03:19:39 +02:00
/*
* Copyright (C) 2014 Hamburg University of Applied Sciences (HAW)
* Copyright (C) 2014 Ho Chi Minh University of Technology (HCMUT)
*
2014-08-23 15:43:13 +02:00
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
2014-06-25 03:19:39 +02:00
*/
/**
* @file
2014-06-25 03:19:39 +02:00
* @brief Demonstration of mixed c++ and c user application with pure c RIOT
* - mixing of c and c++ source to test name mangling
* - introducing a namespace to declarative block, avoiding to qualify calls, e.g. std::vector
* - using private and public member functions, e.g. 'cpp_obj.greet()' cannot be accessed from main.cpp
* - overloading of function 'cpp_obj.say_hello(...)' for 'none', 'int' or 'float'
* - demonstration of templated c++ container 'std::vector'
* - usage of iterator to access elements of the container type
*
* @author Martin Landsmann <martin.landsmann@haw-hamburg.de>
* @author DangNhat Pham-Huu <51002279@hcmut.edu.vn>
*/
#include "architecture.h"
2014-06-25 03:19:39 +02:00
#include "thread.h"
#include "c_functions.h"
#include <cstdio>
#include <vector>
#include "cpp_class.hpp"
2014-06-25 03:19:39 +02:00
using namespace std;
/* thread's stack */
char threadA_stack [THREAD_STACKSIZE_MAIN];
2014-06-25 03:19:39 +02:00
/* thread's function */
void *threadA_func(void *arg);
2014-06-25 03:19:39 +02:00
/* main */
int main()
{
printf("\n************ RIOT and C++ demo program ***********\n");
printf("\n");
/* create thread A */
thread_create(threadA_stack, sizeof(threadA_stack), 0,
THREAD_CREATE_WOUT_YIELD,
threadA_func, NULL, "thread A");
2014-06-25 03:19:39 +02:00
printf("******** Hello, you're in thread #%" PRIkernel_pid " ********\n",
thread_getpid());
2014-06-25 03:19:39 +02:00
printf("We'll test C++ class and methods here!\n");
cpp_class cpp_obj;
printf("\n-= Test overloading functions =-\n");
cpp_obj.say_hello();
cpp_obj.say_hello(42);
cpp_obj.say_hello(3.141592f);
printf("\n-= Test namespace =-\n");
printf("typing std::vector is obsolete when 'using namespace std;'\n");
vector<int> vInts;
vInts.push_back(1);
vInts.push_back(3);
vInts.push_back(2);
printf("The vector vInts has been filled with %" PRIuSIZE " numbers.\n", vInts.size());
2014-06-25 03:19:39 +02:00
printf("\n-= Test iterator =-\n");
printf("The content of vInts = { ");
for (vector<int>::iterator it = vInts.begin(); it != vInts.end(); ++it) {
printf("%d ", *(it));
}
printf("}\n");
return 0;
}
2019-10-23 21:23:14 +02:00
/* thread A function implementation */
void *threadA_func(void *)
2014-06-25 03:19:39 +02:00
{
int day = 13, month = 6, year = 2014;
int ret_day;
printf("******** Hello, you're in thread #%" PRIkernel_pid " ********\n",
thread_getpid());
2014-06-25 03:19:39 +02:00
printf("We'll test some C functions here!\n");
printf("\n-= hello function =-\n");
hello();
printf("\n-= day_of_week function =-\n");
printf("day %d, month %d, year %d is ", day, month, year);
ret_day = day_of_week(day, month, year);
if (ret_day >= 0){
char day_of_week_table[][32] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
2014-06-25 03:19:39 +02:00
printf("%s\n", day_of_week_table[ret_day]);
}
printf("\nThis demo ends here, press Ctrl-C to exit (if you're on native)!\n");
return NULL;
2014-06-25 03:19:39 +02:00
}