STAR Firmware by JonnyC - Source Code and Explanation

1335 posts / 0 new
Last post
wight
Offline
Last seen: 1 year 10 months ago
Joined: 11/27/2013 - 16:40
Posts: 4969
Location: Virginia, USA

Sounds like good work so far. Will you post your current code? If you don’t have hosting you can use http://pastebin.com (or any similar service) to share it with us.

Still fine, still on a break. One day I’ll catch up with you folks! previous wight catchup Wink
list of my drivers & variants (A17DD, FET+1 stuff, WIP stuff, etc)

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 1 year 7 months ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

Mike C wrote:
ToyKeeper wrote:
Thanks for testing this. I’ll probably be hurting for spare bytes when I start making changes, so every little bit helps.

I’ve been playing around a lot lately with optimizing for size and have managed to do a slimed down version of STAR 1.1. Mine basically does everything that STAR 1.1 does except check for soldered stars. I removed all those checks and did those selections by using a bunch of #IFDEF and #ELSE sequences. I saved quite a few bytes doing this. Personally I have no use for soldering stars, if I want to change the things the stars changed I’ll just flash a driver with the changes.

I also made a new delay loop by stealing a small section of code from delay_basic.h. My time resolution is not good (77ms) but for my purposes I can get the delays I want, I don’t need pin point accuracy.

Then I edited the voltage monitoring sequence quite a lot, removed a bunch of while loops and delay loops, and saved a total 278 bytes on the original STAR 1.1… That leaves me with plenty of room for playing around Smile

I’m no programmer though. I’ve tested the functionality with the help of a variable DC power supply, seems to be running fine but I guess I’ll see once it’s been built into a light.


Care to share your code?

Beautiful thing is…if you can trim in down with code tweaks, more functionality can be added on the back end
Maybe even put in hidden flashies or whatever

Mike C
Mike C's picture
Offline
Last seen: 10 hours 4 min ago
Joined: 01/22/2014 - 08:03
Posts: 2606
Location: Sweden

I made it easy on myself and uploaded it to my website, download it here: http://www.mikec.se/Stuff/MikeC01.zip

This current version is not as optimized as before as I have re-written a lot of it to make it easier for me to understand exactly what’s going on.

A few differences from STAR 1.1:
No stars, all is defined with #DEFINE and #IFDEF statements
Low voltage warning switches to next lower mode for about half sec, goes back up to current mode for half sec, then changes mode back to the next lower mode and stores it.
If already in lowest mode when low voltage (but not critical) is reached, nothing happens. I might do a one time blink just to let the user know if I have room for it later.
Critical voltage blinks between lowest mode and off 5 times before shutting down.
Re-wrote a bunch of stuff just to make it easier for me to understand what was going on and stripped out some stuff. For example my voltage monitoring routine doesn’t use any while loops.
The only delay-loops I use are for low and critical voltage routines. I got rid of STAR 1.1 _delay_ms at end of main loop. It looks like it actually prevents sleep mode between WDT interrupts as interrupts are about 0.5 seconds, but I never tested that. My low voltage routine skims through and goes into sleep mode between WDT interrupts expect during warning routines.
Re-organized a lot of comments and wrote plenty of my own.
My delayms() function uses assembler code from delay_basic.h. I have no idea what the assembler code does! I can loop it for desired delay times, that’s enough for me. I wrote a version of my delayms() that could take a millisecond value and accurately delay accordingly, but it cost more bytes. As I only use it for warning blinks I don’t need millisecond accuracy, free bytes are more valuable.
This version is 140 bytes smaller than STAR. Besides the way my warnings alert, the user should not notice any difference.

I use AVR Studio 5.1.208 it if matters.

I haven’t put it in a light yet, only run it in connected to a variable DC and LED, and switching modes with this DC has been a little bit of a pain. I’m no programmer, and know even less about programming chips. Anyone find any particular flaws, I am all ears! I’ve probably done a few things really stupid in there somewhere…

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

Mike C wrote:
I made it easy on myself and uploaded it to my website, download it here: http://www.mikec.se/Stuff/MikeC01.zip

I would like to collect the various flashlight firmwares and publish them in one place. This has no copyright or license assigned though, so it falls under the default terms: “Copyright © 2014 Mike C, All Rights Reserved.”

