目录
文章介绍
当时想到的一个还算有点儿意思的功能,如果是个人DIY的话感觉还算是比较炫的效果。关键是如何实现随机,每次几个灯随机颜色不能重复,存在重复就实际亮的灯数就不够了,我认为这是最核心的部分。
当时对动态内存分配有点儿兴趣,因为咱没用过。就在代码中添加这部分功能,可使用静态数组,也可选择动态内存分配。
但后来了解到动态内存需要有动态内存管理算法的配合使用,电脑上是有的,但单片机上就很可能没有了,动态内存管理需要自己实现,比如我前段时间写的:链栈的应用---内存池
我认为核心部分的代码,是我在网上看到的,不知道这算不算一种惰性,有一种想法先去网上搜搜资料,有现成的直接用或者稍微改改再用。我给自己的安慰:并不是目标重要还是什么重要,我的想法实现就是我的目标,如果时间太长估计自己都不想搞下去了,除非我有坚韧不拔的毅力。
核心代码部分
/**
* find
* @brief 与数组a中的前size个数进行比较,若有与key相同的,则返回0,若没有则返回1
* @param a 数组元素首地址
* @param size 数组元素个数
* @param key 查找对比项
*/
uint8_t
find(uint8_t *a,uint8_t size, int key)
{
for(uint8_t i=0; i < size; i++)
{
if(a[i] == key)
{
return 0;
}
}
return 1;
}
/**
* get_random_num_mutexes
* @brief 得到num个互不相同的数,覆盖掉数组中原有的数值
* @param array 数组元素首地址
* @num num 数组元素个数
* @param limit_val 随机数限制范围
* @example 要得到42之内的6个随机数,写入到数组random_array
* @example get_random_num_mutexes(random_array, 6, 42)
*/
void
get_random_num_mutexes(uint8_t *array,uint8_t num, uint8_t limit_val)
{
uint8_t random_val;
for(uint8_t i = 0; i < num; i++)
{
random_val = rand() % limit_val;
while(! find(array, i + 1, random_val))
{
random_val = rand() % limit_val;
}
array[i] = random_val;
}
}
总体代码部分
因涉及到清屏和延时,需要 调用windows.h ,因此无法使用菜鸟C在线工具运行。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define DEBUG_USE_MALLOC 1
#define RANDOM_NUM 2
#define RANDOM_TIMES 10
#if !DEBUG_USE_MALLOC
uint8_t random_rgb_array[RANDOM_NUM];
uint8_t random_color_array[RANDOM_NUM];
#endif
enum rgb_color
{
NO_COLOR = 0,
RED,
YELLOW,
GREEN,
CHING,
BLUE,
PURPLE,
WHITE,
};
void rgb_all_no_display_invoke(void);
void led_1_set_color(uint8_t color);
void led_2_set_color(uint8_t color);
void led_3_set_color(uint8_t color);
void led_4_set_color(uint8_t color);
void led_5_set_color(uint8_t color);
void led_6_set_color(uint8_t color);
void rgb_led_on_choose(uint8_t choose_rgb,uint8_t set_color);
uint8_t find(uint8_t *a,uint8_t size, int key);
void get_random_num_mutexes(uint8_t *array,uint8_t num, uint8_t limit_val);
void random_led_callback(void);
/**
*
* @brief 随机灯亮RANDOM_TIMES 次,每次间隔1秒,通过cmd命令窗口实现
* @retval int 返回值描述: 无
*/
int main(void)
{
srand((unsigned)time(NULL));
for(uint8_t i=0; i<RANDOM_TIMES; i++)
{
random_led_callback();
Sleep(1000);
}
return 0;
}
/**
* rgb_all_no_display_invoke
* @brief 清除上次亮的LED
*/
void
rgb_all_no_display_invoke(void)
{
system("cls");
}
/**
* rgb_led_on_choose
* @brief 选择LED来点亮
* @param choose_rgb 有1~6个RGB灯供选择
* @param set_color 每个RGB灯有7种颜色供选择
*/
void
rgb_led_on_choose(uint8_t choose_rgb,uint8_t set_color)
{
switch(choose_rgb)
{
case 1: led_1_set_color(set_color); break;
case 2: led_2_set_color(set_color); break;
case 3: led_3_set_color(set_color); break;
case 4: led_6_set_color(set_color); break;
case 5: led_5_set_color(set_color); break;
case 6: led_4_set_color(set_color); break;
default: break;
}
}
/**
* find
* @brief 与数组a中的前size个数进行比较,若有与key相同的,则返回0,若没有则返回1
* @param a 数组元素首地址
* @param size 数组元素个数
* @param key 查找对比项
*/
uint8_t
find(uint8_t *a,uint8_t size, int key)
{
for(uint8_t i=0; i < size; i++)
{
if(a[i] == key)
{
return 0;
}
}
return 1;
}
/**
* get_random_num_mutexes
* @brief 得到num个互不相同的数,覆盖掉数组中原有的数值
* @param array 数组元素首地址
* @num num 数组元素个数
* @param limit_val 随机数限制范围
* @example 要得到42之内的6个随机数,写入到数组random_array
* @example get_random_num_mutexes(random_array, 6, 42)
*/
void
get_random_num_mutexes(uint8_t *array,uint8_t num, uint8_t limit_val)
{
uint8_t random_val;
for(uint8_t i = 0; i < num; i++)
{
random_val = rand() % limit_val;
while(! find(array, i + 1, random_val))
{
random_val = rand() % limit_val;
}
array[i] = random_val;
}
}
/**
* random_led_callback
* @brief 随机灯亮函数回调
* @note 要想显示random_num个灯,需要获得random_num互不相同的数
* @note 产品考虑:为亮灭切换间隔短一些,得到新的随机数之后再灭灯
*/
void
random_led_callback(void)
{
uint8_t set_color;
uint8_t choose_led;
#if DEBUG_USE_MALLOC
uint8_t *random_rgb_array = NULL;
uint8_t *random_color_array = NULL;
random_rgb_array = (uint8_t*)malloc(RANDOM_NUM * sizeof(uint8_t));
if(random_rgb_array == NULL)
{
printf("random_rgb_array : no memory space allocated! \n");
return;
}
random_color_array = (uint8_t*)malloc(RANDOM_NUM * sizeof(uint8_t));
if(random_color_array == NULL)
{
printf("random_color_array : no memory space allocated! \n");
free(random_rgb_array);
return;
}
#endif
get_random_num_mutexes(random_rgb_array, RANDOM_NUM, 6);
get_random_num_mutexes(random_color_array, RANDOM_NUM, 7);
rgb_all_no_display_invoke();
for(uint8_t i=0; i < RANDOM_NUM; i++)
{
choose_led = (random_rgb_array[i]) + 1;
set_color = (random_color_array[i]) + 1;
rgb_led_on_choose(choose_led, set_color);
}
#if DEBUG_USE_MALLOC
free(random_rgb_array);
free(random_color_array);
random_rgb_array = NULL;
random_color_array = NULL;
#endif
}
/**
* led_1_set_color
* RGB1 设置颜色
*/
void led_1_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB1 display color is : RED \n");
break;
case GREEN:
printf("RGB1 display color is : GREEN \n");
break;
case BLUE:
printf("RGB1 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB1 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB1 display color is : PURPLE \n");
break;
case CHING:
printf("RGB1 display color is : CHING \n");
break;
case WHITE:
printf("RGB1 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB1 display color is : NO_COLOR \n");
break;
default: break;
}
}
/**
* led_2_set_color
* RGB2 设置颜色
*/
void led_2_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB2 display color is : RED \n");
break;
case GREEN:
printf("RGB2 display color is : GREEN \n");
break;
case BLUE:
printf("RGB2 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB2 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB2 display color is : PURPLE \n");
break;
case CHING:
printf("RGB2 display color is : CHING \n");
break;
case WHITE:
printf("RGB2 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB2 display color is : NO_COLOR \n");
break;
default: break;
}
}
/**
* led_3_set_color
* RGB3 设置颜色
*/
void led_3_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB3 display color is : RED \n");
break;
case GREEN:
printf("RGB3 display color is : GREEN \n");
break;
case BLUE:
printf("RGB3 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB3 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB3 display color is : PURPLE \n");
break;
case CHING:
printf("RGB3 display color is : CHING \n");
break;
case WHITE:
printf("RGB3 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB3 display color is : NO_COLOR \n");
break;
default: break;
}
}
/**
* led_4_set_color
* RGB4 设置颜色
*/
void led_4_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB4 display color is : RED \n");
break;
case GREEN:
printf("RGB4 display color is : GREEN \n");
break;
case BLUE:
printf("RGB4 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB4 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB4 display color is : PURPLE \n");
break;
case CHING:
printf("RGB4 display color is : CHING \n");
break;
case WHITE:
printf("RGB4 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB4 display color is : NO_COLOR \n");
break;
default: break;
}
}
/**
* led_5_set_color
* RGB5 设置颜色
*/
void led_5_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB5 display color is : RED \n");
break;
case GREEN:
printf("RGB5 display color is : GREEN \n");
break;
case BLUE:
printf("RGB5 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB5 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB5 display color is : PURPLE \n");
break;
case CHING:
printf("RGB5 display color is : CHING \n");
break;
case WHITE:
printf("RGB5 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB5 display color is : NO_COLOR \n");
break;
default: break;
}
}
/**
* led_6_set_color
* RGB6 设置颜色
*/
void led_6_set_color(uint8_t color)
{
switch(color)
{
case RED:
printf("RGB6 display color is : RED \n");
break;
case GREEN:
printf("RGB6 display color is : GREEN \n");
break;
case BLUE:
printf("RGB6 display color is : BLUE \n");
break;
case YELLOW:
printf("RGB6 display color is : YELLOW \n");
break;
case PURPLE:
printf("RGB6 display color is : PURPLE \n");
break;
case CHING:
printf("RGB6 display color is : CHING \n");
break;
case WHITE:
printf("RGB6 display color is : WHITE \n");
break;
case NO_COLOR:
printf("RGB6 display color is : NO_COLOR \n");
break;
default: break;
}
}