/
send.c
137 lines (110 loc) · 4.64 KB
/
send.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
/*
* libniftyled - Interface library for LED interfaces
* Copyright (C) 2010-2014 Daniel Hiepler <daniel@niftylight.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Alternatively, the contents of this file may be used under the
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
* which case the following provisions apply instead of the ones
* mentioned above:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <niftyled.h>
int main(int argc, char *argv[])
{
nft_log_level_set(L_DEBUG);
/* register new dummy hardware called "nlo01" */
LedHardware *h;
if(!(h = led_hardware_new("nlo01", "dummy")))
{
NFT_LOG(L_ERROR, "Hardware creation FAILED");
return -1;
}
/* initialize any hardware that can be found (id="*"), define 12
* connected LEDs (4 BGR pixels). Greyscale data should be provided in
* unsigned 8-bit format */
if(!led_hardware_init(h, "*", 12, "BGR u8"))
{
NFT_LOG(L_ERROR, "failed to initialize hardware");
return -1;
}
/**********************************************************************
* niftyled basically provides 2 ways to send data to LED hardware:
*
* 1. set "raw" greyscale value of a LED in a chain connected to a
* correctly initialized LED hardware
*
* 1.1 use the API to set each greyscale value
*
* 1.2 directly write to the RAW buffer
*
*
* 2. send a complete pixelframe and have niftyled translate that to
* mapped LEDs connected to one or more LED hardware devices
* according to the current setup configuration (where each LED
* has been assigned a X/Y coordinate and a "Component" that refers
* to a component of a pixel at this position in the pixelframe)
*/
/****** METHOD 1.1 *******/
/* get chain of this hardware */
LedChain *c = led_hardware_get_chain(h);
/* set all LEDs to half brightness */
LedCount l;
for(l = 0; l < led_chain_get_ledcount(c); l++)
{
led_chain_set_greyscale(c, l, 128);
}
/* send data to hardware */
led_hardware_send(h);
/* show previously sent hardware (latch to LEDs) */
led_hardware_show(h);
/****** METHOD 1.2 *******/
/* ...or access raw pixelbuffer */
uint8_t *pixels = led_chain_get_buffer(c);
/* walk all pixels to turn on all LEDs */
int i;
for(i = 0; i < 16; i++)
pixels[i] = 0xff;
/* send data to hardware */
led_hardware_send(h);
/* show previously sent hardware (latch to LEDs) */
led_hardware_show(h);
/****** METHOD 2 - s. ledcat sources *******/
/* deinitialize hardware */
led_hardware_deinit(h);
return 0;
}