version 2.8
gdriver.h
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 /**
9  * @file src/gdriver/gdriver.h
10  *
11  * @addtogroup GDRIVER
12  *
13  * @brief Module to support registering and unregistering of drivers
14  *
15  * @details GDRIVER provides a generalized way of defining and registering drivers.
16  *
17  * @note There are many different types of drivers and GDRIVER can handle any
18  * type of driver defined by the uGFX system.
19  *
20  * @note GDRIVER supports multiple drivers for one type of device. eg a SSD1289 LCD
21  * driver simultaneously with a framebuffer driver.
22  * @note GDRIVER supports multiple instances of a single driver. eg 2 SSD1289 LCD's.
23  * @note If there is only a single device of a particular type it will automatically
24  * register that device (it only needs to be included in the build, no special
25  * configuration is required)
26  * @note This module gdriver.h file is NOT included in the general gfx.h file.
27  * Instead it is included in each driver type's driver API.
28  *
29  * @pre GFX_USE_GDRIVER must be set to TRUE in your gfxconf.h
30  *
31  * @{
32  */
33 
34 #ifndef _GDRIVER_H
35 #define _GDRIVER_H
36 
37 #if GFX_USE_GDRIVER || defined(__DOXYGEN__)
38 
39 /*===========================================================================*/
40 /* Type definitions */
41 /*===========================================================================*/
42 
43 #define GDRIVER_TYPE_DISPLAY 'g' // @< A graphics display
44 #define GDRIVER_TYPE_MOUSE 'm' // @< A mouse
45 #define GDRIVER_TYPE_TOUCH 'm' // @< A touch display (equivalent to a mouse)
46 #define GDRIVER_TYPE_TOGGLE 't' // @< A toggle device eg GPIO pins, switches etc
47 #define GDRIVER_TYPE_DIAL 'd' // @< A analog or digit dial (ranges in value from a minimum to a maximum)
48 #define GDRIVER_TYPE_KEYBOARD 'k' // @< A keyboard
49 #define GDRIVER_TYPE_BLOCK 'b' // @< A block device
50 #define GDRIVER_TYPE_STRING 's' // @< A device that returns strings of data
51 
52 /**
53  * @brief All runtime driver structures start with this structure
54  *
55  * @note This structure (and any additional structure memory) is allocated
56  * dynamically by the system for each driver instance.
57  */
58 typedef struct GDriver {
59  struct GDriver * driverchain;
60  const struct GDriverVMT * vmt;
61 } GDriver;
62 
63 /**
64  * @brief All driver VMT's start with this structure.
65  */
66 typedef struct GDriverVMT {
67  uint16_t type; // @< What type of driver this is
68  uint16_t flags; // @< Flags for the driver. Meaning is specific to each driver type.
69  uint32_t objsize; // @< How big the runtime driver structure is
70  bool_t (*init)(GDriver *driver, void *param, unsigned driverinstance, unsigned systeminstance); // @< Initialise the driver. Returns TRUE if OK.
71  // driverinstance is the instance 0..n of this driver.
72  // systeminstance is the instance 0..n of this type of device.
73  // The memory allocated is cleared before this call.
74  void (*postinit)(GDriver *driver); // @< Called once the driver is registered.
75  void (*deinit)(GDriver *driver); // @< De-initialise the driver
76 } GDriverVMT;
77 
78 /**
79  * @brief A definition that allows getting addresses of GDriverVMT structures to put into a list.
80  * @note eg. <code>
81  * const MyDriverVMTtype a[1] = {{...}};
82  * const MyDriverVMTtype b[1] = {{...}};
83  * ...
84  * \#define DRIVER_LIST a, b
85  * extern GDriverVMTList DRIVER_LIST; // Now treated as single element arrays of GDriverVMT
86  * const GDriverVMT const * mylist = { DRIVER_LIST };
87  * </code>
88  *
89  */
90 typedef const struct GDriverVMT const GDriverVMTList[1];
91 
92 /*===========================================================================*/
93 /* External declarations. */
94 /*===========================================================================*/
95 
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
100  /**
101  * @brief Register a new driver instance.
102  * @return The runtime driver structure or NULL if it fails.
103  *
104  * @param[in] vmt The driver's vmt
105  * @param[in] param An arbitrary paramater passed to the driver init routine.
106  */
107  GDriver *gdriverRegister(const GDriverVMT *vmt, void *param);
108 
109  /**
110  * @brief UnRegister a driver instance.
111  *
112  * @param[in] driver The driver instance's runtime structure
113  */
114  void gdriverUnRegister(GDriver *driver);
115 
116  /**
117  * @brief Get the driver for a particular instance of a type of device
118  * @return The runtime driver structure or NULL if it fails.
119  *
120  * @param[in] type The type of driver to find
121  * @param[in] instance The instance (0..n) to find
122  */
123  GDriver *gdriverGetInstance(uint16_t type, unsigned instance);
124 
125  /**
126  * @brief Get the count of instances of a type of device
127  * @return The instance count.
128  *
129  * @note Valid instance numbers are then 0 .. count-1
130  *
131  * @param[in] type The type of driver to find
132  */
133  unsigned gdriverInstanceCount(uint16_t type);
134 
135  /**
136  * @brief Get the instance number for a device
137  * @return The instance number or (unsigned)-1 if it fails.
138  *
139  * @param[in] driver The driver to find the instance number for
140  */
141  unsigned gdriverGetDriverInstanceNumber(GDriver *driver);
142 
143  /**
144  * @brief Get the next driver for a type of device
145  * @return The runtime driver structure or NULL if there are no more.
146  *
147  * @param[in] type The type of driver to find
148  * @param[in] driver The last driver returned or NULL to start again
149  */
150  GDriver *gdriverGetNext(uint16_t type, GDriver *driver);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* GFX_USE_GDRIVER */
157 
158 #endif /* _GDRIVER_H */
159 /** @} */
void gdriverUnRegister(GDriver *driver)
UnRegister a driver instance.
Definition: gdriver.c:69
const struct GDriverVMT const GDriverVMTList[1]
A definition that allows getting addresses of GDriverVMT structures to put into a list...
Definition: gdriver.h:90
GDriver * gdriverRegister(const GDriverVMT *vmt, void *param)
Register a new driver instance.
Definition: gdriver.c:31
All runtime driver structures start with this structure.
Definition: gdriver.h:58
unsigned gdriverGetDriverInstanceNumber(GDriver *driver)
Get the instance number for a device.
Definition: gdriver.c:136
unsigned gdriverInstanceCount(uint16_t type)
Get the count of instances of a type of device.
Definition: gdriver.c:114
GDriver * gdriverGetNext(uint16_t type, GDriver *driver)
Get the next driver for a type of device.
Definition: gdriver.c:127
struct GDriver GDriver
All runtime driver structures start with this structure.
struct GDriverVMT GDriverVMT
All driver VMT&#39;s start with this structure.
All driver VMT&#39;s start with this structure.
Definition: gdriver.h:66
GDriver * gdriverGetInstance(uint16_t type, unsigned instance)
Get the driver for a particular instance of a type of device.
Definition: gdriver.c:98