mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-06-24 00:21:05 +08:00
Merge pull request #361 from BernardXiong/master
Add cplusplus and sensor framework.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# C++ support for RT-Thread #
|
||||
|
||||
This is the C++ component in RT-Thread RTOS. In order to support C++ language, this component
|
||||
implement a basic environment, such as new/delete operators.
|
||||
|
||||
Because RT-Thread RTOS is used in embedded system mostly, there are some rules for C++ applications:
|
||||
1. DOES NOT use exception.
|
||||
2. DOES NOT use Run-Time Type Information (RTTI).
|
||||
3. Template is discouraged and it easily causes code text large.
|
||||
4. Static class variables are discouraged. The time and place to call their constructor function could not be precisely controlled and make multi-threaded programming a nightmare.
|
||||
5. Multiple inheritance is strongly discouraged, as it can cause intolerable confusion.
|
||||
|
||||
*NOTE*: For armcc compiler, the libc must be enable.
|
||||
@@ -0,0 +1,11 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <rtthread.h>
|
||||
#include "crt.h"
|
||||
|
||||
void *operator new(size_t size)
|
||||
{
|
||||
return rt_malloc(size);
|
||||
}
|
||||
|
||||
void *operator new[](size_t size)
|
||||
{
|
||||
return rt_malloc(size);
|
||||
}
|
||||
|
||||
void operator delete(void *ptr)
|
||||
{
|
||||
rt_free(ptr);
|
||||
}
|
||||
|
||||
void operator delete[] (void *ptr)
|
||||
{
|
||||
return rt_free(ptr);
|
||||
}
|
||||
|
||||
void __cxa_pure_virtual(void)
|
||||
{
|
||||
rt_kprintf("Illegal to call a pure virtual function.\n");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef CRT_H_
|
||||
#define CRT_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void *operator new(size_t size);
|
||||
void *operator new[](size_t size);
|
||||
|
||||
void operator delete(void * ptr);
|
||||
void operator delete[] (void *ptr);
|
||||
|
||||
extern "C" void __cxa_pure_virtual(void);
|
||||
|
||||
#endif
|
||||
@@ -750,11 +750,12 @@ int nfs_open(struct dfs_fd *file)
|
||||
if (file->flags & DFS_O_CREAT)
|
||||
{
|
||||
if (nfs_mkdir(nfs, file->path, 0755) < 0)
|
||||
return -1;
|
||||
return -DFS_STATUS_EAGAIN;
|
||||
}
|
||||
|
||||
/* open directory */
|
||||
dir = nfs_opendir(nfs, file->path);
|
||||
if (dir == RT_NULL) return -DFS_STATUS_ENOENT;
|
||||
file->data = dir;
|
||||
}
|
||||
else
|
||||
@@ -766,20 +767,20 @@ int nfs_open(struct dfs_fd *file)
|
||||
if (file->flags & DFS_O_CREAT)
|
||||
{
|
||||
if (nfs_create(nfs, file->path, 0664) < 0)
|
||||
return -1;
|
||||
return -DFS_STATUS_EAGAIN;
|
||||
}
|
||||
|
||||
/* open file (get file handle ) */
|
||||
fp = rt_malloc(sizeof(nfs_file));
|
||||
if (fp == RT_NULL)
|
||||
return -1;
|
||||
return -DFS_STATUS_ENOMEM;
|
||||
|
||||
handle = get_handle(nfs, file->path);
|
||||
if (handle == RT_NULL)
|
||||
{
|
||||
rt_free(fp);
|
||||
|
||||
return -1;
|
||||
return -DFS_STATUS_ENOENT;
|
||||
}
|
||||
|
||||
/* get size of file */
|
||||
@@ -798,7 +799,7 @@ int nfs_open(struct dfs_fd *file)
|
||||
|
||||
/* set private file */
|
||||
file->data = fp;
|
||||
file->size = fp->size;
|
||||
file->size = fp->size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# SConscript for sensor framework
|
||||
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd, cwd + '/../include']
|
||||
|
||||
group = DefineGroup('Sensors', src, depend = ['RT_USING_SENSOR', 'RT_USING_DEVICE'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#include <stddef.h>
|
||||
#include "sensor.h"
|
||||
|
||||
/**
|
||||
* Sensor
|
||||
*/
|
||||
Sensor::Sensor()
|
||||
{
|
||||
this->next = this->prev = NULL;
|
||||
Subscribe(NULL, NULL);
|
||||
}
|
||||
|
||||
Sensor::~Sensor()
|
||||
{
|
||||
}
|
||||
|
||||
int Sensor::GetType(void)
|
||||
{
|
||||
return this->type;
|
||||
}
|
||||
|
||||
int Sensor::Subscribe(SensorEventHandler_t *handler, void* user_data)
|
||||
{
|
||||
this->evtHandler = handler;
|
||||
this->userData = user_data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Sensor::Publish(sensors_event_t* event)
|
||||
{
|
||||
if (this->evtHandler != NULL)
|
||||
{
|
||||
/* invoke subscribed handler */
|
||||
(*evtHandler)(this, event, this->userData);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sensor Manager
|
||||
*/
|
||||
/* sensor manager instance */
|
||||
static SensorManager _sensor_manager;
|
||||
|
||||
SensorManager::SensorManager()
|
||||
{
|
||||
sensorList = NULL;
|
||||
}
|
||||
|
||||
SensorManager::~SensorManager()
|
||||
{
|
||||
}
|
||||
|
||||
int SensorManager::RegisterSensor(Sensor* sensor)
|
||||
{
|
||||
SensorManager* self = &_sensor_manager;
|
||||
|
||||
RT_ASSERT(sensor != RT_NULL);
|
||||
|
||||
/* add sensor into the list */
|
||||
if (self->sensorList = NULL)
|
||||
{
|
||||
sensor->prev = sensor->next = sensor;
|
||||
}
|
||||
else
|
||||
{
|
||||
sensor->prev = self->sensorList;
|
||||
sensor->next = self->sensorList->next;
|
||||
|
||||
self->sensorList->next->prev = sensor;
|
||||
self->sensorList->next = sensor;
|
||||
}
|
||||
|
||||
/* point the sensorList to this sensor */
|
||||
self->sensorList = sensor;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SensorManager::DeregisterSensor(Sensor* sensor)
|
||||
{
|
||||
SensorManager* self = &_sensor_manager;
|
||||
|
||||
/* disconnect sensor list */
|
||||
sensor->next->prev = sensor->prev;
|
||||
sensor->prev->next = sensor->next;
|
||||
|
||||
/* check the sensorList */
|
||||
if (sensor == self->sensorList)
|
||||
{
|
||||
if (sensor->next == sensor) self->sensorList = NULL; /* empty list */
|
||||
else self->sensorList = sensor->next;
|
||||
}
|
||||
|
||||
/* re-initialize sensor node */
|
||||
sensor->next = sensor->prev = sensor;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Sensor *SensorManager::GetDefaultSensor(int type)
|
||||
{
|
||||
SensorManager* self = &_sensor_manager;
|
||||
Sensor *sensor = self->sensorList;
|
||||
|
||||
if (sensor == NULL) return NULL;
|
||||
|
||||
do
|
||||
{
|
||||
/* find the same type */
|
||||
if (sensor->GetType() == type) return sensor;
|
||||
|
||||
sensor = sensor->next;
|
||||
}
|
||||
while (sensor != self->sensorList);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void* user_data)
|
||||
{
|
||||
Sensor *sensor;
|
||||
|
||||
sensor = SensorManager::GetDefaultSensor(type);
|
||||
if (sensor != NULL)
|
||||
{
|
||||
sensor->Subscribe(handler, user_data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -50,6 +50,7 @@ FILEHANDLE _sys_open(const char *name, int openmode)
|
||||
{
|
||||
#ifdef RT_USING_DFS
|
||||
int fd;
|
||||
int mode = O_RDONLY;
|
||||
#endif
|
||||
|
||||
/* Register standard Input Output devices. */
|
||||
@@ -63,8 +64,33 @@ FILEHANDLE _sys_open(const char *name, int openmode)
|
||||
#ifndef RT_USING_DFS
|
||||
return -1;
|
||||
#else
|
||||
/* TODO: adjust open file mode */
|
||||
fd = open(name, openmode, 0);
|
||||
/* Correct openmode from fopen to open */
|
||||
if (openmode & OPEN_PLUS)
|
||||
{
|
||||
if (openmode & OPEN_W)
|
||||
{
|
||||
mode |= (O_RDWR | O_TRUNC | O_CREAT);
|
||||
}
|
||||
else if (openmode & OPEN_A)
|
||||
{
|
||||
mode |= (O_RDWR | O_APPEND | O_CREAT);
|
||||
}
|
||||
else
|
||||
mode |= O_RDWR;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (openmode & OPEN_W)
|
||||
{
|
||||
mode |= (O_WRONLY | O_TRUNC | O_CREAT);
|
||||
}
|
||||
else if (openmode & OPEN_A)
|
||||
{
|
||||
mode |= (O_WRONLY | O_APPEND | O_CREAT);
|
||||
}
|
||||
}
|
||||
|
||||
fd = open(name, mode, 0);
|
||||
if(fd < 0)
|
||||
return -1;
|
||||
else
|
||||
@@ -140,7 +166,6 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
|
||||
return 0;
|
||||
#else
|
||||
rt_device_t console_device;
|
||||
extern rt_device_t rt_console_get_device(void);
|
||||
|
||||
console_device = rt_console_get_device();
|
||||
if (console_device != 0) rt_device_write(console_device, 0, buf, len);
|
||||
@@ -227,7 +252,6 @@ int _sys_istty(FILEHANDLE fh)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int remove(const char *filename)
|
||||
{
|
||||
#ifndef RT_USING_DFS
|
||||
@@ -238,7 +262,7 @@ int remove(const char *filename)
|
||||
}
|
||||
|
||||
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(RT_USING_MODULE) && defined(RT_USING_DFS)
|
||||
/* use system implementation in the msh */
|
||||
/* use system(const char *string) implementation in the msh */
|
||||
#else
|
||||
int system(const char *string)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user