From a2cff3b2bf090f8de1a216550358e31822e5218a Mon Sep 17 00:00:00 2001 From: librae8226 Date: Wed, 13 Dec 2017 16:19:36 +0800 Subject: [PATCH] c++ support experiment, tested ok on mk3060, esp32 --- include/aos/internal/log_impl.h | 2 +- sketch/cpp_class.cpp | 56 +++++++++++++++++++++++++++++ sketch/cpp_class.h | 64 +++++++++++++++++++++++++++++++++ sketch/ino.cpp | 9 +++++ sketch/sketch.cpp | 35 ++++++++++++++++++ sketch/sketch.mk | 26 ++++++++++++++ 6 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 sketch/cpp_class.cpp create mode 100644 sketch/cpp_class.h create mode 100644 sketch/ino.cpp create mode 100644 sketch/sketch.cpp create mode 100644 sketch/sketch.mk diff --git a/include/aos/internal/log_impl.h b/include/aos/internal/log_impl.h index acb020bed3..150b73a3bd 100644 --- a/include/aos/internal/log_impl.h +++ b/include/aos/internal/log_impl.h @@ -64,7 +64,7 @@ extern int csp_printf(const char *fmt, ...); #define log_print(CON, MOD, COLOR, LVL, FMT, ...) \ do { \ if (CON) { \ - csp_printf("[%06d]<" LVL "> "FMT"\n", (unsigned)aos_now_ms(), ##__VA_ARGS__); \ + csp_printf("[%06d]<" LVL "> " FMT "\n", (unsigned)aos_now_ms(), ##__VA_ARGS__); \ } \ } while (0) diff --git a/sketch/cpp_class.cpp b/sketch/cpp_class.cpp new file mode 100644 index 0000000000..f428c7e335 --- /dev/null +++ b/sketch/cpp_class.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2014 Hamburg University of Applied Sciences (HAW) + * Copyright (C) 2014 Ho Chi Minh city University of Technology (HCMUT) + * + * 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. + */ + +/** + * @ingroup examples + * @{ + * + * @file + * @brief implementation of declared functions of object cpp_class + * + * @author Martin Landsmann + * @author DangNhat Pham-Huu <51002279@hcmut.edu.vn> + * + * @} + */ + +#include +#include "cpp_class.h" + +cpp_class::cpp_class() +{ + printf("Instanciating Object [constructor called]\n"); + greet(); +} + +cpp_class::~cpp_class() +{ + printf("Destroying Object [destructor called]\n"); + printf("Im shutting down!\n"); +} + +void cpp_class::say_hello(void) +{ + printf("Hello!\n"); +} + +void cpp_class::say_hello(int n) +{ + printf("Hello![int: %d]\n", n); +} + +void cpp_class::say_hello(float f) +{ + printf("Hello![float: %f]\n", f); +} + +void cpp_class::greet(void) +{ + printf("Im starting!\n"); +} diff --git a/sketch/cpp_class.h b/sketch/cpp_class.h new file mode 100644 index 0000000000..b30f0c8b71 --- /dev/null +++ b/sketch/cpp_class.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014 Hamburg University of Applied Sciences (HAW) + * Copyright (C) 2014 Ho Chi Minh city University of Technology (HCMUT) + * + * 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. + */ + +/** + * @defgroup + * @brief + * @ingroup examples + * @{ + * + * @file + * @brief simple c++ object declaration with public and private functions + * + * @author Martin Landsmann + * @author DangNhat Pham-Huu <51002279@stu.hcmut.edu.vn> + */ + +#ifndef CPP_CLASS_H +#define CPP_CLASS_H + +class cpp_class +{ +public: + /** + * @brief constructor + */ + cpp_class(); + + /** + * @brief destructor + */ + ~cpp_class(); + + /** + * @brief public function + */ + void say_hello(void); + + /** + * @brief overloaded public function with int parameter + */ + void say_hello(int n); + + /** + * @brief overloaded public function with float parameter + */ + void say_hello(float f); +private: + /** + * @brief private function + */ + void greet(void); +}; + +void setup(void); +void loop(void); + +/** @} */ +#endif /* CPP_CLASS_H */ diff --git a/sketch/ino.cpp b/sketch/ino.cpp new file mode 100644 index 0000000000..f3aec8856e --- /dev/null +++ b/sketch/ino.cpp @@ -0,0 +1,9 @@ +#include "cpp_class.h" + +void setup() { + cpp_class cpp_obj; + cpp_obj.say_hello((float)3.141592653); +} + +void loop() { +} diff --git a/sketch/sketch.cpp b/sketch/sketch.cpp new file mode 100644 index 0000000000..d04b454ba6 --- /dev/null +++ b/sketch/sketch.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015-2017 Alibaba Group Holding Limited + */ + +#if defined (__cplusplus) +extern "C" { +#endif +#include +#include +#if defined (__cplusplus) +} +#endif +#include +#include "cpp_class.h" + +static void app_delayed_action(void *arg) +{ + LOG("%s:%d %s\r\n", __func__, __LINE__, aos_task_name()); + aos_post_delayed_action(5000, app_delayed_action, NULL); +} + +extern "C" int application_start(int argc, char *argv[]) +{ + cpp_class cpp_obj; + + aos_post_delayed_action(1000, app_delayed_action, NULL); + + printf("hello\n"); + cpp_obj.say_hello(42); + setup(); + + aos_loop_run(); + + return 0; +} diff --git a/sketch/sketch.mk b/sketch/sketch.mk new file mode 100644 index 0000000000..b473b11327 --- /dev/null +++ b/sketch/sketch.mk @@ -0,0 +1,26 @@ +NAME := sketch + +$(NAME)_SOURCES := \ + sketch.cpp \ + cpp_class.cpp \ + ino.cpp + +GLOBAL_DEFINES += AOS_NO_WIFI + +$(NAME)_COMPONENTS := yloop cli + +#$(NAME)_CXXFLAGS := -std=c++11 -fno-rtti -fno-exceptions + +ifeq ($(BENCHMARKS),1) +$(NAME)_COMPONENTS += benchmarks +GLOBAL_DEFINES += CONFIG_CMD_BENCHMARKS +endif + +ifneq (,${BINS}) +GLOBAL_CFLAGS += -DSYSINFO_OS_BINS +endif + +CURRENT_TIME = $(shell /bin/date +%Y%m%d.%H%M) +CONFIG_SYSINFO_APP_VERSION = APP-1.0.0-$(CURRENT_TIME) +$(info app_version:${CONFIG_SYSINFO_APP_VERSION}) +GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"