~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to DrJones/MiniMo/README

  • Committer: Selene Scriven
  • Date: 2015-02-16 20:18:46 UTC
  • Revision ID: ubuntu@toykeeper.net-20150216201846-a0gn20xs79193o04
Added more firmware from DrJones: luxdrv 0.30, MiniDrv, MiniMo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
MiniMo - simple firmware for momentary (electronic) switches (source code)
 
2
DrJones
 
3
Fri, 01/31/2014 - 18:47
 
4
 
 
5
Lights with electronic switches are becoming quite popular, I also like them,
 
6
because they give more flexibility regarding the UI. Similar to MiniDrv, a
 
7
small firmware for clickies, I made a minimalistic firmware for momentary
 
8
buttons, named it MiniMo (MINImalistic driver for MOmentary switches) and
 
9
decided (not lightly; I hope people still buy some drivers from me so I can buy
 
10
some flashlights...) to share it.
 
11
 
 
12
The UI is simple but consistent: A long press (>0.4s, so it's not that long)
 
13
switches the light on and off (with memory), a short press switches to the next
 
14
mode (circular) when on.
 
15
 
 
16
To configure, just add/delete/edit the values in the modes[]={...} line.
 
17
 
 
18
  //MiniMo -- minimalistic driver firmware for momentary buttons (no blinkies)  --  DrJones 2014
 
19
  #define F_CPU 4800000                    //use fuses  low:0x75  high:0xff
 
20
  #include <avr/io.h>
 
21
  #include <util/delay.h>
 
22
  //change modes here; just change/add/delete values. The "0" is 'off' 
 
23
  uint8_t modes[]={0,  8,90,255};          //PWM values, 5..255 - LEAVE THE "0" THERE
 
24
  int main() {
 
25
    DDRB=2; PORTB=8;                       //define PB1 as output and pull-up switch on PB3
 
26
    TCCR0A=0b00100001; TCCR0B=0b00000001;  //PWM setup, 9kHz
 
27
    uint8_t count=0,mode=0,lastmode=1,waspressed=0; //define some variables used below
 
28
 
 
29
    while(1) {                             //endless loop
 
30
 
 
31
      if ((PINB&8)==0) {                   //when the button is pressed (PB3 pulled down)
 
32
        count++;                           //count length of button press
 
33
        if (count==16) {                    //pressed long (8*50ms)
 
34
          if (mode>0) {lastmode=mode; mode=0;}  //was on?  -> off, but remember the mode
 
35
          else mode=lastmode;                   //was off? -> on, with previous mode.
 
36
        }
 
37
        waspressed=1;                      //remember that the button was pressed, see below
 
38
      }
 
39
      else {                               //button not pressed
 
40
        if (waspressed) {                    //but it was pressed, so it has just been released!
 
41
          waspressed=0;                      //reset that
 
42
          if (count<16 && mode>0) {           //really was a short press AND the light is on
 
43
            mode++; if (mode>=sizeof(modes)) mode=1; //next mode
 
44
          }
 
45
          count=0;                           //reset counter
 
46
        }
 
47
      }    
 
48
 
 
49
    OCR0B=modes[mode];                     //set PWM level (0 is off)
 
50
    _delay_ms(25);                         //wait a bit before checking again, important for counting
 
51
    }
 
52
  }
 
53
 
 
54
I think this may help a couple of projects here.
 
55
License: CC
 
56
 
 
57
EDIT Since people seem to like Werner's UI (short press for next mode, long
 
58
press for previous mode, like in mobydrv; but OFF is just one of these modes)
 
59
and since it is already implemented elsewhere, i thought I'd just show how
 
60
easily it can be implemented in MiniMo, too. Only 4 lines had to be edited,
 
61
marked with **, and the lastmode variable declaration was omitted.
 
62
 
 
63
I also changed the timing a bit (25ms delay in the main loop) to make it
 
64
respond a bit quicker.
 
65
 
 
66
  //MiniMo -- minimalistic driver firmware for momentary buttons (no blinkies)  --  DrJones 2014
 
67
  //Up&Down variant ("Werner's UI")
 
68
  #define F_CPU 4800000                    //use fuses  low:0x75  high:0xff
 
69
  #include <avr/io.h>
 
70
  #include <util/delay.h>
 
71
  //change modes here; just change/add/delete values. The "0" is 'off' 
 
72
  uint8_t modes[]={0,  8,90,255};          //PWM values, 5..255 - LEAVE THE "0" THERE
 
73
  int main() {
 
74
    DDRB=2; PORTB=8;                       //define PB1 as output and pull-up switch on PB3
 
75
    TCCR0A=0b00100001; TCCR0B=0b00000001;  //PWM setup, 9kHz
 
76
    uint8_t count=0,mode=0,waspressed=0;   //define some variables used below
 
77
 
 
78
    while(1) {                             //endless loop
 
79
 
 
80
      if ((PINB&8)==0) {                   //when the button is pressed (PB3 pulled down)
 
81
        count++;                           //count length of button press
 
82
        if (count==16) {                   //pressed long (16*25ms=0.4s)
 
83
          if (mode>0)  mode--;             // ** decrease mode
 
84
          else mode=sizeof(modes)-1;       // ** wraparound
 
85
        }
 
86
        waspressed=1;                      //remember that the button was pressed, see below
 
87
      }
 
88
      else {                               //button not pressed
 
89
        if (waspressed) {                    //but it was pressed, so it has just been released!
 
90
          waspressed=0;                      //reset that
 
91
          if (count<16) {                  // ** really was a short press
 
92
            mode++; if (mode>=sizeof(modes)) mode=0; // ** next mode
 
93
          }
 
94
          count=0;                           //reset counter
 
95
        }
 
96
      }    
 
97
 
 
98
 
 
99
    OCR0B=modes[mode];                     //set PWM level (0 is off)
 
100
    _delay_ms(25);                         //wait a bit before checking again, important for counting
 
101
    }
 
102
  }
 
103