Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated to RGBW mode #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

mnemocron
Copy link

  • select RGB / RGBW
  • added lots of documentation
  • updated SPL library files to newest version
  • updated main.c with documentation
  • updated main.c with gpio init()

Thank you for the great library.
I changed a few things and added a way to select between RGB and RGBW type LEDs.
This pull request is to let you know that there are improvements that you might want to copy.
I tested the code on STM8S and STM8L MCUs - but did not run your exact example project.

- select RGB / RGBW
- added lots of documentation
- updated SPL library files
- updated main.c with documentation
- updated main.c with gpio init()
@j3qq4hch
Copy link
Owner

Wow, this is my first PR from a stranger. Thank you for your effort, I will surely check this out.

@mnemocron
Copy link
Author

No hurry. Just wanted to let you know :)
Not particularly a "good" merge request from me tbh.
Since it has so many changes - and because I moved the library files to src / inc folders which made git loose track of them.
You can also manually copy parts from my fork if you find something useful.

@j3qq4hch
Copy link
Owner

You know, I am thinking about removing SPL from this repo at all. Its not about SPL after all. SPL is here only because I don't like messing with registers directly (and keeping SPL here is stupid after all).
I see, you have noticed, that I had to redefine all the GPIO ports definitions somewhere inside WS2812 driver. This was done only to avoid mentioning SPL inside the lib itself (but then I needed it for some reason anyway)...
So SPL is here only for example project to work. I wonder, how can I avoid it...

@mixaz
Copy link

mixaz commented Nov 16, 2021

I would be good to get rid of IAR Workbench too - it is not opensource, it is not free and it is for MS Windows only ((
I see someone tried to port the library to SDCC https://github.com/jaseg/stm8_ws2811 but there's a bit of mess in example code (seemed he worked on a flame effect, that is an interesting topic by itself)

@mixaz
Copy link

mixaz commented Nov 16, 2021

I tried to compile @mnemocron's fork with SDCC but failed because of differences in IAR and SDCC compilers, looking at it now. @mnemocron - it is not good to pass structures by values especially in weak MCU cases. Usually it is just a waste of CPU instructions. And SDCC doesn't support it )

@mnemocron
Copy link
Author

mnemocron commented Nov 16, 2021

I see someone tried to port the library to SDCC

I too discovered this repo just recently. I appreciate the effort but at the current stage it is just not productive.
So I don't see myself porting this anytime soon.

it is not good to pass structures by values especially in weak MCU cases.

@mixaz are you talking about the RGBColor_t type?
Under normal circumstances I would agree that it is inefficient. But in this case it is 3 or 4 bytes, not more than a single uint32_t.
But if SDCC is incompatible with this it makes sense to change it to a pointer.

@mixaz
Copy link

mixaz commented Nov 16, 2021

@mnemocron, I understand that porting to SDCC is not a goal for you, that's OK. I have to work on it myself, if I need it. And I value your efforts in the PR code, actually.
I see now, RGBColor_t comes original code, not yours. I beg your pardon )


/* Includes ------------------------------------------------------------------*/
#include "ws2812b_LLD.h"
#include "stm8l15x.h"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stm8l15x.h is not needed

so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */

/* Time base configuration */
TIM4_TimeBaseInit(TIM4_Prescaler_128, TIM4_PERIOD);
Copy link

@mixaz mixaz Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIM4_PRESCALER_128, not TIM4_Prescaler_128. The same for TIM4_FLAG_Update, TIM4_IT_Update (sorry for being sourish - just spent almost a day and started losing my hair on this)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIM4_PRESCALER_128, not TIM4_Prescaler_128. The same for TIM4_FLAG_Update, TIM4_IT_Update (sorry for being sourish - just spent almost a day and started losing my hair on this)

Do we use the same SPL? I noticed, that ST was using inconsistent naming between the STM8S and STM8L libraries. Could it be that they changed it now?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIM4_PRESCALER_128 = ((uint8_t)0x07)

