Interface abstraction
Posted: Mon Jun 11, 2012 8:48 pm
It would be great if the library can work with multiple interfaces (FSMC, GPIO, SPI etc). This would really open up the options for how each display can be used.
My thinking was that we could do something similar to Chibios' BaseSequentialStream style abstraction. A pointer to each interface could then be cast to one generic type, e.g LCDInterfaceDriver*.
The abstraction would be very simple, and would just contain pointers to functions that do reading / writing over the appropriate peripheral.
Here's how BaseSequentialStream works in Chibios:
And then a driver has support of this abstract 'virtual class':
Basically we just define a set of function pointers that are at the start of every derived LCDInterfaceDriver struct.
I think the operations we need to support are very basic:
There should be no need to abstract the initialisation method.
Does anyone have any thoughts or suggestions?
My thinking was that we could do something similar to Chibios' BaseSequentialStream style abstraction. A pointer to each interface could then be cast to one generic type, e.g LCDInterfaceDriver*.
The abstraction would be very simple, and would just contain pointers to functions that do reading / writing over the appropriate peripheral.
Here's how BaseSequentialStream works in Chibios:
Code: Select all
/**
* @brief BaseSequentialStream specific methods.
*/
#define _base_sequential_stream_methods \
/* Stream write buffer method.*/ \
size_t (*write)(void *instance, const uint8_t *bp, size_t n); \
/* Stream read buffer method.*/ \
size_t (*read)(void *instance, uint8_t *bp, size_t n);
/**
* @brief @p BaseSequentialStream specific data.
* @note It is empty because @p BaseSequentialStream is only an interface
* without implementation.
*/
#define _base_sequential_stream_data
/**
* @brief @p BaseSequentialStream virtual methods table.
*/
struct BaseSequentialStreamVMT {
_base_sequential_stream_methods
};
/**
* @brief Base stream class.
* @details This class represents a generic blocking unbuffered sequential
* data stream.
*/
typedef struct {
/** @brief Virtual Methods Table.*/
const struct BaseSequentialStreamVMT *vmt;
_base_sequential_stream_data
} BaseSequentialStream;And then a driver has support of this abstract 'virtual class':
Code: Select all
/**
* @brief @p SerialDriver specific methods.
*/
#define _serial_driver_methods \
_base_asynchronous_channel_methods
/**
* @extends BaseAsynchronousChannelVMT
*
* @brief @p SerialDriver virtual methods table.
*/
struct SerialDriverVMT {
_serial_driver_methods
};
/**
* @extends BaseAsynchronousChannel
*
* @brief Full duplex serial driver class.
* @details This class extends @p BaseAsynchronousChannel by adding physical
* I/O queues.
*/
struct SerialDriver {
/** @brief Virtual Methods Table.*/
const struct SerialDriverVMT *vmt;
_serial_driver_data
};Basically we just define a set of function pointers that are at the start of every derived LCDInterfaceDriver struct.
I think the operations we need to support are very basic:
- read(address, length)
- write(address, data, length)
There should be no need to abstract the initialisation method.
Does anyone have any thoughts or suggestions?