2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "HL_UI_LED.h"
28 #define SUCCESS_FLAG 760302
35 HL_UI_LED_Init(uint32_t led_num)
41 handle = (HL_UI_LED*)malloc(sizeof(HL_UI_LED));
46 handle->number = led_num;
47 handle->brightness = 0xFF;
48 handle->pixels = (uint8_t *)malloc(handle->number * 4);
49 if(handle->pixels == NULL)
55 while(count < RETRY_TIMES)
57 if(peripheral_spi_open(SPI_BUS, SPI_DEV, &(handle->hnd_spi)) == 0)
59 printf("spi open success!\n");
61 if((ret = peripheral_spi_set_frequency(handle->hnd_spi, BITRATE)) != 0)
63 printf("Frequency Failed : 0x%x\n", ret);
65 if((ret = peripheral_spi_set_bits_per_word(handle->hnd_spi, 8)) != 0)
67 printf("BIT_WORD Failed : 0x%x\n", ret);
69 if((ret = peripheral_spi_set_bit_order(handle->hnd_spi,PERIPHERAL_SPI_BIT_ORDER_MSB)) != 0)
71 printf("BIT_ORDER Failed : 0x%x\n", ret);
73 if((ret = peripheral_spi_set_mode(handle->hnd_spi,PERIPHERAL_SPI_MODE_1)) != 0)
75 printf("SPI Mode Failed : 0x%x\n", ret);
85 if(count == SUCCESS_FLAG)
87 HL_UI_LED_Clear_All(handle);
99 HL_UI_LED_Change_Brightness(HL_UI_LED *handle, uint8_t brightness)
102 handle->brightness = 0xFF;
104 handle->brightness = 0xE0 | (0x1F & brightness);
105 HL_UI_LED_Refresh(handle);
109 HL_UI_LED_Get_Brightness(HL_UI_LED *handle)
111 return handle->brightness & 0x1F;
115 HL_UI_LED_Set_Pixel_RGB(HL_UI_LED *handle, uint32_t index, uint8_t red, uint8_t green, uint8_t blue)
117 if (index < handle->number) {
118 uint8_t *ptr = &(handle->pixels[index * 4]);
119 ptr[R_OFF_SET] = red;
120 ptr[G_OFF_SET] = green;
121 ptr[B_OFF_SET] = blue;
126 HL_UI_LED_Get_Pixel_RGB(HL_UI_LED *handle, uint32_t index, uint8_t *red, uint8_t *green, uint8_t *blue)
128 if (index < handle->number) {
129 uint8_t *ptr = &(handle->pixels[index * 4]);
130 *red = ptr[R_OFF_SET];
131 *green = ptr[G_OFF_SET];
132 *blue = ptr[B_OFF_SET];
137 HL_UI_LED_Set_Pixel_4byte(HL_UI_LED *handle, uint32_t index, uint32_t colour)
140 uint8_t *ptr = (uint8_t *)&colour;
144 HL_UI_LED_Set_Pixel_RGB(handle, index, r, g, b);
148 HL_UI_LED_Get_Pixel_4byte(HL_UI_LED *handle, uint32_t index)
150 uint8_t r=0, g=0, b=0;
152 uint8_t *ptr = (uint8_t *)&colour;
153 HL_UI_LED_Get_Pixel_RGB(handle, index, &r, &g, &b);
161 HL_UI_LED_Clear_All(HL_UI_LED *handle)
165 for(ptr = handle->pixels, i=0; i<handle->number; i++, ptr += 4) {
170 HL_UI_LED_Refresh(handle);
174 HL_UI_LED_Refresh(HL_UI_LED *handle)
178 uint32_t buf_len = 4 + 4 * handle->number + (handle->number + 15) / 16 + 1;
180 uint8_t *tx = (uint8_t *)malloc(buf_len);
187 for (i = 0; i < 4; i++)
193 for(ptr = handle->pixels, i=0; i<handle->number; i++, ptr += 4, qtr += 4) {
194 qtr[0] = handle->brightness;
201 for (i = handle->number * 4 + 4; i < buf_len; i++)
206 ret = peripheral_spi_write(handle->hnd_spi, tx, buf_len);
210 fprintf(stdout, "[Error] can't send spi message\n");
218 HL_UI_LED_Show(HL_UI_LED *handle)
220 return HL_UI_LED_Refresh(handle);
224 HL_UI_LED_Close(HL_UI_LED *handle)
226 HL_UI_LED_Clear_All(handle);
227 peripheral_spi_close(handle->hnd_spi);
229 if (handle->pixels) {
230 free(handle->pixels);