If the original STAR firmware didn’t specify any terms, it would have the same defaults, only with a different owner, which would make this derivative technically illegal. Not that anyone around here cares, but I try to make sure I avoid legal issues when possible.

Would it be okay to add an explicit copyright and license? I was thinking probably the GPL, the most popular free software license. It basically says anyone can do anything they want with the code, so long as they release code for any derivatives they make (also under the GPL), and don’t add external restrictions through patents. Basically, “I made this free-as-in-speech, and if you change it you must also make your version free-as-in-speech.” It does not prohibit selling the code or any products based on it.

Then I’ll be able to publish it along with other firmware, and anyone will be explicitly free to modify or use it.

Helios-
Helios-'s picture
Offline
Last seen: 2 years 1 month ago
Joined: 01/18/2012 - 21:12
Posts: 2099

GPLv3?


Counterfeit 18650s, 2,<a href=“http://

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

I’m not terribly picky about the license used, but I was figuring the current version of the GPL by default, unless there was reason to use something else. Depending on details, it might be more appropriate to go with an older GPL or LGPL or AGPL, or a BSD license, or Apache license, or … etc.

I’m open to suggestions, if the default isn’t agreeable.

Otherwise, I figured I’d probably host sources and binaries on Launchpad.net, with release files available to deliver builds to people who don’t want to use a DVCS to access the files. Github would work too, though I’m not as familiar with it and it isn’t quite as full-featured. However, git is quite a bit more popular so it might be a good choice even though I don’t like it as much. I could go for either one.

This explains some differences between the two DVCS tools involved:
http://toykeeper.net/tutorials/bzr-vs-git/

Their associated project management sites are far more different though, and it would take a much more in-depth article to compare them.

wight
Offline
Last seen: 1 year 10 months ago
Joined: 11/27/2013 - 16:40
Posts: 4969
Location: Virginia, USA

ToyKeeper wrote:
I’m not terribly picky about the license used, but I was figuring the current version of the GPL by default, unless there was reason to use something else. Depending on details, it might be more appropriate to go with an older GPL or LGPL or AGPL, or a BSD license, or Apache license, or … etc.

I’m open to suggestions, if the default isn’t agreeable.

Otherwise, I figured I’d probably host sources and binaries on Launchpad.net, with release files available to deliver builds to people who don’t want to use a DVCS to access the files. Github would work too, though I’m not as familiar with it and it isn’t quite as full-featured. However, git is quite a bit more popular so it might be a good choice even though I don’t like it as much. I could go for either one.

This explains some differences between the two DVCS tools involved:
http://toykeeper.net/tutorials/bzr-vs-git/

Their associated project management sites are far more different though, and it would take a much more in-depth article to compare them.

I think GPL v2 is the more widely accepted license. GPL v3 is the political/controversial one IIRC. I’m not sure that there is anything wrong w/ v2.

If we were voting (which clearly wouldn’t make a difference since it’s the original author(s)’s call) I’d suggest considering a more permissive license. Copyleft licences such as GPL are useful for encouraging folks to share their changes, but sometimes that turns into squabbles which don’t do anyone any good. Mud slinging & etc.

Still fine, still on a break. One day I’ll catch up with you folks! previous wight catchup Wink
list of my drivers & variants (A17DD, FET+1 stuff, WIP stuff, etc)

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

There is a lot of code which was licensed under “v2” instead of “v2 or later”, with copyrights owned by lots of different people (some of which aren’t alive any more), which means the project is stuck with that license forever. People can avoid the version lock-in with the “or later” clause, or avoid all re-licensing issues by assigning all copyrights to a trusted third party such as the FSF (and requiring contributors to do the same).

Otherwise, it’s a question of what people consider to be most free (is it okay to take without giving back?) and what kinds of legal trickery they want to protect against — for example, should we allow “tivo-ization”, which prevents users from running modified code? Like, if Blackshadow were to start using a version of the STAR firmware on their products, would it be okay for them to also make it refuse to run user-flashed firmware?

I don’t mind if different people decide on different licenses for their code; I’m mostly hoping that they’ll all allow republishing and derived works, so that we can all join in the fun. And if someone wants to share but doesn’t want to have to care about all the licensing details, I think GPL v2 with “or later” clause is a good default.

wight
Offline
Last seen: 1 year 10 months ago
Joined: 11/27/2013 - 16:40
Posts: 4969
Location: Virginia, USA

