liblightify
lightify-example.cpp
Go to the documentation of this file.
1 /*
2  liblightify -- library to control OSRAM's LIGHTIFY
3 
4  Copyright (c) 2015, Tobias Frost <tobi@coldtobi.de>
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  * Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  * Neither the name of the author nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
36 
37 #include <iostream>
38 #include <iomanip>
39 
40 using namespace std;
41 
42 static const char *decode_lamptype(int type) {
43  switch (type) {
45  return "oo-plug";
47  return "dim";
49  return "color";
51  return "ext-col";
52  case LIGHTIFY_CCT_LIGHT:
53  return "cct";
55  return "4way-sw";
56  default:
57  return "unknown";
58  }
59 }
60 
61 static const char *decode_onoff_sate(int state) {
62  if (state < 0) return "err";
63  switch (state) {
64  case 0:
65  return "off";
66  case 1:
67  return "on";
68  default:
69  return "???";
70  }
71 }
72 
73 static const char *decode_online_state(int state) {
74  switch (state) {
75  case LIGHTIFY_OFFLINE:
76  return "offline";
77  case LIGHTIFY_ONLINE:
78  return "online";
79  default:
80  return "unknown";
81  }
82 }
83 
84 
85 int main(void) {
86  int err;
87 
88  Lightify l("lightify", 4000);
89  err = l.Open();
90  if (err < 0) cerr << "Lightify::Open failed: " << -err << " " << strerror(-err) << endl;
91  l.ScanNodes();
92 
93  Lightify_Node *node = 0;
94  cout << "|-----------------|------------------|---------|-------|---------|-----|-----|------|-----|-----|-----|-----|---|" << endl;
95  cout << "| Name | MAC | type | group | online | 0/1 | dim | CCT | Red | Grn | Blu | Wht | s |" << endl;
96  cout << "|-----------------|------------------|---------|-------|---------|-----|-----|------|-----|-----|-----|-----|---|" << endl;
97 
98  for(int i = 0; i< l.GetNodesCount(); i++) {
99  node = l.GetNodeAtPosX(i);
100  cout << '|' <<
101  setw(16) << node->GetName() << " | " <<
102  setw(16) << hex << node->GetMAC() << " | " <<
103  setw(7) << decode_lamptype(node->GetLampType()) << " | " <<
104  setw(5) << dec << node->GetGroup() << " | " <<
105  setw(7) << decode_online_state(node->GetOnlineState()) << " | " <<
106  setw(3) << decode_onoff_sate(node->IsOn()) << " | " <<
107  setw(3) << node->GetBrightness() << " | " <<
108  setw(4) << node->GetCCT() << " | " <<
109  setw(3) << node->GetRed() << " | " <<
110  setw(3) << node->GetGreen() << " | " <<
111  setw(3) << node->GetBlue() << " | " <<
112  setw(3) << node->GetWhite() << " | " <<
113  setw(1) << (node->IsStale() ? '*' :' ') << " |" << endl;
114  }
115 
116 
117  cout << "|-----------------|------------------|---------|-------|---------|-----|-----|------|-----|-----|-----|-----|---|" << endl << endl;
118  l.ScanGroups();
119  cout << "|------------------|--------|" << endl;
120  cout << "| Group Name | id |" << endl;
121  cout << "|------------------|--------|" << endl;
122  Lightify_Group *group = NULL;
123  for (int i = 0; i < l.GetGroupsCount(); i++) {
124  group = l.GetGroupAtPosX(i);
125  cout << '|' <<
126  setw(17) << group->GetName() << " | " <<
127  setw(6) << group->GetId() << " |" << endl;
128  }
129  cout << "|------------------|--------|" << endl;
130 
131  return 0;
132 }
Lightify_Node::GetName
const char * GetName(void) const
Definition: liblightify++.hpp:75
Lightify_Node::GetBlue
int GetBlue(void) const
Definition: liblightify++.hpp:105
Lightify::Open
int Open(void)
Definition: liblightify++.hpp:339
err
#define err(ctx, arg...)
Lightify_Node::GetRed
int GetRed(void) const
Definition: liblightify++.hpp:100
LIGHTIFY_ONOFF_PLUG
@ LIGHTIFY_ONOFF_PLUG
Definition: liblightify.h:106
LIGHTIFY_OFFLINE
@ LIGHTIFY_OFFLINE
Definition: liblightify.h:120
main
int main(void)
Definition: lightify-example.cpp:85
Lightify_Node::GetLampType
lightify_node_type GetLampType(void) const
Definition: liblightify++.hpp:95
Lightify_Group
Definition: liblightify++.hpp:220
LIGHTIFY_DIMABLE_LIGHT
@ LIGHTIFY_DIMABLE_LIGHT
Definition: liblightify.h:107
Lightify::GetNodeAtPosX
Lightify_Node * GetNodeAtPosX(int x) const
Definition: liblightify++.hpp:508
Lightify_Node::GetGreen
int GetGreen(void) const
Definition: liblightify++.hpp:110
Lightify_Node::GetMAC
unsigned long long GetMAC(void) const
Definition: liblightify++.hpp:80
Lightify
Definition: liblightify++.hpp:304
Lightify::ScanGroups
int ScanGroups(void)
Definition: liblightify++.hpp:448
Lightify_Node
Enable the use of exception within this wrapper.
Definition: liblightify++.hpp:60
Lightify_Node::IsOn
int IsOn(void) const
Definition: liblightify++.hpp:130
Lightify::GetGroupAtPosX
Lightify_Group * GetGroupAtPosX(int pos) const
Definition: liblightify++.hpp:520
LIGHTIFY_COLOUR_LIGHT
@ LIGHTIFY_COLOUR_LIGHT
Definition: liblightify.h:108
LIGHTIFY_ONLINE
@ LIGHTIFY_ONLINE
Definition: liblightify.h:121
Lightify_Node::GetGroup
unsigned int GetGroup(void) const
Definition: liblightify++.hpp:90
Lightify_Node::GetOnlineState
int GetOnlineState(void) const
Definition: liblightify++.hpp:135
LIGHTIFY_4WAY_SWITCH
@ LIGHTIFY_4WAY_SWITCH
Definition: liblightify.h:111
Lightify_Node::IsStale
int IsStale(void) const
Definition: liblightify++.hpp:142
Lightify_Node::GetCCT
int GetCCT(void) const
Definition: liblightify++.hpp:120
LIGHTIFY_CCT_LIGHT
@ LIGHTIFY_CCT_LIGHT
Definition: liblightify.h:110
Lightify_Node::GetBrightness
int GetBrightness(void) const
Definition: liblightify++.hpp:125
Lightify_Group::GetId
int GetId()
Definition: liblightify++.hpp:230
decode_lamptype
const char * decode_lamptype(int type)
Definition: lightify-util.c:311
Lightify::GetNodesCount
int GetNodesCount(void)
Definition: liblightify++.hpp:563
Lightify::ScanNodes
int ScanNodes(void)
Definition: liblightify++.hpp:410
decode_onoff_sate
const char * decode_onoff_sate(int state)
Definition: lightify-util.c:330
Lightify_Node::GetWhite
int GetWhite(void) const
Definition: liblightify++.hpp:115
decode_online_state
const char * decode_online_state(int state)
Definition: lightify-util.c:300
Lightify_Group::GetName
const char * GetName()
Definition: liblightify++.hpp:234
Lightify::GetGroupsCount
int GetGroupsCount(void)
Definition: liblightify++.hpp:567
LIGHTIFY_EXT_COLOUR_LIGHT
@ LIGHTIFY_EXT_COLOUR_LIGHT
Definition: liblightify.h:109
liblightify++.hpp