-
Notifications
You must be signed in to change notification settings - Fork 12
/
pgHW.cpp
535 lines (405 loc) · 11.1 KB
/
pgHW.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
pgHW.cpp library
this library is to be used with standuino MicroGranny 2.0 hardware
documentation of the library you find here:
pgHW library
Created by Vaclav Pelousek 2013 www.pelousek.eu
for Standuino wwww.standuino.eu
*/
#include "Arduino.h"
#include <avr/pgmspace.h>
#include <shiftRegisterFast.h>
//#include <portManipulations.h>
#include "pgHW.h"
//#include <avr/pgmspace.h>
#include <fastAnalogRead.h>
#define SHIFTREGISTER_SER D,5
#define SHIFTREGISTER_RCK D,6
#define SHIFTREGISTER_SRCK D,7
#define LATCH_PIN 7
#define CLOCK_PIN 6
#define DATA_PIN 5
#define BUTTON_1_PIN 14
#define BUTTON_2_PIN 1
#define BUTTON_3_PIN 16
#define BUTTON_4_PIN 15
#define BUTTON_5_PIN 8
#define BUTTON_6_PIN 9
#define BUT_PIN_BAM C,0
#define BUT_PIN_BAM_2 C,2
#define BUT_PIN_BAM_3 D,1
#define BUT_PIN_BAM_4 C,1
#define BUT_PIN_BAM_5 B,0
#define BUT_PIN_BAM_6 B,1
#define KNOB_PIN_1 3
#define KNOB_PIN_2 4
#define CV_PIN 5
#define NUMBER_OF_KNOBS 2
#define DISPLAY 0
#define LEDS 1
//const unsigned char row[5]={ ROW_1_PIN,ROW_2_PIN,ROW_3_PIN,ROW_4_PIN,ROW_5_PIN };
const unsigned char rgbPin[3]={LED_R_PIN,LED_G_PIN,LED_B_PIN};
PROGMEM prog_char KnobPin[3]={KNOB_PIN_1,KNOB_PIN_2, CV_PIN};
//const uint8_t KnobPin[3]={KNOB_PIN_1,KNOB_PIN_2, CV_PIN};
/*
Segments
- A
F / / B
- G
E / / C
- D
*/
const unsigned char segments[8]={
SEG_A,SEG_B,SEG_C,SEG_D,SEG_E,SEG_F,SEG_G,SEG_DOT};
#define BLACK_BITS 0
#define RED_BITS 1
#define GREEN_BITS 2
#define BLUE_BITS 4
#define YELLOW_BITS 3
#define MAGENTA_BITS 5
#define CIAN_BITS 6
#define WHITE_BITS 7
prog_uchar ColorBit[NUMBER_OF_COLORS] PROGMEM = {
BLACK_BITS, RED_BITS,GREEN_BITS,BLUE_BITS,YELLOW_BITS,MAGENTA_BITS,CIAN_BITS,WHITE_BITS
};
enum BitOrder {LSB_FIRST,MSB_FIRST};
PROGMEM prog_uchar Typo[53]={
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111, //9
B01110111, //A 10
B01111100, //b
B01011000, //c
B01011110, //d
B01111001, //e
B01110001, //f
B00111101, //g
B01110100, //h
B00000100, //i
B00011110, //j
B01110000, //k 20
B00111000, //l
B01010101, //m
B01010100, //n
B01011100, //o
B01110011, //p 25
B01100111, //q
B01010000, //r
B01101101, //s //tu memit
B01111000, //t
B00011100, //u 30
B00001100, //v 31
B01101010, //w
B01001001, //x
B01110010, //y
B01011011, //z tu menit 35
B00000000, // void 36
B01000000, //37
B01001001, //38
B01010010, //39 slash
B00010000, //40
B00011000, //41
B00001000, //42
B00001100, //43
B00000100, //44
B01000100, //45
B01000000, //46
B01100000, //47
B00100000, //48
B00100001, //49
B00000001, //50
B00000011, //51
B00000010, //52
};
/*
Segments
- A
F / / B
- G
E / / C
- D
*/
pgHW::pgHW(){
}
//############# ROUTINE FUNCTIONS #############
void pgHW::initialize(){
pinMode(DATA_PIN,OUTPUT);
pinMode(CLOCK_PIN,OUTPUT);
pinMode(LATCH_PIN,OUTPUT);
bit_dir_inp(BUT_PIN_BAM);
bit_dir_inp(BUT_PIN_BAM_2);
// bit_dir_inp(BUT_PIN_BAM_3);
bit_dir_inp(BUT_PIN_BAM_4);
bit_dir_inp(BUT_PIN_BAM_5);
bit_dir_inp(BUT_PIN_BAM_6);
bit_set(BUT_PIN_BAM);
bit_set(BUT_PIN_BAM_2);
// bit_set(BUT_PIN_BAM_3);
bit_set(BUT_PIN_BAM_4);
bit_set(BUT_PIN_BAM_5);
bit_set(BUT_PIN_BAM_6);
//pinMode(A2,OUTPUT);
/*
fastAnalogRead::init(); // for analog read
_knob=0;
fastAnalogRead::connectChannel(KnobPin[_knob]);//pgm_read_word_near(KnobPin + _knob));
fastAnalogRead::startConversion();
*/
}
void pgHW::update(){
updateKnobs();
updateButtons();
updateDisplay();
}
//############# KNOB RELATED FUNCTIONS #############
/*
void pgHW::setFreezeType(unsigned char _TYPE){
// if(_TYPE==DEFAULT) unfreezeExternaly=false;
// else if(_TYPE==UNFREEZE_EXTERNALY) unfreezeExternaly=true;
}
*/
//update values and hashes of knobs
void pgHW::updateKnobs(){
/*
knobChangedHash = 0;
while(!fastAnalogRead::isConversionFinished()){
lastKnobValues[_knob]=knobValues[_knob];
int newValue = fastAnalogRead::getConversionResult();
// int distance = abs(newValue - knobValues[_knob]);
// Serial.print
if (abs(newValue - knobValues[_knob]) > KNOB_TOLERANCE) {
bitWrite(knobChangedHash, _knob, true);
}
else bitWrite(knobChangedHash, _knob, false);
// lastKnobValues[i]=knobValues[i];
knobValues[_knob] = newValue;
//delay(10); ///
// updateDisplay();
if(_knob<2) _knob++;
else _knob=0;
fastAnalogRead::connectChannel(KnobPin[_knob]);//pgm_read_word_near(KnobPin + _knob));
fastAnalogRead::startConversion();
}
*/
if(_knob<2) _knob++;
else _knob=0;
knobChangedHash = 0;
lastKnobValues[_knob]=knobValues[_knob];
int newValue = analogRead(pgm_read_word_near(KnobPin + _knob));
int distance = abs(newValue - knobValues[_knob]);
if (abs(newValue - knobValues[_knob]) > KNOB_TOLERANCE) {
bitWrite(knobChangedHash, _knob, true);
}
else bitWrite(knobChangedHash, _knob, false);
// lastKnobValues[i]=knobValues[i];
knobValues[_knob] = newValue;
//delay(10); ///
// updateDisplay();
/*
knobChangedHash = 0;
for (int i = 0; i < 3; i++) {
lastKnobValues[i]=knobValues[i];
int newValue = analogRead(pgm_read_word_near(KnobPin + i));
int distance = abs(newValue - knobValues[i]);
if (abs(newValue - knobValues[i]) > KNOB_TOLERANCE) {
bitWrite(knobChangedHash, i, true);
}
else bitWrite(knobChangedHash, i, false);
// lastKnobValues[i]=knobValues[i];
knobValues[i] = newValue;
//delay(10); ///
// updateDisplay();
}
*/
}
//returns the freezing state of knob
boolean pgHW::knobFreezed(unsigned char _KNOB){
return bitRead(knobFreezedHash,_KNOB);
}
boolean pgHW::knobMoved(unsigned char _KNOB){
return bitRead(knobChangedHash,_KNOB);
}
// freeze all knobs
void pgHW::freezeAllKnobs(){
for(uint8_t i=0;i<NUMBER_OF_KNOBS;i++){
bitWrite(knobFreezedHash,i,true);
}
}
// unfreeze all knobs
void pgHW::unfreezeAllKnobs(){
for(uint8_t i=0;i<NUMBER_OF_KNOBS;i++){
bitWrite(knobFreezedHash,i,false);
}
}
// freeze one knob
void pgHW::freezeKnob(unsigned char _KNOB){
bitWrite(knobFreezedHash,_KNOB,true);
}
// unfreeze one knob
void pgHW::unfreezeKnob(unsigned char _KNOB){
bitWrite(knobFreezedHash,_KNOB,false);
}
// get knob value
int pgHW::knobValue(unsigned char _KNOB){
return knobValues[_KNOB];
}
int pgHW::getCvValue(){
return knobValues[2];
}
// get last knob value
int pgHW::lastKnobValue(unsigned char _KNOB){
return lastKnobValues[_KNOB];
}
void pgHW::setLastKnobValue(unsigned char _KNOB,int _val){
lastKnobValues[_KNOB]=_val;
}
//############# LED RELATED FUNCTIONS #############
// write the values from the hash to the pins
//bitRead(ledStateHash,i)
// set state of led
void pgHW::setLed(unsigned char _LED,boolean _STATE){
if(_LED>=6) bitWrite(displayBuffer[LEDS],_LED,!_STATE);
else bitWrite(displayBuffer[LEDS],_LED,_STATE);
}
void pgHW::setColor(unsigned char _COLOR){
unsigned char _bits=pgm_read_word_near(ColorBit + _COLOR) ;
for(uint8_t i=0;i<3;i++){
setLed(rgbPin[i],bitRead(_bits,i));
}
}
void pgHW::displayText(char *text){
// int i=0;i<NUMBER_OF_DIGITS;i++) displayChar(text[i],i);
}
void pgHW::displayChar(char whichChar) {
if(whichChar==32)
{
lightNumber(VOID);
}
else if(whichChar<65){
lightNumber(whichChar-48);
}
else if(whichChar<97){
lightNumber(whichChar-65+10);
}
else {
lightNumber(whichChar-97+10);
}
}
void pgHW::lightNumber(int numberToDisplay) {
for(uint8_t i=0;i<7;i++){
bitWrite(displayBuffer[DISPLAY],segments[i],bitRead(pgm_read_word_near(Typo+numberToDisplay),i));
}
}
void pgHW::displayNumber(int _number){
// lightNumber(_number/100,1);
//lightNumber((_number%100)/10,2);
lightNumber(_number%10);
}
void pgHW::setDot( boolean _state){
bitWrite(displayBuffer[DISPLAY],SEG_DOT,_state);
}
//############# BUTTON RELATED FUNCTIONS #############
/*
void pgHW::updateButtons(){
updateButtons();
}
*/
void pgHW::updateDisplay(){
//inline void updateDisplay(){
/*
shiftRegFast::write_8bit(~displayBuffer[1]);
shiftRegFast::write_8bit(~displayBuffer[0]);
shiftRegFast::enableOutput();
*/
if(displayBuffer[0]!=lastDisplayBuffer[0] || displayBuffer[1]!=lastDisplayBuffer[1]){
digitalWrite(LATCH_PIN,LOW);
shiftOut(DATA_PIN,CLOCK_PIN,MSBFIRST,~displayBuffer[0]);//analogRead(4)>>_row);//);
shiftOut(DATA_PIN,CLOCK_PIN,MSBFIRST,~displayBuffer[1]);
digitalWrite(LATCH_PIN,HIGH);
}
lastDisplayBuffer[0]=displayBuffer[0];
lastDisplayBuffer[1]=displayBuffer[1];
}
// updates all button related hashes
void pgHW::updateButtons(){
/*
pinMode(BUTTON_1_PIN,INPUT_PULLUP);
pinMode(BUTTON_2_PIN,INPUT_PULLUP);
pinMode(BUTTON_3_PIN,INPUT_PULLUP);
pinMode(BUTTON_4_PIN,INPUT_PULLUP);
pinMode(BUTTON_5_PIN,INPUT_PULLUP);
pinMode(BUTTON_6_PIN,INPUT_PULLUP);
*/
bitWrite(buttonStateHash,0,!bit_read_in(BUT_PIN_BAM));//bit_read_in(BUT_PIN));
bitWrite(buttonStateHash,1,!bit_read_in(BUT_PIN_BAM_2));
bit_dir_inp(BUT_PIN_BAM_3);
bit_set(BUT_PIN_BAM_3);
bitWrite(buttonStateHash,2,!bit_read_in(BUT_PIN_BAM_3));
//bit_clear(BUT_PIN_BAM_3);
//bit_dir_outp(BUT_PIN_BAM_3);
bitWrite(buttonStateHash,3,!bit_read_in(BUT_PIN_BAM_4));
bitWrite(buttonStateHash,4,bit_read_in(BUT_PIN_BAM_5));
bitWrite(buttonStateHash,5,bit_read_in(BUT_PIN_BAM_6));
// justReleasedHash=0;
// justPressedHash=0;
for(uint8_t i=0;i<6;i++){ // first read the buttons and update button states
bitWrite(justPressedHash,i,false);
bitWrite(justReleasedHash,i,false);
if(bitRead(buttonStateHash,i)==true && bitRead(lastButtonStateHash,i)==false) bitWrite(justPressedHash,i,true);
if(bitRead(buttonStateHash,i)==false && bitRead(lastButtonStateHash,i)==true) bitWrite(justReleasedHash,i,true);
bitWrite(lastButtonStateHash,i,bitRead(buttonStateHash,i));
}
}
//returns current state of a button
boolean pgHW::buttonState(unsigned char _BUTTON){
return bitRead(buttonStateHash,_BUTTON);
}
//returns true if the button was just pressed
boolean pgHW::justPressed(unsigned char _BUTTON){
return bitRead(justPressedHash,_BUTTON);
}
//returns true if the button was just released
boolean pgHW::justReleased(unsigned char _BUTTON){
return bitRead(justReleasedHash,_BUTTON);
}
//flips the software switch
void pgHW::flipSwitch(unsigned char _SWITCH){
bitWrite(switchStateHash,_SWITCH,!bitRead(switchStateHash,_SWITCH));
}
// sets switch state
void pgHW::setSwitch(unsigned char _SWITCH, boolean _STATE){
bitWrite(switchStateHash,_SWITCH,_STATE);
}
//returns switch state
boolean pgHW::switchState(unsigned char _SWITCH){
return bitRead(switchStateHash,_SWITCH);
}
//resetsSwitches
void pgHW::resetSwitches(){
for(uint8_t i=0;i<NUMBER_OF_BUTTONS;i++){
bitWrite(switchStateHash,i,false);
}
}
//use switch states as bits of one number - sound
unsigned char pgHW::soundFromSwitches(){
unsigned char val=0;
for(uint8_t i=0;i<4;i++){
bitWrite(val,i,bitRead(switchStateHash,i));
}
return val;
}
//use button states as bits of one number - sound
unsigned char pgHW::soundFromButtons(){
unsigned char val=0;
for(uint8_t i=0;i<4;i++){
bitWrite(val,i,bitRead(buttonStateHash,i));
}
return val;
}