You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

395 rivejä
10 KiB

  1. #include "define.h"
  2. #include "AudioConsole.h"
  3. #include "timer.h"
  4. #include "ProtocolDefs.h"
  5. #include "NetworkProtocol.h"
  6. #include "VUMeter.h"
  7. #include "PushBtnMgr.h"
  8. #include "AudioConsoleLedMgr.h"
  9. AVReceiverStatus_t mMainZoneStatus, mZone2Status;
  10. bool mMasterInterfaceConnected;
  11. int mAudioConsoleState;
  12. int mAudioConsoleStatusRetryCount;
  13. int AudioConsoleInit()
  14. {
  15. ProtocolInit(NETWORK_PROTOCOL_USER_AUDIO_CONSOLE_IF);
  16. mMasterInterfaceConnected = false;
  17. mAudioConsoleState = AUDIO_CONSOLE_INIT_STATE;
  18. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
  19. mAudioConsoleStatusRetryCount = 0;
  20. mZone2Status.MainVolume = 0;
  21. mMainZoneStatus.MainVolume = 0;
  22. }
  23. int AudioConsoleSetReceiverStatus(AVReceiverStatus_t *MainZone, AVReceiverStatus_t *Zone2)
  24. {
  25. mAudioConsoleStatusRetryCount = 0;
  26. mMainZoneStatus.MainPwrStatus = MainZone ->MainPwrStatus;
  27. mMainZoneStatus.MainSleepStatus = MainZone->MainSleepStatus;
  28. mMainZoneStatus.IsMute = MainZone->IsMute;
  29. mMainZoneStatus.DataValid = MainZone->DataValid;
  30. mMainZoneStatus.ReceiverOnline = MainZone->ReceiverOnline;
  31. mMainZoneStatus.SyncZones = MainZone->SyncZones;
  32. mMainZoneStatus.MainVolume = MainZone->MainVolume;
  33. mZone2Status.MainPwrStatus = Zone2 ->MainPwrStatus;
  34. mZone2Status.MainSleepStatus = Zone2->MainSleepStatus;
  35. mZone2Status.IsMute = Zone2->IsMute;
  36. mZone2Status.DataValid = Zone2->DataValid;
  37. mZone2Status.ReceiverOnline = Zone2->ReceiverOnline;
  38. mZone2Status.SyncZones = Zone2->SyncZones;
  39. mZone2Status.MainVolume = Zone2->MainVolume;
  40. VUMeterSetVolume(mMainZoneStatus.MainVolume,mZone2Status.MainVolume);
  41. UpdatePushButtonLEDs(&mMainZoneStatus,&mZone2Status);
  42. return RET_OK;
  43. }
  44. int AudioConsoleSM(int Event)
  45. {
  46. switch(mAudioConsoleState)
  47. {
  48. case AUDIO_CONSOLE_INIT_STATE:
  49. {
  50. //At boot, we wait for wifi to connect first...
  51. if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
  52. {
  53. if(IsWiFiConnected() == true)
  54. {
  55. ConnectAudioInterface();
  56. mAudioConsoleState = AUDIO_CONSOLE_CONNECTING_STATE;
  57. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_CONNECT_TIMEOUT);
  58. }
  59. else
  60. {
  61. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
  62. }
  63. }
  64. break;
  65. }
  66. case AUDIO_CONSOLE_DISCONNECTED_STATE:
  67. {
  68. switch(Event)
  69. {
  70. case AUDIO_CONSOLE_SM_TICK_EVENT:
  71. {
  72. if(IsWiFiConnected() == true)
  73. {
  74. if(ConnectAudioInterface() == RET_ERROR)
  75. {
  76. //Socket don't work anymore... not shure what to do.
  77. int toto = 1;
  78. toto ++;
  79. }
  80. mAudioConsoleState = AUDIO_CONSOLE_CONNECTING_STATE;
  81. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_CONNECT_TIMEOUT);
  82. }
  83. else
  84. {
  85. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_WAIT_FOR_WIFI_TIMEOUT);
  86. }
  87. break;
  88. }
  89. case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
  90. {
  91. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
  92. RequestStatus();
  93. mAudioConsoleState = AUDIO_CONSOLE_RUN_STATE;
  94. break;
  95. }
  96. case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
  97. {
  98. break;
  99. }
  100. }
  101. break;
  102. }
  103. case AUDIO_CONSOLE_CONNECTING_STATE:
  104. {
  105. switch(Event)
  106. {
  107. case AUDIO_CONSOLE_SM_TICK_EVENT:
  108. {
  109. if(IsWiFiConnected() == false)
  110. {
  111. AudioConsoleDisconnected();
  112. }
  113. else if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
  114. {
  115. AudioConsoleDisconnected(); //Retry to connect...
  116. }
  117. break;
  118. }
  119. case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
  120. {
  121. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
  122. RequestStatus();
  123. mAudioConsoleState = AUDIO_CONSOLE_RUN_STATE;
  124. LedMgrShowOfflineMode(false);
  125. break;
  126. }
  127. case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
  128. {
  129. AudioConsoleDisconnected();
  130. break;
  131. }
  132. }
  133. break;
  134. }
  135. case AUDIO_CONSOLE_RUN_STATE:
  136. {
  137. switch(Event)
  138. {
  139. case AUDIO_CONSOLE_SM_TICK_EVENT:
  140. {
  141. if(IsAudioInterfaceConnected() == false || IsWiFiConnected() == false)
  142. {
  143. AudioConsoleDisconnected();
  144. }
  145. else if(IsTimerExpired(CONSOLE_IF_CONNECT_TIMER))
  146. {
  147. if(RequestStatus() == RET_OK)
  148. {
  149. TimerStart(CONSOLE_IF_CONNECT_TIMER,AUDIO_CONSOLE_STATUS_REQUEST_TIMEOUT);
  150. }
  151. else
  152. {
  153. AudioConsoleDisconnected();
  154. }
  155. }
  156. break;
  157. }
  158. case AUDIO_CONSOLE_SM_CONNECTED_EVENT:
  159. {
  160. //Should not happen....
  161. break;
  162. }
  163. case AUDIO_CONSOLE_SM_DISCONNECTED_EVENT:
  164. {
  165. AudioConsoleDisconnected();
  166. break;
  167. }
  168. }
  169. break;
  170. }
  171. }
  172. }
  173. void AudioConsoleTick()
  174. {
  175. AudioConsoleSM(AUDIO_CONSOLE_SM_TICK_EVENT);
  176. }
  177. int AudioConsoleSetIFConnectionStatus(bool Connected)
  178. {
  179. if(Connected == true)
  180. {
  181. ONBOARD_LED1_PIN = LED_ON;
  182. AudioConsoleSM(AUDIO_CONSOLE_SM_CONNECTED_EVENT);
  183. }
  184. else
  185. {
  186. ONBOARD_LED1_PIN = LED_OFF;
  187. AudioConsoleSM(AUDIO_CONSOLE_SM_DISCONNECTED_EVENT);
  188. }
  189. }
  190. int RequestStatus()
  191. {
  192. if(mAudioConsoleStatusRetryCount >= AUDIO_CONSOLE_MAX_STATUS_RETRY)
  193. {
  194. return RET_ERROR;
  195. }
  196. mAudioConsoleStatusRetryCount++;
  197. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_GENERAL_STATUS_REQUEST,0,0);
  198. return RET_OK;
  199. }
  200. int SendMainVolumeToMaster()
  201. {
  202. char Volume = mMainZoneStatus.MainVolume;
  203. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_MAIN_VOLUME_REQUEST,&Volume,1);
  204. return RET_OK;
  205. }
  206. int SendZone2VolumeToMaster()
  207. {
  208. char Volume = mZone2Status.MainVolume;
  209. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_VOLUME_REQUEST,&Volume,1);
  210. return RET_OK;
  211. }
  212. int SendIncrementMainVolume(bool Increment)
  213. {
  214. char data;
  215. if(Increment == true)
  216. {
  217. data = 1;
  218. }
  219. else
  220. {
  221. data = 0;
  222. }
  223. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_INCREMENT_MAIN_VOLUME_REQUEST,&data,1);
  224. return RET_OK;
  225. }
  226. int SendIncrementZ2Volume(bool Increment)
  227. {
  228. char data;
  229. if(Increment == true)
  230. {
  231. data = 1;
  232. }
  233. else
  234. {
  235. data = 0;
  236. }
  237. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_INCREMENT_Z2_VOLUME_REQUEST,&data,1);
  238. return RET_OK;
  239. }
  240. int AudioConsoleDisconnected()
  241. {
  242. //TODO reset console display
  243. ONBOARD_LED1_PIN = LED_OFF;
  244. mAudioConsoleStatusRetryCount = 0;
  245. DisconnectAudioInterface();
  246. mAudioConsoleState = AUDIO_CONSOLE_DISCONNECTED_STATE;
  247. TimerStop(CONSOLE_IF_CONNECT_TIMER);
  248. LedMgrShowOfflineMode(true);
  249. }
  250. int AudioConsoleVolumeUp(int Zone)
  251. {
  252. switch(Zone)
  253. {
  254. case AUDIO_CONSOLE_SALON_ZONE:
  255. {
  256. SendIncrementMainVolume(true);
  257. break;
  258. }
  259. case AUDIO_CONSOLE_CUISINE_ZONE:
  260. {
  261. SendIncrementZ2Volume(true);
  262. break;
  263. }
  264. }
  265. RequestStatus();
  266. return RET_OK;
  267. }
  268. int AudioConsoleVolumeDown(int Zone)
  269. {
  270. switch(Zone)
  271. {
  272. case AUDIO_CONSOLE_SALON_ZONE:
  273. {
  274. SendIncrementMainVolume(false);
  275. break;
  276. }
  277. case AUDIO_CONSOLE_CUISINE_ZONE:
  278. {
  279. SendIncrementZ2Volume(false);
  280. break;
  281. }
  282. }
  283. RequestStatus();
  284. return RET_OK;
  285. }
  286. int LinkZ2BtnPressed()
  287. {
  288. if(mMainZoneStatus.SyncZones == true)
  289. {
  290. SendLinkZ2VolumeToMaster(false);
  291. }
  292. else
  293. {
  294. SendLinkZ2VolumeToMaster(true);
  295. }
  296. RequestStatus();
  297. return RET_OK;
  298. }
  299. int MainZonePwrBtnPressed()
  300. {
  301. if(mMainZoneStatus.MainPwrStatus == true)
  302. {
  303. SendMainZonePwrToMaster(false);
  304. }
  305. else
  306. {
  307. SendMainZonePwrToMaster(true);
  308. }
  309. RequestStatus();
  310. return RET_OK;
  311. }
  312. int Zone2PwrBtnPressed()
  313. {
  314. if(mZone2Status.MainPwrStatus == true)
  315. {
  316. SendZone2PwrToMaster(false);
  317. }
  318. else
  319. {
  320. SendZone2PwrToMaster(true);
  321. }
  322. RequestStatus();
  323. return RET_OK;
  324. }
  325. int SendLinkZ2VolumeToMaster(bool link)
  326. {
  327. char data;
  328. if(link == true)
  329. {
  330. data = 1;
  331. }
  332. else
  333. {
  334. data = 0;
  335. }
  336. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_SYNC_Z2_WITH_Z1_REQUEST,&data,1);
  337. return RET_OK;
  338. }
  339. int SendMainZonePwrToMaster(bool ON)
  340. {
  341. char data;
  342. if(ON == true)
  343. {
  344. data = 1;
  345. }
  346. else
  347. {
  348. data = 0;
  349. }
  350. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_MAIN_POWER_REQUEST,&data,1);
  351. return RET_OK;
  352. }
  353. int SendZone2PwrToMaster(bool ON)
  354. {
  355. char data;
  356. if(ON == true)
  357. {
  358. data = 1;
  359. }
  360. else
  361. {
  362. data = 0;
  363. }
  364. AudioConsoleSendCommand(AV_RECEIVER_INTERFACE_SET_ZONE2_REQUEST,&data,1);
  365. return RET_OK;
  366. }