Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 pfowler 1
#include <Encoder.h>
2
 
3
// Delays for certain events in ms
4
const int ROTARY_DELAY = 100;    // Time between checking for rotary turns
5
const int BUTTON_DELAY = 800;    // Button debouncing, and repeating
6
const int FLASH_DELAY = 600;     // How long to flash the entire row after button press
7
const int CURSOR_DELAY = 200;    // How quickly to flash the cursor led
8
 
9
// Pin Assignments
10
uint8_t rota = 2;
11
uint8_t rotb = 3;
12
uint8_t button = 4;
13
uint8_t dataIn = 5;
14
uint8_t load = 6;
15
uint8_t clock = 7;
16
uint8_t trigger = 12;
17
 
18
int maxInUse = 1; 
19
uint8_t x = 0;
20
 
21
byte max7219_reg_noop        = 0x00;
22
byte max7219_reg_digit0      = 0x01;
23
byte max7219_reg_digit1      = 0x02;
24
byte max7219_reg_digit2      = 0x03;
25
byte max7219_reg_digit3      = 0x04;
26
byte max7219_reg_digit4      = 0x05;
27
byte max7219_reg_digit5      = 0x06;
28
byte max7219_reg_digit6      = 0x07;
29
byte max7219_reg_digit7      = 0x08;
30
byte max7219_reg_decodeMode  = 0x09;
31
byte max7219_reg_intensity   = 0x0a;
32
byte max7219_reg_scanLimit   = 0x0b;
33
byte max7219_reg_shutdown    = 0x0c;
34
byte max7219_reg_displayTest = 0x0f;
35
 
36
 
37
Encoder knobLeft(rota, rotb);
38
 
39
 
40
char id=0;
41
 
42
void putByte(byte data) {
43
  byte i = 8;
44
  byte mask;
45
  while(i > 0) {
46
    mask = 0x01 << (i - 1);      // get bitmask
47
    digitalWrite( clock, LOW);   // tick
48
    if (data & mask){            // choose bit
49
      digitalWrite(dataIn, HIGH);// send 1
50
    }else{
51
      digitalWrite(dataIn, LOW); // send 0
52
    }
53
    digitalWrite(clock, HIGH);   // tock
54
    --i;                         // move to lesser bit
55
  }
56
}
57
 
58
void maxSingle( byte reg, byte col) {    
59
//maxSingle is the "easy"  function to use for a     //single max7219
60
 
61
  digitalWrite(load, LOW);       // begin    
62
  putByte(reg);                  // specify register
63
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data  
64
  digitalWrite(load, LOW);       // and load da shit
65
  digitalWrite(load,HIGH);
66
}
67
 
68
void maxAll (byte reg, byte col) {    // initialize  all  MAX7219's in the system
69
  int c = 0;
70
  digitalWrite(load, LOW);  // begin    
71
  for ( c =1; c<= maxInUse; c++) {
72
  putByte(reg);  // specify register
73
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
74
    }
75
  digitalWrite(load, LOW);
76
  digitalWrite(load,HIGH);
77
}
78
 
79
void maxOne(byte maxNr, byte reg, byte col) {    
80
//maxOne is for adressing different MAX7219's,
81
//whilele having a couple of them cascaded
82
 
83
  int c = 0;
84
  digitalWrite(load, LOW);  // begin    
85
 
86
  for ( c = maxInUse; c > maxNr; c--) {
87
    putByte(0);    // means no operation
88
    putByte(0);    // means no operation
89
  }
90
 
91
  putByte(reg);  // specify register
92
  putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
93
 
94
  for ( c =maxNr-1; c >= 1; c--) {
95
    putByte(0);    // means no operation
96
    putByte(0);    // means no operation
97
  }
98
 
99
  digitalWrite(load, LOW); // and load da shit
100
  digitalWrite(load,HIGH);
101
}
102
 
103
 
