STAR Firmware by JonnyC - Source Code and Explanation

1335 posts / 0 new
Last post
ToyKeeper
ToyKeeper's picture
Offline
Last seen: 44 min 59 sec ago
Joined: 01/12/2013 - 14:40
Posts: 10741
Location: (469219) 2016 HO3

Yes, the attiny13a can do two PWM channels at once, but they share a loop counter. See the “dual PWM” section of the repository index file for a list of projects which support this, as examples of how to do it. The shared loop counter goes from 0 to 255 and each PWM channel has its own ceiling so you can control the output of each independently.

Serp
Serp's picture
Offline
Last seen: 7 months 3 days ago
Joined: 01/14/2014 - 11:29
Posts: 626
Location: Ukraine

100Hz & 8kHz at the same time?

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 44 min 59 sec ago
Joined: 01/12/2013 - 14:40
Posts: 10741
Location: (469219) 2016 HO3

Serp wrote:
100Hz & 8kHz at the same time?

If you really want that, it’s technically possible… but the 100Hz part wouldn’t use the onboard PWM hardware. You’d have to set the 8kHz level to run automatically, then turn the 100Hz channel on and off manually with the MCU.

If you limit things to only what the dedicated PWM hardware can do, both channels must run at the same frequency. The difference between them is their duty cycle… so one can run at 100% and the other channel at 8%.

finges
Offline
Last seen: 3 days 22 hours ago
Joined: 11/19/2014 - 14:50
Posts: 504
Location: Germany

I am using STAR_dual_switch in the Yezel Y3. It has a rear clicky and a electronic side switch. So I can switch the light on with the rear clicky and than cycle trough the modes using the side switch. It has mode memory, so you can use the rear clicky to switch the light off and on and it stays in the chosen mode. It is good for signaling or morse code for example.

Now I would like to ask if this firmware can be updated with dual-PWM output because I would like to switch the driver to a FET+7135 driver.

bob808
Offline
Last seen: 3 years 2 months ago
Joined: 05/09/2015 - 07:44
Posts: 16

Hi guys,
I need to modify something in the software so I figured I ask here.

I have a headlamp with a XM-L led. The driver is on the back of the head in the battery case.
The lamp has a momentary switch on the back of the led housing. This way there are 3 wires running from the led to the battery case. When I press the button the 3rd wire (the control) goes to LED ground thus pulling low the control pin on the controller.
Now, stock driver is crap and I designed my own using 9xAMC7135 and attiny.
The issue is that AMC7135 is on the – side of the LED thus the ground being unconnected when the light is off. This means that I have no way of pulling the controller leg low.
So now I have two options. Replace (once again) the wiring going to the LED and another wire for a total of 4, or modifying the software so I can pull the pin high on the controller. The LED+ wire is live all the time and I can mod the internal connections of the button.
What would I need to modify in the firmware to pull the pin high instead of low?
After some breadboard tests I found that pin 2 (PB3) must be pulled low with stock firmware.
Thank you.

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 44 min 59 sec ago
Joined: 01/12/2013 - 14:40
Posts: 10741
Location: (469219) 2016 HO3

Er, I’m not entirely sure if you just answered your own question or not.

The attiny can tell you if a pin is grounded or not, and you can reverse the interpretation of that if necessary… I think it just requires switching some 1s and 0s or putting a logical “not” operator in a few lines. I can’t really answer much about the hardware level of the issue though.

bob808
Offline
Last seen: 3 years 2 months ago
Joined: 05/09/2015 - 07:44
Posts: 16

ToyKeeper wrote:
Er, I’m not entirely sure if you just answered your own question or not.

The attiny can tell you if a pin is grounded or not, and you can reverse the interpretation of that if necessary… I think it just requires switching some 1s and 0s or putting a logical “not” operator in a few lines. I can’t really answer much about the hardware level of the issue though.

For me it would be easier to just edit the code and choose pull-up instead of pull-down as a matter of state detection.
That way I would not need to mess with the wires. But I provisioned my pcb for both cases where I added an extra resistor in line with the button wire.

I’d like to know where and how should I alter the code for what I need.

Mike C
Mike C's picture
Offline
Last seen: 6 days 18 hours ago
Joined: 01/22/2014 - 08:03
Posts: 2578
Location: Sweden

DIDR0 |= (1<<ADC3D); // Disables digital input on PB3. Not really needed, but saves power.

DDRB |= (1 << PB3); // Sets PB3 as output.
PORTB |= (1 << PB3); // Sets PB3 high.

