六 基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr

本文详细介绍以跨平台框架为基础,利用库和库设计实现了基于MQTT协议的PC版步进电机控制客户端 。
编译环境为,使用的语言主要是C++ 。
一、前期准备
本文所使用的跨平台界面库:
官网:
【六基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr】版本 1.5.4
下载地址:
本文使用的两个第三方库:
1、,下载地址:
-1.4.9
源码:
2、
官网地址:
下载地址:
本文将分三个部分来实现:
1、工程创建和界面的大致设计规划;
2、数据库的操作;
3、MQTT协议栈和功能实现 。
以下是具体内容:
二、工程创建和界面的大致设计规划
解压之后:

六  基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr

文章插图
双击-.exe:
六  基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr

文章插图
工程名填:
Name那里中间填公司名,本项目随便填了一个,最右边填App名字,本项目填
然后点击Now 。
跳出一个对话框来,点确定,然后看到:
六  基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr

文章插图
然后关闭 。
看到nano-目录下出现一个文件夹,这个就是我们项目存放的目录,进去之后看到文件夹,里面就是我们的项目了 。
我们先做上的版本,然后再进一步兼容其他平台 。
先把框架建起来:
然后双击nano-\\\proj.win32下的.sln,打开vs工程,把设置为启动项,在debug版编译一下,运行调试,看到:
六  基于FreeRTOS与MQTT的物联网技术应用系列——步进电机控制基于Cr

文章插图
参考官方的test代码(在nano-\\Test目录下),
从test项目下拷贝.h、.cpp、.h、.cpp
我们添加四个文件:.h、.cpp、er.h、er.cpp
具体内容如下:
.h
#ifndef __HelloCpp__RootWindow__#define __HelloCpp__RootWindow__#include #include "CrossApp.h"class RootWindow: public CAWindow, public CAKeypadDelegate{public:static RootWindow* getInstance();RootWindow();virtual ~RootWindow();virtual bool init();virtual void draw();CC_SYNTHESIZE_READONLY(CANavigationController*, m_pRootNavigationController, RootNavigationController);CC_SYNTHESIZE_READONLY(CADrawerController*, m_pRootDrawerController, DrawerController);void initUIView();virtual void keyBackClicked();void buttonCallBack(CAControl* btn,DPoint point);};#endif /* defined(__HelloCpp__ViewController__) */
.cpp:
#include "RootWindow.h"#include "MenuViewController.h"#include "StepMotorControlView.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include #include "platform/android/jni/JniHelper.h"#endifstatic RootWindow* _window = NULL;RootWindow* RootWindow::getInstance(){if (_window == NULL){_window = new RootWindow();_window->init();_window->autorelease();}return _window;}RootWindow::RootWindow():m_pRootNavigationController(NULL),m_pRootDrawerController(NULL){CAApplication::getApplication()->getKeypadDispatcher()->addDelegate(this);}RootWindow::~RootWindow(){CAApplication::getApplication()->getKeypadDispatcher()->removeDelegate(this);if (m_pRootNavigationController)m_pRootNavigationController->release();}bool RootWindow::init(){if (!CAWindow::init()){return false;}CAApplication::getApplication()->setNotificationView(CAView::createWithFrame(this->getBounds(), CAColor_green));this->initUIView();MenuViewController* _menuview = MenuViewController::create();CADrawerController* drawer = new CADrawerController();drawer->initWithController(_menuview, m_pRootNavigationController);drawer->setBackgroundImage(CAImage::create("image/bg.jpg"));drawer->setEffect3D(true);this->setRootViewController(drawer);drawer->autorelease();m_pRootDrawerController = drawer;CAApplication::getApplication()->setNotificationView(NULL);return true;}void RootWindow::draw(){}void RootWindow::initUIView(){do{CAViewController* viewController = m_pRootNavigationController ? m_pRootNavigationController->getViewControllerAtIndex(0) : NULL;CC_BREAK_IF(dynamic_cast(viewController));StepMotorControlView* tabBarController = new StepMotorControlView();tabBarController->init();tabBarController->autorelease();CANavigationBarItem* temp_nav = CANavigationBarItem::create(UTF8("控制器面板"));CABarButtonItem* item = CABarButtonItem::create("", CAImage::create("image/ic_category_list.png"), NULL);item->setTarget(this, CAControl_selector(RootWindow::buttonCallBack));temp_nav->addLeftButtonItem(item);tabBarController->setNavigationBarItem(temp_nav);if (m_pRootNavigationController){m_pRootNavigationController->replaceViewController(tabBarController, false);}else{m_pRootNavigationController = new CANavigationController();m_pRootNavigationController->initWithRootViewController(tabBarController);m_pRootNavigationController->setNavigationBarBackgroundImage(CAImage::create("image/navbg.jpg"));}} while (0);if (m_pRootDrawerController){m_pRootDrawerController->hideLeftViewController(true);}CAApplication::getApplication()->setStatusBarStyle(CAStatusBarStyleLightContent);}void RootWindow::buttonCallBack(CAControl* btn,DPoint point){this->getDrawerController()->showLeftViewController(true);}void RootWindow::keyBackClicked(){CC_RETURN_IF(CAAlertView::hideWithDisplayed());if (this->getModalViewController()){this->dismissModalViewController(true);}else if (this->getDrawerController()->isShowLeftViewController()){this->getDrawerController()->hideLeftViewController(true);}else if (this->getRootNavigationController()->getViewControllerCount() > 1){this->getRootNavigationController()->popViewControllerAnimated(true);}else{CAApplication::getApplication()->end();}}