-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzcl_samplelight.c
2213 lines (1944 loc) · 68.4 KB
/
zcl_samplelight.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************************************
Filename: zcl_sampleLight.c
Revised: $Date: 2014-07-01 22:24:24 -0700 (Tue, 01 Jul 2014) $
Revision: $Revision: 39317 $
Description: Zigbee Cluster Library - sample device application.
Copyright 2006-2014 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/*********************************************************************
This application implements a ZigBee HA 1.2 Light. It can be configured as an
On/Off light, or as a dimmable light. The following flags must be defined in
the compiler's pre-defined symbols.
ZCL_ON_OFF
ZCL_LEVEL_CTRL (only if dimming functionality desired)
HOLD_AUTO_START
ZCL_EZMODE
This device supports all mandatory and optional commands/attributes for the
OnOff (0x0006) and LevelControl (0x0008) clusters.
SCREEN MODES
----------------------------------------
Main:
- SW1: Toggle local light
- SW2: Invoke EZMode
- SW4: Enable/Disable local permit join
- SW5: Go to Help screen
----------------------------------------
*********************************************************************/
/*********************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "MT_SYS.h"
#include "zcl.h"
#include "zcl_general.h"
#include "zcl_ha.h"
#include "zcl_ezmode.h"
#include "zcl_diagnostic.h"
#include "zcl_samplelight.h"
#include "onboard.h"
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#if ( defined (ZGP_DEVICE_TARGET) || defined (ZGP_DEVICE_TARGETPLUS) \
|| defined (ZGP_DEVICE_COMBO) || defined (ZGP_DEVICE_COMBO_MIN) )
#include "zgp_translationtable.h"
#if (SUPPORTED_S_FEATURE(SUPP_ZGP_FEATURE_TRANSLATION_TABLE))
#define ZGP_AUTO_TT
#endif
#endif
#if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
#include "math.h"
#include "hal_timer.h"
#endif
#include "NLMEDE.h"
/*********************************************************************
* MACROS
*/
#define zcl_AccessCtrlRead( a ) ( (a) & ACCESS_CONTROL_READ )
/*********************************************************************
* CONSTANTS
*/
#if (defined HAL_BOARD_ZLIGHT)
#define LEVEL_MAX 0xFE
#define LEVEL_MIN 0x0
#define GAMMA_VALUE 2
#define PWM_FULL_DUTY_CYCLE 1000
#elif (defined HAL_PWM)
#define LEVEL_MAX 0xFE
#define LEVEL_MIN 0x0
#define GAMMA_VALUE 2
#define PWM_FULL_DUTY_CYCLE 100
#endif
/*********************************************************************
* TYPEDEFS
*/
/*********************************************************************
* GLOBAL VARIABLES
*/
byte zclSampleLight_TaskID;
uint8 zclSampleLightSeqNum;
/*********************************************************************
* GLOBAL FUNCTIONS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
afAddrType_t zclSampleLight_DstAddr;
//-- MOD START
uint16 gTimeCounter = 0;
//-- MOD END
#ifdef ZCL_EZMODE
static void zclSampleLight_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
static void zclSampleLight_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
// register EZ-Mode with task information (timeout events, callback, etc...)
static const zclEZMode_RegisterData_t zclSampleLight_RegisterEZModeData =
{
&zclSampleLight_TaskID,
SAMPLELIGHT_EZMODE_NEXTSTATE_EVT,
SAMPLELIGHT_EZMODE_TIMEOUT_EVT,
&zclSampleLightSeqNum,
zclSampleLight_EZModeCB
};
#else
uint16 bindingInClusters[] =
{
ZCL_CLUSTER_ID_GEN_ON_OFF
#ifdef ZCL_LEVEL_CTRL
, ZCL_CLUSTER_ID_GEN_LEVEL_CONTROL
#endif
};
#define ZCLSAMPLELIGHT_BINDINGLIST (sizeof(bindingInClusters) / sizeof(bindingInClusters[0]))
#endif // ZCL_EZMODE
// Test Endpoint to allow SYS_APP_MSGs
static endPointDesc_t sampleLight_TestEp =
{
SAMPLELIGHT_ENDPOINT,
&zclSampleLight_TaskID,
(SimpleDescriptionFormat_t *)NULL, // No Simple description for this test endpoint
(afNetworkLatencyReq_t)0 // No Network Latency req
};
uint8 giLightScreenMode = LIGHT_MAINMODE; // display the main screen mode first
uint8 gPermitDuration = 0; // permit joining default to disabled
devStates_t zclSampleLight_NwkState = DEV_INIT;
#if ZCL_LEVEL_CTRL
uint8 zclSampleLight_WithOnOff; // set to TRUE if state machine should set light on/off
uint8 zclSampleLight_NewLevel; // new level when done moving
bool zclSampleLight_NewLevelUp; // is direction to new level up or down?
int32 zclSampleLight_CurrentLevel32; // current level, fixed point (e.g. 192.456)
int32 zclSampleLight_Rate32; // rate in units, fixed point (e.g. 16.123)
uint8 zclSampleLight_LevelLastLevel; // to save the Current Level before the light was turned OFF
#endif
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void zclSampleLight_HandleKeys( byte shift, byte keys );
static void zclSampleLight_BasicResetCB( void );
static void zclSampleLight_IdentifyCB( zclIdentify_t *pCmd );
static void zclSampleLight_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
static void zclSampleLight_OnOffCB( uint8 cmd );
static void zclSampleLight_ProcessIdentifyTimeChange( void );
#ifdef ZCL_LEVEL_CTRL
static void zclSampleLight_LevelControlMoveToLevelCB( zclLCMoveToLevel_t *pCmd );
static void zclSampleLight_LevelControlMoveCB( zclLCMove_t *pCmd );
static void zclSampleLight_LevelControlStepCB( zclLCStep_t *pCmd );
static void zclSampleLight_LevelControlStopCB( void );
static void zclSampleLight_DefaultMove( void );
static uint32 zclSampleLight_TimeRateHelper( uint8 newLevel );
static uint16 zclSampleLight_GetTime ( uint8 level, uint16 time );
static void zclSampleLight_MoveBasedOnRate( uint8 newLevel, uint32 rate );
static void zclSampleLight_MoveBasedOnTime( uint8 newLevel, uint16 time );
static void zclSampleLight_AdjustLightLevel( void );
//-- MOD START
static void sendZclAttrChangeReport(uint8 srcEp, uint16 clusterId, uint16 attrID, uint8 *lastReportValue, uint8 *currentValue);
//-- MOD END
#endif
// app display functions
static void zclSampleLight_LcdDisplayUpdate( void );
#ifdef LCD_SUPPORTED
static void zclSampleLight_LcdDisplayMainMode( void );
static void zclSampleLight_LcdDisplayHelpMode( void );
#endif
static void zclSampleLight_DisplayLight( void );
#if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
void zclSampleLight_UpdateLampLevel( uint8 level );
#endif
// Functions to process ZCL Foundation incoming Command/Response messages
static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *msg );
#ifdef ZCL_READ
static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
#endif
#ifdef ZCL_WRITE
static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
#endif
static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
#ifdef ZCL_DISCOVER
static uint8 zclSampleLight_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleLight_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleLight_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
#endif
//-- MOD START
#ifdef ZCL_REPORT
static uint8 zclSampleLight_ProcessInConfigReportCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleLight_ProcessInReadReportCfgCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleLight_ProcessInReportCmd( zclIncomingMsg_t *pInMsg );
static void zclSampleLight_CheckReportConfig( void );
static uint8 sendZclAttrReport(uint8 srcEp, uint16 clusterID, zclReportCmd_t *pReportCmd, uint8 dataLen);
static void zclSampleLight_CheckandSendClusterAttrReport( uint16 clusterID, zclConfigReportRecsList *pConfigReportRecsList );
#endif
//-- MOD END
/*********************************************************************
* STATUS STRINGS
*/
#ifdef LCD_SUPPORTED
const char sDeviceName[] = " Sample Light";
const char sClearLine[] = " ";
const char sSwLight[] = "SW1: ToggleLight"; // 16 chars max
const char sSwEZMode[] = "SW2: EZ-Mode";
char sSwHelp[] = "SW5: Help "; // last character is * if NWK open
const char sLightOn[] = " LIGHT ON ";
const char sLightOff[] = " LIGHT OFF";
#if ZCL_LEVEL_CTRL
char sLightLevel[] = " LEVEL ###"; // displays level 1-254
#endif
#endif
/*********************************************************************
* ZCL General Profile Callback table
*/
static zclGeneral_AppCallbacks_t zclSampleLight_CmdCallbacks =
{
zclSampleLight_BasicResetCB, // Basic Cluster Reset command
zclSampleLight_IdentifyCB, // Identify command
#ifdef ZCL_EZMODE
NULL, // Identify EZ-Mode Invoke command
NULL, // Identify Update Commission State command
#endif
NULL, // Identify Trigger Effect command
zclSampleLight_IdentifyQueryRspCB, // Identify Query Response command
zclSampleLight_OnOffCB, // On/Off cluster commands
NULL, // On/Off cluster enhanced command Off with Effect
NULL, // On/Off cluster enhanced command On with Recall Global Scene
NULL, // On/Off cluster enhanced command On with Timed Off
#ifdef ZCL_LEVEL_CTRL
zclSampleLight_LevelControlMoveToLevelCB, // Level Control Move to Level command
zclSampleLight_LevelControlMoveCB, // Level Control Move command
zclSampleLight_LevelControlStepCB, // Level Control Step command
zclSampleLight_LevelControlStopCB, // Level Control Stop command
#endif
#ifdef ZCL_GROUPS
NULL, // Group Response commands
#endif
#ifdef ZCL_SCENES
NULL, // Scene Store Request command
NULL, // Scene Recall Request command
NULL, // Scene Response command
#endif
#ifdef ZCL_ALARMS
NULL, // Alarm (Response) commands
#endif
#ifdef SE_UK_EXT
NULL, // Get Event Log command
NULL, // Publish Event Log command
#endif
NULL, // RSSI Location command
NULL // RSSI Location Response command
};
/*********************************************************************
* @fn zclSampleLight_Init
*
* @brief Initialization function for the zclGeneral layer.
*
* @param none
*
* @return none
*/
void zclSampleLight_Init( byte task_id )
{
zclSampleLight_TaskID = task_id;
// Set destination address to indirect
zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
zclSampleLight_DstAddr.endPoint = 0;
zclSampleLight_DstAddr.addr.shortAddr = 0;
// This app is part of the Home Automation Profile
zclHA_Init( &zclSampleLight_SimpleDesc );
// Register the ZCL General Cluster Library callback functions
zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );
// Register the application's attribute list
zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, zclSampleLight_NumAttributes, zclSampleLight_Attrs );
//-- MOD START
#ifdef ZCL_REPORT
// Register the application's config report record list
zcl_registerConfigReportRecList( SAMPLELIGHT_ENDPOINT, zclSampleLight_NumConfigReportRecs, zclSampleLight_ConfigReportRecs );
#endif
//-- MOD END
// Register the Application to receive the unprocessed Foundation command/response messages
zcl_registerForMsg( zclSampleLight_TaskID );
#ifdef ZCL_DISCOVER
// Register the application's command list
zcl_registerCmdList( SAMPLELIGHT_ENDPOINT, zclCmdsArraySize, zclSampleLight_Cmds );
#endif
// Register for all key events - This app will handle all key events
RegisterForKeys( zclSampleLight_TaskID );
// Register for a test endpoint
afRegister( &sampleLight_TestEp );
#ifdef ZCL_EZMODE
// Register EZ-Mode
zcl_RegisterEZMode( &zclSampleLight_RegisterEZModeData );
// Register with the ZDO to receive Match Descriptor Responses
ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
#endif
#if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
HalTimer1Init( 0 );
halTimer1SetChannelDuty( WHITE_LED, 0 );
halTimer1SetChannelDuty( RED_LED, 0 );
halTimer1SetChannelDuty( BLUE_LED, 0 );
halTimer1SetChannelDuty( GREEN_LED, 0 );
// find if we are already on a network from NV_RESTORE
uint8 state;
NLME_GetRequest( nwkNwkState, 0, &state );
if ( state < NWK_ENDDEVICE )
{
// Start EZMode on Start up to avoid button press
osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_START_EZMODE_EVT, 500 );
}
#if ZCL_LEVEL_CTRL
zclSampleLight_DefaultMove();
#endif
#endif // #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
#ifdef ZCL_DIAGNOSTIC
// Register the application's callback function to read/write attribute data.
// This is only required when the attribute data format is unknown to ZCL.
zcl_registerReadWriteCB( SAMPLELIGHT_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
if ( zclDiagnostic_InitStats() == ZSuccess )
{
// Here the user could start the timer to save Diagnostics to NV
}
#endif
#ifdef LCD_SUPPORTED
HalLcdWriteString ( (char *)sDeviceName, HAL_LCD_LINE_3 );
#endif // LCD_SUPPORTED
#ifdef ZGP_AUTO_TT
zgpTranslationTable_RegisterEP ( &zclSampleLight_SimpleDesc );
#endif
}
/*********************************************************************
* @fn zclSample_event_loop
*
* @brief Event Loop Processor for zclGeneral.
*
* @param none
*
* @return none
*/
uint16 zclSampleLight_event_loop( uint8 task_id, uint16 events )
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id; // Intentionally unreferenced parameter
if ( events & SYS_EVENT_MSG )
{
while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleLight_TaskID )) )
{
switch ( MSGpkt->hdr.event )
{
#ifdef ZCL_EZMODE
case ZDO_CB_MSG:
zclSampleLight_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
break;
#endif
case ZCL_INCOMING_MSG:
// Incoming ZCL Foundation command/response messages
zclSampleLight_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
break;
case KEY_CHANGE:
zclSampleLight_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
case ZDO_STATE_CHANGE:
zclSampleLight_NwkState = (devStates_t)(MSGpkt->hdr.status);
// now on the network
if ( (zclSampleLight_NwkState == DEV_ZB_COORD) ||
(zclSampleLight_NwkState == DEV_ROUTER) ||
(zclSampleLight_NwkState == DEV_END_DEVICE) )
{
giLightScreenMode = LIGHT_MAINMODE;
zclSampleLight_LcdDisplayUpdate();
#ifdef ZCL_EZMODE
zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
#endif // ZCL_EZMODE
}
break;
default:
break;
}
// Release the memory
osal_msg_deallocate( (uint8 *)MSGpkt );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
if ( events & SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT )
{
if ( zclSampleLight_IdentifyTime > 0 )
zclSampleLight_IdentifyTime--;
zclSampleLight_ProcessIdentifyTimeChange();
return ( events ^ SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT );
}
if ( events & SAMPLELIGHT_MAIN_SCREEN_EVT )
{
giLightScreenMode = LIGHT_MAINMODE;
zclSampleLight_LcdDisplayUpdate();
return ( events ^ SAMPLELIGHT_MAIN_SCREEN_EVT );
}
#ifdef ZCL_EZMODE
#if (defined HAL_BOARD_ZLIGHT)
// event to start EZMode on startup with a delay
if ( events & SAMPLELIGHT_START_EZMODE_EVT )
{
// Invoke EZ-Mode
zclEZMode_InvokeData_t ezModeData;
// Invoke EZ-Mode
ezModeData.endpoint = SAMPLELIGHT_ENDPOINT; // endpoint on which to invoke EZ-Mode
if ( (zclSampleLight_NwkState == DEV_ZB_COORD) ||
(zclSampleLight_NwkState == DEV_ROUTER) ||
(zclSampleLight_NwkState == DEV_END_DEVICE) )
{
ezModeData.onNetwork = TRUE; // node is already on the network
}
else
{
ezModeData.onNetwork = FALSE; // node is not yet on the network
}
ezModeData.initiator = FALSE; // OnOffLight is a target
ezModeData.numActiveOutClusters = 0;
ezModeData.pActiveOutClusterIDs = NULL;
ezModeData.numActiveInClusters = 0;
ezModeData.pActiveOutClusterIDs = NULL;
zcl_InvokeEZMode( &ezModeData );
return ( events ^ SAMPLELIGHT_START_EZMODE_EVT );
}
#endif // #if (defined HAL_BOARD_ZLIGHT)
// going on to next state
if ( events & SAMPLELIGHT_EZMODE_NEXTSTATE_EVT )
{
zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL ); // going on to next state
return ( events ^ SAMPLELIGHT_EZMODE_NEXTSTATE_EVT );
}
// the overall EZMode timer expired, so we timed out
if ( events & SAMPLELIGHT_EZMODE_TIMEOUT_EVT )
{
zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
return ( events ^ SAMPLELIGHT_EZMODE_TIMEOUT_EVT );
}
#endif // ZLC_EZMODE
#ifdef ZCL_LEVEL_CTRL
if ( events & SAMPLELIGHT_LEVEL_CTRL_EVT )
{
zclSampleLight_AdjustLightLevel();
return ( events ^ SAMPLELIGHT_LEVEL_CTRL_EVT );
}
if ( events & TURN_OFF_LIGHT )
{
zclSampleLight_OnOffCB(COMMAND_OFF);
return ( events ^ TURN_OFF_LIGHT );
}
#endif
//-- MOD START
#ifdef ZCL_REPORT
if ( events & SAMPLELIGHT_CHECK_REPORT_EVT )
{
zclSampleLight_CheckReportConfig();
return ( events ^ SAMPLELIGHT_CHECK_REPORT_EVT );
}
#endif
//-- MOD END
// Discard unknown events
return 0;
}
/*********************************************************************
* @fn zclSampleLight_HandleKeys
*
* @brief Handles all key events for this device.
*
* @param shift - true if in shift/alt.
* @param keys - bit field for key events. Valid entries:
* HAL_KEY_SW_5
* HAL_KEY_SW_4
* HAL_KEY_SW_2
* HAL_KEY_SW_1
*
* @return none
*/
static void zclSampleLight_HandleKeys( byte shift, byte keys )
{
if ( keys & HAL_KEY_SW_1 )
{
giLightScreenMode = LIGHT_MAINMODE;
// toggle local light immediately
zclSampleLight_OnOff = zclSampleLight_OnOff ? LIGHT_OFF : LIGHT_ON;
#ifdef ZCL_LEVEL_CTRL
zclSampleLight_LevelCurrentLevel = zclSampleLight_OnOff ? zclSampleLight_LevelOnLevel : ATTR_LEVEL_MIN_LEVEL;
#endif
//-- MOD START
if (zclSampleLight_OnOffLastReportValue != zclSampleLight_OnOff)
{
sendZclAttrChangeReport(SAMPLELIGHT_ENDPOINT, ZCL_CLUSTER_ID_GEN_ON_OFF, ATTRID_ON_OFF,
&zclSampleLight_OnOffLastReportValue, &zclSampleLight_OnOff);
}
//-- MOD END
}
if ( keys & HAL_KEY_SW_2 )
{
#if (defined HAL_BOARD_ZLIGHT)
zclSampleLight_BasicResetCB();
#else
giLightScreenMode = LIGHT_MAINMODE;
#ifdef ZCL_EZMODE
{
// Invoke EZ-Mode
zclEZMode_InvokeData_t ezModeData;
// Invoke EZ-Mode
ezModeData.endpoint = SAMPLELIGHT_ENDPOINT; // endpoint on which to invoke EZ-Mode
if ( (zclSampleLight_NwkState == DEV_ZB_COORD) ||
(zclSampleLight_NwkState == DEV_ROUTER) ||
(zclSampleLight_NwkState == DEV_END_DEVICE) )
{
ezModeData.onNetwork = TRUE; // node is already on the network
}
else
{
ezModeData.onNetwork = FALSE; // node is not yet on the network
}
ezModeData.initiator = FALSE; // OnOffLight is a target
ezModeData.numActiveOutClusters = 0;
ezModeData.pActiveOutClusterIDs = NULL;
ezModeData.numActiveInClusters = 0;
ezModeData.pActiveOutClusterIDs = NULL;
zcl_InvokeEZMode( &ezModeData );
}
#else // NOT EZ-Mode
{
zAddrType_t dstAddr;
HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
// Initiate an End Device Bind Request, this bind request will
// only use a cluster list that is important to binding.
dstAddr.addrMode = afAddr16Bit;
dstAddr.addr.shortAddr = 0; // Coordinator makes the match
ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
SAMPLELIGHT_ENDPOINT,
ZCL_HA_PROFILE_ID,
ZCLSAMPLELIGHT_BINDINGLIST, bindingInClusters,
0, NULL, // No Outgoing clusters to bind
TRUE );
}
#endif // ZCL_EZMODE
#endif // HAL_BOARD_ZLIGHT
}
if ( keys & HAL_KEY_SW_4 )
{
giLightScreenMode = LIGHT_MAINMODE;
if ( ( zclSampleLight_NwkState == DEV_ZB_COORD ) ||
( zclSampleLight_NwkState == DEV_ROUTER ) )
{
// toggle permit join
gPermitDuration = gPermitDuration ? 0 : 0xff;
NLME_PermitJoiningRequest( gPermitDuration );
}
}
// Shift F5 does a Basic Reset (factory defaults)
if ( shift && ( keys & HAL_KEY_SW_5 ) )
{
zclSampleLight_BasicResetCB();
}
else if ( keys & HAL_KEY_SW_5 )
{
giLightScreenMode = giLightScreenMode ? LIGHT_MAINMODE : LIGHT_HELPMODE;
}
// update the display, including the light
zclSampleLight_LcdDisplayUpdate();
}
/*********************************************************************
* @fn zclSampleLight_LcdDisplayUpdate
*
* @brief Called to update the LCD display.
*
* @param none
*
* @return none
*/
void zclSampleLight_LcdDisplayUpdate( void )
{
#ifdef LCD_SUPPORTED
if ( giLightScreenMode == LIGHT_HELPMODE )
{
zclSampleLight_LcdDisplayHelpMode();
}
else
{
zclSampleLight_LcdDisplayMainMode();
}
#endif
zclSampleLight_DisplayLight();
}
#if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
/*********************************************************************
* @fn zclSampleLight_UpdateLampLevel
*
* @brief Update lamp level output with gamma compensation
*
* @param level
*
* @return none
*/
void zclSampleLight_UpdateLampLevel( uint8 level )
{
uint16 gammaCorrectedLevel;
// gamma correct the level
gammaCorrectedLevel = (uint16) ( pow( ( (float)level / LEVEL_MAX ), (float)GAMMA_VALUE ) * (float)LEVEL_MAX);
halTimer1SetChannelDuty(WHITE_LED, (uint16)(((uint32)gammaCorrectedLevel*PWM_FULL_DUTY_CYCLE)/LEVEL_MAX) );
}
#endif
/*********************************************************************
* @fn zclSampleLight_DisplayLight
*
* @brief Displays current state of light on LED and also on main display if supported.
*
* @param none
*
* @return none
*/
static void zclSampleLight_DisplayLight( void )
{
// set the LED1 based on light (on or off)
if ( zclSampleLight_OnOff == LIGHT_ON )
{
HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
}
else
{
HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
}
#ifdef LCD_SUPPORTED
if (giLightScreenMode == LIGHT_MAINMODE)
{
#ifdef ZCL_LEVEL_CTRL
// display current light level
if ( ( zclSampleLight_LevelCurrentLevel == ATTR_LEVEL_MIN_LEVEL ) &&
( zclSampleLight_OnOff == LIGHT_OFF ) )
{
HalLcdWriteString( (char *)sLightOff, HAL_LCD_LINE_2 );
}
else if ( ( zclSampleLight_LevelCurrentLevel >= ATTR_LEVEL_MAX_LEVEL ) ||
( zclSampleLight_LevelCurrentLevel == zclSampleLight_LevelOnLevel ) ||
( ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT ) &&
( zclSampleLight_LevelCurrentLevel == zclSampleLight_LevelLastLevel ) ) )
{
HalLcdWriteString( (char *)sLightOn, HAL_LCD_LINE_2 );
}
else // " LEVEL ###"
{
zclHA_uint8toa( zclSampleLight_LevelCurrentLevel, &sLightLevel[10] );
HalLcdWriteString( (char *)sLightLevel, HAL_LCD_LINE_2 );
}
#else
if ( zclSampleLight_OnOff )
{
HalLcdWriteString( (char *)sLightOn, HAL_LCD_LINE_2 );
}
else
{
HalLcdWriteString( (char *)sLightOff, HAL_LCD_LINE_2 );
}
#endif // ZCL_LEVEL_CTRL
}
#endif // LCD_SUPPORTED
}
#ifdef LCD_SUPPORTED
/*********************************************************************
* @fn zclSampleLight_LcdDisplayMainMode
*
* @brief Called to display the main screen on the LCD.
*
* @param none
*
* @return none
*/
static void zclSampleLight_LcdDisplayMainMode( void )
{
// display line 1 to indicate NWK status
if ( zclSampleLight_NwkState == DEV_ZB_COORD )
{
zclHA_LcdStatusLine1( ZCL_HA_STATUSLINE_ZC );
}
else if ( zclSampleLight_NwkState == DEV_ROUTER )
{
zclHA_LcdStatusLine1( ZCL_HA_STATUSLINE_ZR );
}
else if ( zclSampleLight_NwkState == DEV_END_DEVICE )
{
zclHA_LcdStatusLine1( ZCL_HA_STATUSLINE_ZED );
}
// end of line 3 displays permit join status (*)
if ( gPermitDuration )
{
sSwHelp[15] = '*';
}
else
{
sSwHelp[15] = ' ';
}
HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
}
/*********************************************************************
* @fn zclSampleLight_LcdDisplayHelpMode
*
* @brief Called to display the SW options on the LCD.
*
* @param none
*
* @return none
*/
static void zclSampleLight_LcdDisplayHelpMode( void )
{
HalLcdWriteString( (char *)sSwLight, HAL_LCD_LINE_1 );
HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
}
#endif // LCD_SUPPORTED
/*********************************************************************
* @fn zclSampleLight_ProcessIdentifyTimeChange
*
* @brief Called to process any change to the IdentifyTime attribute.
*
* @param none
*
* @return none
*/
static void zclSampleLight_ProcessIdentifyTimeChange( void )
{
if ( zclSampleLight_IdentifyTime > 0 )
{
osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT, 1000 );
HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
}
else
{
#ifdef ZCL_EZMODE
if ( zclSampleLight_IdentifyCommissionState & EZMODE_COMMISSION_OPERATIONAL )
{
HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
}
else
{
HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
}
#endif
osal_stop_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_IDENTIFY_TIMEOUT_EVT );
}
}
/*********************************************************************
* @fn zclSampleLight_BasicResetCB
*
* @brief Callback from the ZCL General Cluster Library
* to set all the Basic Cluster attributes to default values.
*
* @param none
*
* @return none
*/
static void zclSampleLight_BasicResetCB( void )
{
NLME_LeaveReq_t leaveReq;
// Set every field to 0
osal_memset( &leaveReq, 0, sizeof( NLME_LeaveReq_t ) );
// This will enable the device to rejoin the network after reset.
leaveReq.rejoin = TRUE;
// Set the NV startup option to force a "new" join.
zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
// Leave the network, and reset afterwards
if ( NLME_LeaveReq( &leaveReq ) != ZSuccess )
{
// Couldn't send out leave; prepare to reset anyway
ZDApp_LeaveReset( FALSE );
}
}
/*********************************************************************
* @fn zclSampleLight_IdentifyCB
*
* @brief Callback from the ZCL General Cluster Library when
* it received an Identity Command for this application.
*
* @param srcAddr - source address and endpoint of the response message
* @param identifyTime - the number of seconds to identify yourself
*
* @return none
*/
static void zclSampleLight_IdentifyCB( zclIdentify_t *pCmd )
{
zclSampleLight_IdentifyTime = pCmd->identifyTime;
zclSampleLight_ProcessIdentifyTimeChange();
}
/*********************************************************************
* @fn zclSampleLight_IdentifyQueryRspCB
*
* @brief Callback from the ZCL General Cluster Library when
* it received an Identity Query Response Command for this application.
*
* @param srcAddr - requestor's address
* @param timeout - number of seconds to identify yourself (valid for query response)
*
* @return none
*/
static void zclSampleLight_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
{
(void)pRsp;
#ifdef ZCL_EZMODE
{
zclEZMode_ActionData_t data;
data.pIdentifyQueryRsp = pRsp;
zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
}
#endif
}
/*********************************************************************
* @fn zclSampleLight_OnOffCB
*
* @brief Callback from the ZCL General Cluster Library when
* it received an On/Off Command for this application.
*
* @param cmd - COMMAND_ON, COMMAND_OFF or COMMAND_TOGGLE
*
* @return none
*/
static void zclSampleLight_OnOffCB( uint8 cmd )
{
// Turn on the light
if ( cmd == COMMAND_ON )
{
zclSampleLight_OnOff = LIGHT_ON;
}
// Turn off the light
else if ( cmd == COMMAND_OFF )
{
zclSampleLight_OnOff = LIGHT_OFF;
}
// Toggle the light
else if ( cmd == COMMAND_TOGGLE )
{
if ( zclSampleLight_OnOff == LIGHT_OFF )
{
zclSampleLight_OnOff = LIGHT_ON;
}
else
{
zclSampleLight_OnOff = LIGHT_OFF;
}
}
//-- MOD START
if (zclSampleLight_OnOffLastReportValue != zclSampleLight_OnOff)
{
sendZclAttrChangeReport(SAMPLELIGHT_ENDPOINT, ZCL_CLUSTER_ID_GEN_ON_OFF, ATTRID_ON_OFF,
&zclSampleLight_OnOffLastReportValue, &zclSampleLight_OnOff);
}
//-- MOD END
#if ZCL_LEVEL_CTRL
zclSampleLight_DefaultMove( );
#endif
// update the display
zclSampleLight_LcdDisplayUpdate( );
}