I don’t understand why anyone would want to use the “or later” part. I have only the most basic layman’s understanding of the reason people dislike v3. My understanding is that RMS added an anti-DRM skew. That is the only reason I’m aware of to avoid v3.

If anyone decided to license under v2 at this point in time I can only assume it would be specifically because they did not want v3. It seems that licensing under “v2 or later” would be opening the door to getting relicensed under v3 as others contribute.

Can you help me understand?

Still fine, still on a break. One day I’ll catch up with you folks! previous wight catchup Wink
list of my drivers & variants (A17DD, FET+1 stuff, WIP stuff, etc)

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

The “or later” clause means it’s legally compatible with both v2 and v3, and can be linked with source code from either version. The reason to include the “or later” clause is to maximize compatibility (and minimize drama) when you don’t specifically need the extra stuff in v3. It also helps that v3 is compatible with more non-GPL licenses, so the “or later” means it can be linked with Apache-licensed software too (for example).

wight
Offline
Last seen: 1 year 10 months ago
Joined: 11/27/2013 - 16:40
Posts: 4969
Location: Virginia, USA

Ah, shows what I know. I didn’t realize, or forgot (same thing), that just using / linking against a library required that you be licensed under GPL. Looking at the GNU licenses FAQ I see that now.

Still fine, still on a break. One day I’ll catch up with you folks! previous wight catchup Wink
list of my drivers & variants (A17DD, FET+1 stuff, WIP stuff, etc)

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 1 year 7 months ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

intellectual license and giving credit where credit is due, more or less leaving the writers name in the original source code is good enough for me

open source, community development copyleft

Mike C
Mike C's picture
Offline
Last seen: 10 hours 4 min ago
Joined: 01/22/2014 - 08:03
Posts: 2606
Location: Sweden

