RGB LED: improved set_color

This commit is contained in:
Your Name 2021-11-13 21:21:58 +01:00
parent 464dbc78e4
commit 32746c111f
5 changed files with 56 additions and 29 deletions

View File

@ -188,4 +188,5 @@
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="refreshScope"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
</cproject>

View File

@ -17,6 +17,7 @@
#define RGBLED_OK 0
#define RGBLED_ERROR -1 // generic error
#define RGBLED_WRONG_CHANNEL -2 // channel number not in {1,2,3,4}
#define RGBLED_PRESCALER 0
#define RGBLED_PERIOD 255
@ -30,12 +31,22 @@ typedef struct {
// tim22 ch1 G
// tim22 ch2 R
// TODO union, aby se dalo nastavovat LED v cyklu (set_color)
TIM_TypeDef *timer_R;
TIM_TypeDef *timer_G;
TIM_TypeDef *timer_B;
uint8_t channel_R;
uint8_t channel_G;
uint8_t channel_B;
union {
struct {
TIM_TypeDef *R;
TIM_TypeDef *G;
TIM_TypeDef *B;
} timer;
TIM_TypeDef *timer_array[3];
};
union {
struct {
uint8_t R;
uint8_t G;
uint8_t B;
} channel;
uint8_t channel_array[3];
};
} rgbled_context_t;
@ -44,7 +55,7 @@ typedef struct {
*/
int8_t rgbled_init(rgbled_context_t *context);
int8_t rgbled_set_color(uint16_t R, uint16_t G, uint16_t B);
int8_t rgbled_set_color(uint8_t R, uint8_t G, uint8_t B);
int8_t rgbled_off(void);

View File

@ -217,18 +217,17 @@ int main(void)
/* RGB LED context init */
rgbled_context_t rgbled_context;
rgbled_context.timer_R = TIM22;
rgbled_context.timer_G = TIM22;
rgbled_context.timer_B = TIM2;
rgbled_context.channel_R = 2;
rgbled_context.channel_G = 1;
rgbled_context.channel_B = 1;
rgbled_context.timer.R = TIM22;
rgbled_context.timer.G = TIM22;
rgbled_context.timer.B = TIM2;
rgbled_context.channel.R = 2;
rgbled_context.channel.G = 1;
rgbled_context.channel.B = 1;
rgbled_init(&rgbled_context);
rgbled_set_color(255, 0, 0);
rgbled_set_color(50, 50, 0);
while (1) {
// rgbled_set_color(30000, 0, 0);
LL_mDelay(1000);
}

View File

@ -20,21 +20,37 @@ int8_t rgbled_init(rgbled_context_t *context)
return RGBLED_OK;
}
int8_t rgbled_set_color(uint16_t R, uint16_t G, uint16_t B)
int8_t rgbled_set_color(uint8_t R, uint8_t G, uint8_t B)
{
// tim2 ch1 B
// tim22 ch1 G
// tim22 ch2 R
if (
R > RGBLED_PERIOD || \
G > RGBLED_PERIOD || \
B > RGBLED_PERIOD)
{
return RGBLED_ERROR;
uint8_t colors[3];
uint8_t i;
colors[0] = R;
colors[1] = G;
colors[2] = B;
for (i = 0; i < 3; i++) {
if (colors[i] > RGBLED_PERIOD) {
return RGBLED_ERROR;
}
}
for (i = 0; i < 3; i++) {
switch (rgbled_context->channel_array[i]) {
case 1:
LL_TIM_OC_SetCompareCH1(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i]));
break;
case 2:
LL_TIM_OC_SetCompareCH2(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i]));
break;
case 3:
LL_TIM_OC_SetCompareCH3(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i]));
break;
case 4:
LL_TIM_OC_SetCompareCH4(rgbled_context->timer_array[i], (uint32_t)(RGBLED_PERIOD - colors[i]));
break;
default:
return RGBLED_WRONG_CHANNEL;
}
}
LL_TIM_OC_SetCompareCH1(rgbled_context->timer_B, (uint32_t)(RGBLED_PERIOD - B)); // B
LL_TIM_OC_SetCompareCH1(rgbled_context->timer_G, (uint32_t)(RGBLED_PERIOD - G)); // G
LL_TIM_OC_SetCompareCH2(rgbled_context->timer_R, (uint32_t)(RGBLED_PERIOD - R)); // R
return RGBLED_OK;
}

View File

@ -28,7 +28,7 @@
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.enable_shared_stlink" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.external_loader" value=""/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.external_loader_init" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.frequency" value="24000"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.frequency" value="8000"/>
<booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.halt_all_on_reset" value="false"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.log_file" value="/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Debug/st-link_gdbserver_log.txt"/>
<stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.low_power_debug" value="enable"/>