Ovládanie RGB LED pásika
Zo stránky SensorWiki
Záverečný projekt predmetu MIPS / LS2023 - Dominik Illés
Zadanie
Ovládanie RGB led pásika pomocou jednoduchého menu v serial linke
Literatúra:
Analýza a opis riešenia
Zapojenie LED pasíka je jednoduché len tri vodiče a v data vstupom som dal resistor, aby náhodne veľké prúdy nepoškodili pása. Najťažšia časť bola nastavid pás aby poslané farby vykresloval, na to som použil asembler kód čo som našiel na internete. Na ovládanie som napísal malú menu s tromi zvláštnymi módmi, používaním numpadu môžme navigovať cez módy prednastavené, manuálna nastavenia celého pása a manuálna nastavenia hocijakeho LEDa. Aby nevykreslilo vždy pod sebou text a vyzeralo lepšie som použil printf("\033[2J"); čo vymaže čo je na terminále a printf("\033[H"); aby kurzor bolo na začiatku.
Algoritmus a program
.
int main(void)
{
char ReceivedChar;
uart_init();
stdout = &mystdout;
uint16_t time = 0;
int m = 0;
while(1)
{
if(m==0){
printf("Menu\n 1.Nastavene efeky\n 2.Nastavit cely pas\n 3.Nastavit LEDky");
ReceivedChar = uart_getc();
m=1;
printf("\033[2J");
printf("\033[H");
}
//------------------------------------Predpisane effekty----------------------------------------------------------------
if(ReceivedChar == '1')
{
//--------------------------------Effekty-----------------------------
printf("Effekty\n 0.Vypnut\n 1.Red\n 2.Green\n 3.Blue\n 4.Rainbow-1\n 5.Rinbow-2\n 6.Breathing\n 7.Spat");
while(m==1)
{
ReceivedChar = uart_getc();
//------------------------------RED---------------------------------
if ( ReceivedChar == '1' )
{
RedStrip();
}
//------------------------------Green---------------------------------
if (ReceivedChar == '2')
{
GreenStrip();
}
//------------------------------Blue--------------------------------
if (ReceivedChar == '3')
{
BlueStrip();
}
//------------------------------Vypnut--------------------------------
if (ReceivedChar == '0')
{
OffStrip();
}
//------------------------------------RainbowV1---------------------------------
if (ReceivedChar == '4')
{
Rainbow1();
//-------------------------------------Rainbow-----------------------------------
}
if (ReceivedChar == '5') {
Rainbow2();
}
//----------------------------Breathing------------------------------------
if (ReceivedChar == '6')
{
Breathing();
}
//---------------------------------------Spat---------------------------------------
if (ReceivedChar == '7')
{
m=0;
printf("\033[2J");
printf("\033[H");
}
}
}
//-------------------------------Nastavenie celej pasy-------------------------------------------------------
if(ReceivedChar == '2')
{
fullLED(&m);
}
//-------------------------------Nastavenie zvlast ledky----------------------------------------------------------
if(ReceivedChar == '3')
{
pieceLED(&m);
}
}
return(0);
}
#define F_CPU 16000000
#define BAUD 9600
#include <avr/io.h>
#include <util/setbaud.h>
void uart_init( void )
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
//------------------------------------Uart Prikazy-------------------------------------------------
void uart_putc(char c)
{
if (c == '\n')
{
uart_putc('\r');
}
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
}
void uart_puts(const char *str) {
// Transmit a null-terminated string via UART
while (*str) {
// Wait for the UART data register to be ready for transmission
while (!(UCSR0A & (1 << UDRE0)));
// Transmit the character
UDR0 = *str++;
}
}
char uart_getc(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
void uart_gets(char *buffer, uint8_t max_length) {
uint8_t count = 0;
char received_char;
while (1) {
received_char = uart_getc();
// Check for newline or carriage return
if (received_char == '\n' || received_char == '\r') {
break;
}
// Append received character to the buffer
if (count < max_length - 1) {
buffer[count++] = received_char;
}
}
// Null-terminate the buffer
buffer[count] = '\0';
}
// Function to convert HSV to RGB
rgb_color hsv_to_rgb(uint8_t hue) {
uint8_t sector = hue / 42; // Divide the hue into 6 sectors (42 degrees each)
uint8_t remainder = (hue % 42) * 6; // Get the remainder and scale it to fit in a 8-bit range
uint8_t red = 0, green = 0, blue = 0;
switch (sector) {
case 0:
red = 255;
green = remainder;
break;
case 1:
red = 255 - remainder;
green = 255;
break;
case 2:
green = 255;
blue = remainder;
break;
case 3:
green = 255 - remainder;
blue = 255;
break;
case 4:
red = remainder;
blue = 255;
break;
case 5:
red = 255;
blue = 255 - remainder;
break;
}
rgb_color rgb = { red, green, blue };
return rgb;
}
Zdrojový kód: LEDovlad.c
Overenie
Video:
Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.