I’ve updated the zip file in the link ( http://www.mikec.se/Stuff/MikeC01.zip ) with a new C file that includes the same “Free to use, modify, and share for private use” as STAR v1.1. Is that OK? Otherwise feel free to write what I need to add to make it free for anyone to do as they please with it for private use and I’ll update the file on the link.

Any comments on what I’ve done with it? Any obvious blunders?

Also, does anyone know how to change the WDT interrupt time? It’s about 0.5 seconds default. I can’t see any obvious way of adjusting it. Not that it’s really needed but I would very much like to know how.

JonnyC
JonnyC's picture
Offline
Last seen: 9 months 3 days ago
Joined: 01/14/2011 - 19:12
Posts: 1148
Location: Green Bay, WI - USA

Mike C wrote:
I've updated the zip file in the link ( http://www.mikec.se/Stuff/MikeC01.zip ) with a new C file that includes the same "Free to use, modify, and share for private use" as STAR v1.1. Is that OK? Otherwise feel free to write what I need to add to make it free for anyone to do as they please with it for private use and I'll update the file on the link. Any comments on what I've done with it? Any obvious blunders? Also, does anyone know how to change the WDT interrupt time? It's about 0.5 seconds default. I can't see any obvious way of adjusting it. Not that it's really needed but I would very much like to know how.

Works for me.  On my site I say "use these programs however you wish".

Look at..

WDTCR = (1<<WDTIE) | (1<<WDP2) | (1<<WDP0); // Enable interrupt every 500ms

Download the datasheet for the Attiny13A from Atmel and just search for WDTCR and you'll see the different timing options.

Mike C
Mike C's picture
Offline
Last seen: 10 hours 4 min ago
Joined: 01/22/2014 - 08:03
Posts: 2606
Location: Sweden

JonnyC wrote:
Download the datasheet for the Attiny13A from Atmel and just search for WDTCR and you’ll see the different timing options.

Thanks, I’ll look into it.
Werner
Werner's picture
Offline
Last seen: 2 years 3 months ago
Joined: 10/19/2012 - 15:00
Posts: 3679
Location: Germany

Nice firmware, thanks for sharing it.
Just had a short look into the momentary firmware and found something which might be useful.
You only enter sleep mode if the light is switched off, I always try to use a sleepmode also in the other modes especially in the moonlight mode because if you use only some mA another some mA consumed by the controller arent perfect.

This could probably easy be added with a else in the main loop. This is what I use to make sure the idle mode is the standard.

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 1 year 7 months ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

Werner wrote:
Nice firmware, thanks for sharing it.
Just had a short look into the momentary firmware and found something which might be useful.
You only enter sleep mode if the light is switched off, I always try to use a sleepmode also in the other modes especially in the moonlight mode because if you use only some mA another some mA consumed by the controller arent perfect.

This could probably easy be added with a else in the main loop:

If mode==0 {…}….. … … else { set_sleep_mode(SLEEP_MODE_IDLE); //in every other mode just reduce current consumption sleep_mode(); ….

In luxdrv it shows

#define SLEEP asm volatile (“SLEEP”)

Then where you want SLEEP you just add SLEEP;

I believe…I really don’t know anything about C++

Werner
Werner's picture
Offline
Last seen: 2 years 3 months ago
Joined: 10/19/2012 - 15:00
Posts: 3679
Location: Germany

I like to write it like in the link, I set the sleep mode and then start the sleep mode. Its easy to read and i know which mode i used.
The controllers have different sleepmodes with different pro and contras so this is a specific ucontroller thing.
If that is wrong please correct me.

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 1 year 7 months ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

Werner wrote:
I like to write it like in the link, I set the sleep mode and then start the sleep mode. Its easy to read and i know which mode i used.

You are the coding master…always best to pay attention to those who know better Wink
Werner
Werner's picture
Offline
Last seen: 2 years 3 months ago
Joined: 10/19/2012 - 15:00
Posts: 3679
Location: Germany

I just tried the offtime memory. Awesome stuff. not tested it in a light but on a power supply it works perfect.
I used a 10uF cap and a CAP_TRESHHOLD value of 200.
The batteryprotection kicked in abit to low for my feeling so I bumped the criticallevel a bit.
I like it very much that you have added a level were the power is totally down. THUMBS UP!
I just added a line to disable the ADC, you already have a procedure in the code. This helps to reduce the current a lot. You even use the idle mode as a standard.
No memory works like a champ too. Fantastic.

I have a question because there is something which confuses me
What does this line: while (!low_voltage(ADC_CRIT));
its in the main routine.

_the_
_the_'s picture
Offline
Last seen: 2 days 21 hours ago
Joined: 07/08/2011 - 06:22
Posts: 3647
Location: Finland

Werner wrote:
I have a question because there is something which confuses me What does this line: while (!low_voltage(ADC_CRIT)); its in the main routine.

It (busy)loops until voltage is critically low.

I would put the semicolon to separate line to improve readability. Smile

=the=

 

JonnyC
JonnyC's picture
Offline
Last seen: 9 months 3 days ago
Joined: 01/14/2011 - 19:12
Posts: 1148
Location: Green Bay, WI - USA

Yup, _the_ has it right.  Glad it's working for you!

I posted up a dual switch program on my site (for rear clicky + side switch), but I haven't fully tested it out yet and RMM hasn't given it his normal runthrough either.  Once it's tested I will post up and we can see what additions can be added if needed.

Werner
Werner's picture
Offline
Last seen: 2 years 3 months ago
Joined: 10/19/2012 - 15:00
Posts: 3679
Location: Germany

I flashed the momentary firmware last night on a nanjg, it works quite well.
Never played with the turbo timer but thought for a small light it might be usefull.
I can read the code quite well and it is a simple but very nice code.
I changed it a bit so that it slowly dims down to previous mode and not so harsh and I also dim down on the two highest modes because the half power mode is on 3rd place. But because I thought that dimming is just for pussies I dim down after 5minutes but this is to long to prevent finger burning….5minutes on 4.4A and again 5 with 3A are to much…maybe I will shorten that a bit after a few days of playing with it.

I used a sipik ck12 clone and a 4.4A nanjg.

wight
Offline
Last seen: 1 year 10 months ago
Joined: 11/27/2013 - 16:40
Posts: 4969
Location: Virginia, USA

I've started work on putting an ATtiny13A on a LD29 driver board.  Thread link: here is the LD-29 MCU pinout (eg how to put your ATtiny13A in there...).  An adapter PCB is pictured in post #27 which will allow the ATtiny to pickup the right pins for power, ground, ADC inputs, etc.  

I've also started work on the changes to get STAR_offtime working with this, see post #34.  As I posted the code I noticed one glaring mistake (STAR2_PIN should have been removed), but I welcome any review to find additional mistakes.

Still fine, still on a break. One day I’ll catch up with you folks! previous wight catchup Wink
list of my drivers & variants (A17DD, FET+1 stuff, WIP stuff, etc)

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

FWIW, I’ve started a firmware repository and I added a new STAR flavor designed for use as a bike tail light.

The repository currently only contains STAR and a couple derivatives, but I’ll be adding anything and everything I can get permission to share.

The biking tail light code will go into a little 1×16340 light with red XP-E2 as soon as I get the host. It has the following modes: moon, low, med, high, moon-low flasher, low-med flasher, med-high flasher, and heartbeat. It’s my first attiny firmware and I only spent a couple hours on it so far, so it’s a work in progress. However, it does at least work if anyone is interested.

The firmware repository is on Launchpad: https://launchpad.net/flashlight-firmware
Here’s the link if you’d like to browse the code online.

The tail light .hex file is also pre-built for easy flashing: here.

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 1 year 7 months ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

Nice thanks!!

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

BTW, I just wanted to thank everyone who has contributed to the STAR firmware.

When I was little, I learned how to read from a programming manual. I learned about variables and syntax errors before I ever heard of Dick & Jane or an Etch-A-Sketch. As long as I can remember, I’ve been putting code into a computer to make it do what I want, and my current computers have their screens filled up almost entirely with stuff I created. It’s just how I do things.

But with flashlights, something was always missing. I’ve really enjoyed them, and modding lights made it even more fun, but there were always little things I still wanted to change…

Until the past couple days, that is.

Today I took apart one of my lights, wrote some new firmware for it, flashed it, tweaked it, put it all back together, and now it’s finally doing what I wanted all along — namely, whatever I told it to do. Its behavior may have been just thrown together quickly based on what I wanted from it today, but that’s okay. It feels right. That’s how things are supposed to be. It’s running custom code which didn’t exist this morning, a crystalized snapshot of my ideas, dumped out of my brain so I could get rid of them and make room for new ideas.

Is it “finished”? Probably not. I’ll probably reflash it a few dozen more times in the foreseeable future. But that’s okay too, code is fluid and changes with circumstance.

As for STAR, I’m thankful because other people already did all the hard parts. It’s a great basis for derivative projects. I didn’t have to spend all week referring to the encyclopedic attiny13a manual to figure out basic things like using the watchdog timer to implement short-cycle memory, or painstakingly measure the values for low-voltage detection. I got to do the fun parts instead, designing modes and watching them in action just moments later. So thank you! I’m having a great time and making all sorts of terrible blinky things.

JonnyC
JonnyC's picture
Offline
Last seen: 9 months 3 days ago
Joined: 01/14/2011 - 19:12
Posts: 1148
Location: Green Bay, WI - USA

Awesome ToyKeeper!  Thanks for sharing your experience.  It's fun stuff!  I'm at the point now where my lights do what I want them to with a basic 3-mode with off-time.  I'm really done tinkering with new versions of the code, programmability, flashy modes, etc.  But this is great because I can now focus on other things.

Werner
Werner's picture
Offline
Last seen: 2 years 3 months ago
Joined: 10/19/2012 - 15:00
Posts: 3679
Location: Germany

Toykeeper could you please explain a bit what this is?
Can I send you a modified STAR Firmware and you will add them in this STAR Encyclopedia or what?

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 20 hours 37 min ago
Joined: 01/12/2013 - 14:40
Posts: 10829
Location: (469219) 2016 HO3 // I get way more privmsgs than I can respond to, so please ask in a public thread if possible, for a faster answer.

Werner, I’m basically just applying some of the most common “best practices” from the software world and applying them to flashlight firmware. I’m hoping it will encourage more collaboration.

In general, open-source software projects organize their activities around a project management service such as Launchpad or GitHub. This provides a central hub for storing and publishing source code and other project files, along with a system for keeping old revisions and managing code patches. It also provides bug tracking or issue tracking, along with a variety of other optional services like mailing lists or documentation systems. Plus, it’s just nice having everything in one place.

So, you could send me a modified version and I could add it to the repository for you. Or you could branch the repo, add your improvements, publish your own branch, and share it. Or submit a merge proposal to get it back into the main repository. Or you could fork the whole thing complete with history and start your own project if you don’t like the way it’s being managed.

A lot of the features are probably more than we really need, but it’s nice to have room to grow. For example, I doubt we’ll need to bisect the revision history to figure out exactly where a bug was introduced in a firmware’s code, but stuff like that is available if anyone needs it.

Pages