ccalibration.hxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /////////////////////////////////////////////////////////////////////////////
  2. // apps/include/graphics/twm4nx/apps/ccalibration.hxx
  3. //
  4. // Licensed to the Apache Software Foundation (ASF) under one or more
  5. // contributor license agreements. See the NOTICE file distributed with
  6. // this work for additional information regarding copyright ownership. The
  7. // ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. // "License"); you may not use this file except in compliance with the
  9. // License. You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations
  17. // under the License.
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #ifndef __APPS_INCLUDE_GRAPHICS_TWM4NX_APPS_CCALIBRATION_HXX
  21. #define __APPS_INCLUDE_GRAPHICS_TWM4NX_APPS_CCALIBRATION_HXX
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Included Files
  24. /////////////////////////////////////////////////////////////////////////////
  25. #include <nuttx/nx/nxglib.h>
  26. #include <pthread.h>
  27. #include <semaphore.h>
  28. #include <fixedmath.h>
  29. #include "graphics/nxwidgets/cnxstring.hxx"
  30. #include "graphics/nxwidgets/cwidgeteventhandler.hxx"
  31. #include "graphics/nxwidgets/cwidgetcontrol.hxx"
  32. #include "graphics/nxwidgets/clabel.hxx"
  33. #include "graphics/nxwidgets/cnxfont.hxx"
  34. #include "graphics/twm4nx/ctwm4nx.hxx"
  35. #include "graphics/twm4nx/ctwm4nxevent.hxx"
  36. #include "graphics/twm4nx/twm4nx_events.hxx"
  37. #include "graphics/twm4nx/iapplication.hxx"
  38. #include "graphics/twm4nx/cinput.hxx"
  39. /////////////////////////////////////////////////////////////////////////////
  40. // Pre-processor Definitions
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CNxTerm application events
  43. // Window Events
  44. #define EVENT_CALIB_REDRAW EVENT_SYSTEM_NOP
  45. #define EVENT_CALIB_RESIZE EVENT_SYSTEM_NOP
  46. #define EVENT_CALIB_XYINPUT (EVENT_RECIPIENT_APP | 0x0000)
  47. #define EVENT_CALIB_KBDINPUT EVENT_SYSTEM_NOP
  48. #define EVENT_CALIB_DELETE (EVENT_RECIPIENT_APP | 0x0001)
  49. // Button events (there are no buttons)
  50. #define EVENT_CALIB_CLOSE EVENT_SYSTEM_NOP
  51. // Menu Events
  52. #define EVENT_CALIB_START (EVENT_RECIPIENT_APP | 0x0002)
  53. // Calibration indices
  54. #define CALIB_UPPER_LEFT_INDEX 0
  55. #define CALIB_UPPER_RIGHT_INDEX 1
  56. #define CALIB_LOWER_RIGHT_INDEX 2
  57. #define CALIB_LOWER_LEFT_INDEX 3
  58. #define CALIB_DATA_POINTS 4
  59. /////////////////////////////////////////////////////////////////////////////
  60. // Implementation Classes
  61. /////////////////////////////////////////////////////////////////////////////
  62. namespace Twm4Nx
  63. {
  64. class CTwm4Nx; // Forward reference
  65. /**
  66. * The CCalibration class provides the the calibration window and obtains
  67. * calibration data.
  68. */
  69. class CCalibration : public CTwm4NxEvent
  70. {
  71. private:
  72. // The state of the calibration thread.
  73. enum ECalThreadState
  74. {
  75. CALTHREAD_NOTRUNNING = 0, /**< The calibration thread has not yet been started */
  76. CALTHREAD_STARTED, /**< The calibration thread has been started, but is not yet running */
  77. CALTHREAD_RUNNING, /**< The calibration thread is running normally */
  78. CALTHREAD_STOPREQUESTED, /**< The calibration thread has been requested to stop */
  79. CALTHREAD_TERMINATED /**< The calibration thread terminated normally */
  80. };
  81. // Identifies the current display state
  82. enum ECalibrationPhase
  83. {
  84. CALPHASE_NOT_STARTED = 0, /**< Constructed, but not yet started */
  85. CALPHASE_UPPER_LEFT, /**< Touch point is in the upper left corner */
  86. CALPHASE_UPPER_RIGHT, /**< Touch point is in the upper right corner */
  87. CALPHASE_LOWER_RIGHT, /**< Touch point is in the lower left corner */
  88. CALPHASE_LOWER_LEFT, /**< Touch point is in the lower right corner */
  89. CALPHASE_COMPLETE /**< Calibration is complete */
  90. };
  91. // Describes one touchscreen sample. CInput treats mouse and touchscreen
  92. // input the same so this reflects the mousey nature of the object.
  93. struct STouchSample
  94. {
  95. bool valid; /**< True: Sample is valid */
  96. uint8_t buttons; /**< Left button reflects touch state */
  97. struct nxgl_point_s pos; /**< True: Touch position */
  98. };
  99. // Characterizes one calibration screen
  100. struct SCalibScreenInfo
  101. {
  102. struct nxgl_point_s pos; /**< The position of the touch point */
  103. nxgl_mxpixel_t lineColor; /**< The color of the cross-hair lines */
  104. nxgl_mxpixel_t circleFillColor; /**< The color of the circle */
  105. };
  106. // CCalibration state data
  107. FAR CTwm4Nx *m_twm4nx; /**< Twm4Nx session instance */
  108. FAR NXWidgets::CNxWindow *m_nxWin; /**< The window for the calibration display */
  109. #ifdef CONFIG_TWM4NX_CALIBRATION_MESSAGES
  110. FAR NXWidgets::CLabel *m_text; /**< Calibration message */
  111. FAR NXWidgets::CNxFont *m_font; /**< The font used in the message */
  112. #endif
  113. pthread_t m_thread; /**< The calibration thread ID */
  114. sem_t m_synchSem; /**< Synchronize calibration thread with events */
  115. sem_t m_exclSem; /**< For mutually exclusive access to data */
  116. struct SCalibScreenInfo m_screenInfo; /**< Describes the current calibration display */
  117. struct nxgl_point_s m_touchPos; /**< This is the last touch position */
  118. volatile uint8_t m_calthread; /**< Current calibration display state (See ECalibThreadState)*/
  119. uint8_t m_calphase; /**< Current calibration display state (See ECalibrationPhase)*/
  120. bool m_stop; /**< True: We have been asked to stop the calibration */
  121. bool m_touched; /**< True: The screen is touched */
  122. struct STouchSample m_sample; /**< Catches new touch samples */
  123. #ifdef CONFIG_TWM4NX_CALIBRATION_AVERAGE
  124. uint8_t m_nsamples; /**< Number of samples collected so far at this position */
  125. struct nxgl_point_s m_sampleData[CONFIG_TWM4NX_CALIBRATION_NSAMPLES];
  126. #endif
  127. struct nxgl_point_s m_calibData[CALIB_DATA_POINTS];
  128. /**
  129. * Accept raw touchscreen input.
  130. *
  131. * @param sample Touchscreen input sample
  132. */
  133. void touchscreenInput(struct STouchSample &sample);
  134. #ifdef CONFIG_TWM4NX_CALIBRATION_MESSAGES
  135. /**
  136. * Create widgets need by the calibration thread.
  137. *
  138. * @return True if the widgets were successfully created.
  139. */
  140. bool createWidgets(void);
  141. /**
  142. * Destroy widgets created for the calibration thread.
  143. */
  144. void destroyWidgets(void);
  145. #endif
  146. /**
  147. * The calibration thread. This is the entry point of a thread that provides the
  148. * calibration displays, waits for input, and collects calibration data.
  149. *
  150. * @param arg. The CCalibration 'this' pointer cast to a void*.
  151. * @return This function always returns NULL when the thread exits
  152. */
  153. static FAR void *calibration(FAR void *arg);
  154. /**
  155. * Get exclusive access data shared across threads.
  156. *
  157. * @return True is returned if the sample data was obtained without error.
  158. */
  159. bool exclusiveAccess(void);
  160. /**
  161. * Wait for touchscreen next sample data to become available.
  162. *
  163. * @param sample Snapshot of last touchscreen sample
  164. * @return True is returned if the sample data was obtained without error.
  165. */
  166. bool waitTouchSample(FAR struct STouchSample &sample);
  167. #ifdef CONFIG_TWM4NX_CALIBRATION_AVERAGE
  168. /**
  169. * Accumulate and average touch sample data
  170. *
  171. * @param average. When the averaged data is available, return it here
  172. * @return True: Average data is available; False: Need to collect more samples
  173. */
  174. bool averageSamples(struct nxgl_point_s &average);
  175. #endif
  176. /**
  177. * This is the calibration state machine. It is called initially and then
  178. * as new touchscreen data is received.
  179. */
  180. void stateMachine(void);
  181. /**
  182. * Presents the next calibration screen
  183. */
  184. void showCalibration(void);
  185. /**
  186. * Finish calibration steps and provide the calibration data to the
  187. * touchscreen driver.
  188. */
  189. void finishCalibration(void);
  190. /**
  191. * Stop the calibration thread.
  192. */
  193. void stop(void);
  194. /**
  195. * Destroy the application and free all of its resources. This method
  196. * will initiate blocking of messages from the NX server. The server
  197. * will flush the window message queue and reply with the blocked
  198. * message. When the block message is received by CWindowMessenger,
  199. * it will send the destroy message to the start window task which
  200. * will, finally, safely delete the application.
  201. */
  202. void destroy(void);
  203. /**
  204. * Given the raw touch data collected by the calibration thread, create the
  205. * massaged calibration data needed by CTouchscreen.
  206. *
  207. * @param data. A reference to the location to save the calibration data
  208. * @return True if the calibration data was successfully created.
  209. */
  210. bool createCalibrationData(struct SCalibrationData &data);
  211. public:
  212. /**
  213. * CCalibration Constructor
  214. *
  215. * @param twm4nx. The Twm4Nx session instance.
  216. */
  217. CCalibration(FAR CTwm4Nx *twm4nx);
  218. /**
  219. * CCalibration Destructor
  220. */
  221. ~CCalibration(void);
  222. /**
  223. * CCalibration Initializer. Performs parts of the instance
  224. * construction that may fail. This function creates the
  225. * initial calibration display.
  226. */
  227. bool initialize(void);
  228. /**
  229. * Start the application (perhaps in the minimized state).
  230. *
  231. * @return True if the application was successfully started.
  232. */
  233. bool run(void);
  234. /**
  235. * Handle CCalibration events. This overrides a method from
  236. * CTwm4NXEvent.
  237. *
  238. * @param eventmsg. The received NxWidget WINDOW event message.
  239. * @return True if the message was properly handled. false is
  240. * return on any failure.
  241. */
  242. bool event(FAR struct SEventMsg *eventmsg);
  243. };
  244. class CCalibrationFactory : public IApplication,
  245. public IApplicationFactory,
  246. public CTwm4NxEvent
  247. {
  248. private:
  249. FAR CTwm4Nx *m_twm4nx; /**< Twm4Nx session instance */
  250. /**
  251. * Handle CCalibrationFactory events. This overrides a method from
  252. * CTwm4NXEvent
  253. *
  254. * @param eventmsg. The received NxWidget WINDOW event message.
  255. * @return True if the message was properly handled. false is
  256. * return on any failure.
  257. */
  258. bool event(FAR struct SEventMsg *eventmsg);
  259. /**
  260. * Create and start a new instance of an CNxTerm.
  261. */
  262. bool startFunction(void);
  263. /**
  264. * Return the Main Menu item string. This overrides the method from
  265. * IApplication
  266. *
  267. * @param name The name of the application.
  268. */
  269. inline NXWidgets::CNxString getName(void)
  270. {
  271. return NXWidgets::CNxString("Calibration");
  272. }
  273. /**
  274. * There is no sub-menu for this Main Menu item. This overrides
  275. * the method from IApplication.
  276. *
  277. * @return This implementation will always return a null value.
  278. */
  279. inline FAR CMenus *getSubMenu(void)
  280. {
  281. return (FAR CMenus *)0;
  282. }
  283. /**
  284. * There is no custom event handler. We use the common event handler.
  285. *
  286. * @return. null is always returned in this implementation.
  287. */
  288. inline FAR CTwm4NxEvent *getEventHandler(void)
  289. {
  290. return (FAR CTwm4NxEvent *)this;
  291. }
  292. /**
  293. * Return the Twm4Nx event that will be generated when the Main Menu
  294. * item is selected.
  295. *
  296. * @return. This function always returns EVENT_SYSTEM_NOP.
  297. */
  298. inline uint16_t getEvent(void)
  299. {
  300. return EVENT_CALIB_START;
  301. }
  302. public:
  303. /**
  304. * CCalibrationFactory Constructor
  305. */
  306. inline CCalibrationFactory(void)
  307. {
  308. m_twm4nx = (FAR CTwm4Nx *)0;
  309. }
  310. /**
  311. * CCalibrationFactory Destructor
  312. */
  313. inline ~CCalibrationFactory(void)
  314. {
  315. // REVISIT: Would need to remove Main Menu item
  316. }
  317. /**
  318. * CCalibrationFactory Initializer. Performs parts of the instance
  319. * construction that may fail. In this implementation, it registers
  320. * a menu item in the Main Menu and, optionally, bring up a
  321. * CCalibration instance now in order calibrate unconditionally on
  322. * start-up
  323. *
  324. * @param twm4nx The Twm4Nx session instance
  325. * @return True if successfully initialized
  326. */
  327. bool initialize(FAR CTwm4Nx *twm4nx);
  328. };
  329. }
  330. #endif // __APPS_INCLUDE_GRAPHICS_TWM4NX_APPS_CCALIBRATION_HXX