*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include "stm8s_conf.h"
#include "ws2812B_fx.h"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ws2812b_fx.h not ws2812B_fx.h - mind caps in file names. Doesn't compile on Unixes for this reason

static void CLK_Config(void);
static void TIM4_Config(void);
static void GPIO_Config(void);
void _delay_ms(u16);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u16 is not standard. Use uint16_t instead

z = 0;
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
}
uptime++;
Copy link

@mixaz mixaz Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static volatile uint16_t delay_time = 0;

void uptime_routine(void)
{
    if(delay_time)
        delay_time--;
}

void _delay_ms(uint16_t wait)
{
    delay_time = wait;
    while(delay_time){}
}


/* ---------------------------------------------------------------------------*/
/* GPIO Port Register Map */
/** @todo why not use SPL definitions? */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPL is STM library, there are other toolchains as well. It's better to not have dependencies on toolchains, and if you have them, place to separate file

LedsArray[LedId * COL_PER_LED +1] = Color.R;
LedsArray[LedId * COL_PER_LED +2] = Color.B;
LedsArray[LedId * COL_PER_LED +3] = Color.W;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be under WS2812B_MODE_RGB ifdef

// depending on RGBW or RGB type it is 3 or 4 bytes to manage
#ifdef WS2812B_MODE_RGBW
#define COL_PER_LED (4)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof() could be used

#endif

#ifdef WS2812B_MODE_RGB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WS2812B_MODE_RGBW and WS2812B_MODE_RGB can be disabled both, that's not OK. I would use something like #define WS2812B_MODE WS2812B_MODE_RGBW and a check for incorrect value

Comment on lines +25 to +31
const RGBColor_t GREEN = { 0, 255, 0, 0};
const RGBColor_t BLUE = { 0, 0, 255, 0};
const RGBColor_t YELLOW = {255, 255, 0, 0};
const RGBColor_t MAGENTA = {255, 0, 255, 0};
const RGBColor_t BLACK = { 0, 0, 0, 0};
const RGBColor_t WHITE = {255, 255, 255, 0};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLD file is about dealing with WS2812 protocol and hardware. Colors definitions should be placed to separate file then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now you are reviewing the original source code, not the changes in the my PR.
Thanks a lot, I am learning a lot today :)
While we are at it, can you recommend any literature on "good practices in embedded software design"?
Where you learn e.g.

  • which code to separate into files / libraries
  • generally how to build robust ifdef structures
  • when to use/avoid certain data types

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I write comments because I spent quite a bit on this project, and I'm glad to share knowledge. I think you don't need to correct your PR, I'm going to make the changes myself. About literature - I'm an old guy, so books I read decades ago, may be not good for you )
These matters are somewhat advanced and you get it with experience. Reading others code, etc. Sorry I don't know any modern C/C++ book for embedded. I often work with Linux source code which is almost all plain C and some C++. But Linux is too complex for you (and me)), but keep your eye on RTOS (FreeRTOS, works for example on STM32, and STM supports it officially. It is part of Expressiv SDK ESP32 BTW).
You're probably working with baremetal code, next step is RTOS when you'll get out of sandbox.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the feedback! We have briefly touched Linux and RTOS in school where the focus was on the advantages and the new special mechanisms (threads, semaphores, mutexes etc.) but never about how to organize code in a project. So I have my own style in STM32 projects but never quite know if it is good or bad practice ¯_(ツ)_/¯

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually my own code style is far from ideal too. Ability for learning is the greatest skill I know)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found this STM8 port of FastLED library: https://github.com/thielj/FastLED-STM8, I'm going to try it. Seems to be using IAR compiler

Comment on lines +46 to +48
unsigned int nbLedsBytes = NB_LEDS * COL_PER_LED;
extern unsigned char NodeId;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to use sized types - uint8_t and uint16_t in this case. int and char is almost obsolete in modern C coding, they are used when size doesn't matter. Not in this case when the data is shared with ASM code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants