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 }
int GetBlue(void) const
#define err(ctx, arg...)
int GetBrightness(void) const
Lightify_Group * GetGroupAtPosX(int pos) const
int GetGreen(void) const
int IsStale(void) const
int GetGroupsCount(void)
int GetCCT(void) const
int Open(void)
int GetNodesCount(void)
int ScanNodes(void)
int GetOnlineState(void) const
int main(void)
int ScanGroups(void)
lightify_node_type GetLampType(void) const
Lightify_Node * GetNodeAtPosX(int x) const
const char * decode_lamptype(int type)
unsigned long long GetMAC(void) const
int IsOn(void) const
int GetWhite(void) const
Enable the use of exception within this wrapper.
const char * decode_online_state(int state)
unsigned int GetGroup(void) const
int GetRed(void) const
const char * GetName()
const char * GetName(void) const
const char * decode_onoff_sate(int state)