104
void setup () {
105
  pinMode(dataIn, OUTPUT);
106
  pinMode(clock,  OUTPUT);
107
  pinMode(load,   OUTPUT);
108
  pinMode(button, INPUT);
109
  pinMode(trigger, OUTPUT);
110
  digitalWrite(button, HIGH);
111
  digitalWrite(13, LOW);  
112
  digitalWrite(trigger, LOW);
113
 
114
  //initiation of the max 7219
115
  maxAll(max7219_reg_scanLimit, 0x07);      
116
  maxAll(max7219_reg_decodeMode, 0x00);  // using an led matrix (not digits)
117
  maxAll(max7219_reg_shutdown, 0x01);    // not in shutdown mode
118
  maxAll(max7219_reg_displayTest, 0x00); // no display test
119
  for (x=1; x<=8; x++) {    // empty registers, turn all LEDs off
120
    maxAll(x,0);
121
  }
122
  maxAll(max7219_reg_intensity, 0x01 & 0x0f);  // the first 0x0f is the value you can set range: 0x00 to 0x0f
123
 
124
}  
125
 
126
// Value to display a single LED on a row
127
uint8_t values[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128};
128
// Array with all rows, indicating which 'value' to show
129
uint8_t line[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 
130
// What row we are current working with
131
uint8_t idx = 0;
132
// Our delay counters (in ms)
133
int delayButton = 0;
134
int delayRotary = ROTARY_DELAY;
135
int delayFlash = 0;
136
int delayCursor = CURSOR_DELAY;
137
int cursorState = 0;
138
 
139
// Rotary position, reset after each loop
140
long pos = 0;
141
// Do we update the matrix this run?
142
uint8_t update = 1;
143
 
144
void loop () { 
145
 
146
  // Flash the row after a button press
147
  if (delayFlash && idx != 8) {
148
    maxSingle(idx+1, 255);
149
 
150
    if (delayFlash == 1) {
151
      maxSingle(idx+1, values[line[idx+1]]);
152
    }
153
 
154
    delayFlash--;
155
    if (delayButton) delayButton--;
156
 
157
    return;
158
  } 
159
 
160
  // Check for button press
161
  if (delayButton) delayButton--;
162
  if (!digitalRead(4) && delayButton == 0) { 
163
    if (idx == 8) 
164
      idx = 0;
165
     else
166
       idx++;
167
     delayButton = BUTTON_DELAY;
168
     delayFlash = FLASH_DELAY;
169
  }
170
 
171
 
172
  // Checks for rotary turning, and update
173
  //  the matrix buffer
174
  // My rotary encoder had '2 turns' per detent, making
175
  //  it awkward to move a single position. This is why
176
  //  the check is for > 1 and < -1 to make it a full detent.
177
  if (delayRotary) delayRotary--;
178
  if (delayRotary == 0 && idx != 8) {
179
    pos = knobLeft.read();
180
    if (pos > 1) {
181
      if (line[idx] != 0)
182
        line[idx]--;
183
      else
184
        line[idx] = 8;
185
      update = 1;
186
    } else if (pos < -1) {
187
      if (line[idx] != 8)
188
        line[idx]++;
189
      else
190
        line[idx]= 0;
191
      update = 1;
192
    }
193
 
194
    if (update) { 
195
      knobLeft.write(0);
196
      delayRotary = ROTARY_DELAY;
197
 
198
      if (pos) {
199
        cursorState = 1;
200
        delayCursor = CURSOR_DELAY;
201
      }
202
    }
203
  }
204
 
205
 
206
  // Toggle the state of the cursor after the
207
  //  cursor delay (in ms) has been reached
208
  if (delayCursor) delayCursor--; 
209
  if (delayCursor == 0 && idx != 8) {
210
    delayCursor = CURSOR_DELAY;
211
    cursorState = 1 - cursorState;
212
    update = 1; 
213
  }
214
 
215
  // Update the matrix only if a change has been made
216
  if (update) {
217
    for (x = 0; x<8; x++) {
218
       if (x == idx) {
219
         if (cursorState && line[x] != 0)
220
             maxSingle(x+1, values[line[x]]);
221
         else
222
             maxSingle(x+1, 0);
223
       } else {
224
         if (line[x] == 0) continue;
225
         digitalWrite(trigger, HIGH);
226
         maxSingle(x+1, values[line[x]]);
227
         digitalWrite(trigger, LOW);
228
       }
229
    }
230
    update = 0;
231
  }
232
 
233
  delay(1);
234
 
235
}