bob808
Offline
Last seen: 3 years 2 months ago
Joined: 05/09/2015 - 07:44
Posts: 16

Great, thank you!
I will test it tonight when I get home.

Mike C
Mike C's picture
Offline
Last seen: 6 days 18 hours ago
Joined: 01/22/2014 - 08:03
Posts: 2578
Location: Sweden

That was configuring the pin as output and setting it high. It might be worth trying to keep it as input but activate the pull-up resistor.

From the datasheet: “If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.”
To do that you just use the last line in my above example because the initial DDRB values are already 0:

PORTB |= (1 << PB3);

I haven’t use IOs the way you need to in this case so I can’t say it will work. At least you have a couple of things to try.

bob808
Offline
Last seen: 3 years 2 months ago
Joined: 05/09/2015 - 07:44
Posts: 16

Mike C wrote:
That was configuring the pin as output and setting it high. It might be worth trying to keep it as input but activate the pull-up resistor.

From the datasheet: “If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated.”
To do that you just use the last line in my above example because the initial DDRB values are already 0:

PORTB |= (1 << PB3);

I haven’t use IOs the way you need to in this case so I can’t say it will work. At least you have a couple of things to try.

I tried everything I could and it’s not working.
So here’s the relevant bit of code:

Quote:

int is_pressed()
{

static uint8_t pressed = 0;

// Keep track of last switch values polled static uint8_t buffer = 0×00;

// Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);

if (pressed) {

// Need to look for a release indicator by seeing if the last switch status has been 0 for n number of polls
pressed = (buffer & DB_REL_DUR); }
else {
// Need to look for pressed indicator by seeing if the last switch status was 1 for n number of polls pressed = ((buffer & DB_PRES_DUR) == DB_PRES_DUR); }

return pressed;
}
#else
int is_pressed()
{

// Keep track of last switch values polled static uint8_t buffer = 0×00;

// Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);

return (buffer & DB_REL_DUR);
}

Couldn’t make it work.
It says “0 being low for pressed, 1 for pulled-up for released”. 0 is pressed in default config. So I need 1 for pressed.
The pull-up resistor is high because:

Quote:
// Set all ports to input, and turn pull-up resistors on for the inputs we are using DDRB = 0×00; PORTB = (1 << SWITCH_PIN) | (1 << STAR3_PIN);

So I don’t need to do it.
Now, there’s something with the underlined bit of code. I need to reverse the way it registers the push signal. From low to high.

Mike C
Mike C's picture
Offline
Last seen: 6 days 18 hours ago
Joined: 01/22/2014 - 08:03
Posts: 2578
Location: Sweden

bob808 wrote:
I need to reverse the way it registers the push signal. From low to high.

I don’t know if or how that can be done.
Crux
Crux's picture
Offline
Last seen: 6 hours 11 min ago
Joined: 05/03/2011 - 16:27
Posts: 227
Location: Northcoast, Ohio, USA

You need to add a pull-down resistor to keep the line normally low, then pressing the switch will pull the line high. Any value from 1k to 100k can be used if you disable the pull-up resistor on that input. But using a 1k ohm pull-down will overcome the internal pull-up even if not disabled (at the expense of a fraction of a Milli-amp of extra current draw, this could be an issue with e-switch lights). It is important that the positive side of the switch is the same voltage as the controller IC. If the voltage is higher, the IC may be damaged.

I'm not sure about the code, firmware wigs me out, I'm always off by one...

First try adding the pull-down resistor, then revisit the code, it just may work!

Edit: Put a meter on that line and verify it goes from a zero to a one when the switch is pressed.

Keep us posted.

nofear87
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/07/2015 - 05:40
Posts: 114

Where I can find the “optimize for size”-option in Atmel Studio? At the moment my hex file is to large for the Attiny13A Sad

seasam
Offline
Last seen: 5 years 2 months ago
Joined: 01/31/2015 - 13:15
Posts: 189
Location: GA, USA
nofear87 wrote:
Where I can find the “optimize for size”-option in Atmel Studio? At the moment my hex file is to large for the Attiny13A Sad

On the File menu at the top of your screen click “Project”, click the bottom selection “(project name) Properties”, click “Toolchain” on the left, and look for “Optimization”. Optimization level set to -Os Optimize for size.

vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

Hi all,

Is the clickie version of this firmware default to use on time or off time memory?

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 44 min 59 sec ago
Joined: 01/12/2013 - 14:40
Posts: 10741
Location: (469219) 2016 HO3

vestureofblood wrote:

Hi all,

Is the clickie version of this firmware default to use on time or off time memory?


The basic clicky STAR uses on-time. STAR_noinit uses fixed offtime via a memory decay trick. Or STAR_offtime uses configurable offtime via an offtime capacitor. Take your pick.
vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

Ok, for the STAR_noinit.   You say this uses a "decay trick".    Does that mean that I can use that with a nanjg 105C and it will give the board off time memory?

 

I have a version of wights single sided 17mm FET DD board I would like to use it on.   It has an OTC with a value of 1uf.     I am guessing the STAR_offtime would be the way to go with that?

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

_the_
_the_'s picture
Offline
Last seen: 3 weeks 2 days ago
Joined: 07/08/2011 - 06:22
Posts: 3646
Location: Finland

Yes & yes. You got it right. Smile

=the=

 

nofear87
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/07/2015 - 05:40
Posts: 114
seasam wrote:
nofear87 wrote:
Where I can find the “optimize for size”-option in Atmel Studio? At the moment my hex file is to large for the Attiny13A Sad

On the File menu at the top of your screen click “Project”, click the bottom selection “(project name) Properties”, click “Toolchain” on the left, and look for “Optimization”. Optimization level set to -Os Optimize for size.

Thank you! Flash Size is 0.94 KB (972 Bytes) now. That should work?

seasam
Offline
Last seen: 5 years 2 months ago
Joined: 01/31/2015 - 13:15
Posts: 189
Location: GA, USA
nofear87 wrote:
seasam wrote:
nofear87 wrote:
Where I can find the “optimize for size”-option in Atmel Studio? At the moment my hex file is to large for the Attiny13A Sad

On the File menu at the top of your screen click “Project”, click the bottom selection “(project name) Properties”, click “Toolchain” on the left, and look for “Optimization”. Optimization level set to -Os Optimize for size.

Thank you! Flash Size is 0.94 KB (972 Bytes) now. That should work?

yeah should be fine. when you flash you should see exactly the size being written to the attiny. I think it’s 1024 bytes max.

vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

Ok, I seem to be having some trouble with the Star offtime.   I assume this is the one I am suppose to be using for the FET driver?

http://bazaar.launchpad.net/~toykeeper/flashlight-firmware/trunk/view/he...


Even using the code as is, I am not able to get it to build correctly.    At least I dont think I am.   It says 0 errors but 5 warnings

Warning    1    unused variable 'prev_mode_idx' [-Wunused-variable]    line 366

Then for lines 192,186,183, and 177 I get this warning over and over.

Warning    2    'modes' is static but used in inline function 'check_stars' which is not static [enabled by default] 

What should I do about this?

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

seasam
Offline
Last seen: 5 years 2 months ago
Joined: 01/31/2015 - 13:15
Posts: 189
Location: GA, USA
vestureofblood wrote:

Ok, I seem to be having some trouble with the Star offtime.   I assume this is the one I am suppose to be using for the FET driver?

http://bazaar.launchpad.net/~toykeeper/flashlight-firmware/trunk/view/he...


Even using the code as is, I am not able to get it to build correctly.    At least I dont think I am.   It says 0 errors but 5 warnings

Warning    1    unused variable ‘prev_mode_idx’ [-Wunused-variable]    line 366

Then for lines 192,186,183, and 177 I get this warning over and over.

Warning    2    ‘modes’ is static but used in inline function ‘check_stars’ which is not static [enabled by default] 

What should I do about this?

someone who knows more can chime in but I’m pretty sure I had those warnings and it worked fine. flash it and test it Beer

_the_
_the_'s picture
Offline
Last seen: 3 weeks 2 days ago
Joined: 07/08/2011 - 06:22
Posts: 3646
Location: Finland

vestureofblood wrote:

Ok, I seem to be having some trouble with the Star offtime.   I assume this is the one I am suppose to be using for the FET driver?

http://bazaar.launchpad.net/~toykeeper/flashlight-firmware/trunk/view/he...

Yes, that's the correct one.

vestureofblood wrote:

Even using the code as is, I am not able to get it to build correctly.    At least I dont think I am.   It says 0 errors but 5 warnings

Warning    1    unused variable 'prev_mode_idx' [-Wunused-variable]    line 366

Then for lines 192,186,183, and 177 I get this warning over and over.

Warning    2    'modes' is static but used in inline function 'check_stars' which is not static [enabled by default] 

What should I do about this?

0 errors means that it compiled "correctly".

Those warnings are not severe, so you can just ignore them.

Or fix them by:
- removing the line 366 (the variable is not used, so it can - and should be - removed safely)
- removing the 'inline'-definition on line 168 (no point of inlining that function, especially as it uses a static variable which kind of works, but violates the C-standard)

=the=

 

vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

Ok, Thanks.


I have a couple more questions.     I would like to know how to add more modes to this.

I saw that the high mode was disabled, so I enabled that and added a value, but I would also like to be able to add more.

For example can I have the moonlight mode enabled by default rather than when using  star 2?

Also if I wanted to get even more modes than that, like say 6,7,8 in the string, how to do that?

Other frimwares I have seen just had them in a single line like  "define modes, 0,2,7,14,39,100" etc, but this one has a define and the name of each one.

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

_the_
_the_'s picture
Offline
Last seen: 3 weeks 2 days ago
Joined: 07/08/2011 - 06:22
Posts: 3646
Location: Finland

vestureofblood wrote:

For example can I have the moonlight mode enabled by default rather than when using  star 2?

Comment lines 175 & 179 away. (or reverse the star logic on line 175 to be able to disable moon with star 2)

 

vestureofblood wrote:

Also if I wanted to get even more modes than that, like say 6,7,8 in the string, how to do that?

First add more mode defines (after line 76) and then add the defined modes to the mode array (after line 187).

=the=

 

vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

_the_ wrote:

(or reverse the star logic on line 175 to be able to disable moon with star 2)

 

I like this idea the best, but I'm not exactly sure what to do there.       I don't understand exactly how to express the change.

The way it is now
if
((PINB & (1 << STAR2_PIN)) == 0) {

Can you show me what it should look like?




Also what do I need to do to enable the turbo timer?

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

_the_
_the_'s picture
Offline
Last seen: 3 weeks 2 days ago
Joined: 07/08/2011 - 06:22
Posts: 3646
Location: Finland

vestureofblood wrote:

_the_ wrote:

(or reverse the star logic on line 175 to be able to disable moon with star 2)

 

I like this idea the best, but I'm not exactly sure what to do there.       I don't understand exactly how to express the change.

The way it is now
if
((PINB & (1 << STAR2_PIN)) == 0) {

Can you show me what it should look like?

if ((PINB & (1 << STAR2_PIN)) > 0) {


vestureofblood wrote:
Also what do I need to do to enable the turbo timer?

Turbo timer is enabled by default in STAR_offtime. Check lines 78, 79 & 80.

BTW: Looks like STAR_offtime (revision shown by your link) doesn't have the ability to re-enable turbo after step down by a half click. Is it really so?

=the=

 

ToyKeeper
ToyKeeper's picture
Offline
Last seen: 44 min 59 sec ago
Joined: 01/12/2013 - 14:40
Posts: 10741
Location: (469219) 2016 HO3

vestureofblood wrote:
Also if I wanted to get even more modes than that, like say 6,7,8 in the string, how to do that?

Other frimwares I have seen just had them in a single line like “define modes, 0,2,7,14,39,100” etc, but this one has a define and the name of each one.


This is part of why I made a modified version of STAR_off_time called starry_offtime. It allows you to set the mode array directly like you described, for both channels (if you have a FET+1), and set the PWM speed for each mode. Also has some extras which can be disabled, such as blinky modes and “medium” presses to go backward instead of just short/long.

It’s sort of halfway between STAR_off_time.c and blf-a6.c. It’s not heavily tested though, so LVP might behave a little weird. It also doesn’t have a way to completely turn off support for the second power channel, so the best you can do is set it to all zeroes.

Anyway, just an idea in case it helps.

vestureofblood
vestureofblood's picture
Offline
Last seen: 2 days 10 hours ago
Joined: 08/17/2012 - 15:21
Posts: 1987
Location: Missouri

_the_,

Thanks, that code edit did give me the use of moonlight without the start locked.  


I have been playing with the turbo timer a bit.    It looks like it is in fact working.    You are correct about not being able to go back to the turbo setting by a half click.     When the timer kicks in the light ramps down to "turbo low" so since the light is technically still in turbo mode the next half click takes you to low.


ToyKeeper,

I appreciate that.   It is much easier for us noobs to edit that single line with all the modes in it.   I will keep the starry in mind.


 

 

Here is today's question if you guys can bear with me a bit longer.

Is there something else in the code that effects the level of light output besides the values of each mode?

The reason I ask is because I can set value of moonlight all the way down to 1, and it does give a reasonable low mode with one cell.    On a zener modded board with 2 cells its a bit bright still though.    I have other firmware I can flash that puts moonlight lower than this.

In Him (Jesus Christ) was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not.
http://asflashlights.com/ Everyday Carry Flashlights, plus Upgrades for Maglite.

Pages