-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheuropi_func1.c
executable file
·2357 lines (2274 loc) · 93.2 KB
/
europi_func1.c
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Richard R. Goodwin / Audio Morphology
//
// Author: Richard R. Goodwin ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//#include <linux/input.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <pigpio.h>
#include <signal.h>
#include "europi.h"
//#include "front_panel.c"
//#include "touch.h"
//#include "touch.c"
//#include "quantizer_scales.h"
#include "../raylib/src/raylib.h"
//extern struct europi;
extern struct fb_var_screeninfo vinfo;
extern struct fb_fix_screeninfo finfo;
extern struct fb_var_screeninfo orig_vinfo;
extern unsigned hw_version;
extern int fbfd;
extern char *fbp;
extern char *kbfds;
extern int impersonate_hw;
extern int debug;
extern char debug_messages[10][80];
extern char modal_dialog_txt1[];
extern char modal_dialog_txt2[];
extern char modal_dialog_txt3[];
extern char modal_dialog_txt4[];
extern int next_debug_slot;
extern char input_txt[];
extern int kbfd;
extern int ThreadEnd;
extern int prog_running;
extern int run_stop;
extern int save_run_stop;
extern int is_europi;
extern int midi_clock_counter;
extern int midi_clock_divisor;
extern int retrig_counter;
extern int extclk_counter;
extern int extclk_level;
extern int clock_counter;
extern int clock_level;
extern int clock_freq;
extern int clock_source;
extern int TuningOn;
extern uint16_t TuningVoltage;
extern uint8_t PCF8574_state;
extern int led_on;
extern int print_messages;
//extern unsigned int sequence[6][32][3];
//extern int current_step;
//extern int last_step;
extern int last_track;
extern int selected_step;
extern int step_one;
extern int step_one_state;
extern uint32_t step_tick;
extern uint32_t step_ticks;
extern uint32_t slew_interval;
/* globals used by touchscreen interface */
extern int xres,yres,x;
extern int Xsamples[20];
extern int Ysamples[20];
extern int screenXmax, screenXmin;
extern int screenYmax, screenYmin;
extern float scaleXvalue, scaleYvalue;
extern int rawX, rawY, rawPressure, scaledX, scaledY;
extern int Xaverage;
extern int Yaverage;
extern int VerticalScrollPercent;
extern int sample;
extern int touched;
extern int encoder_level_A;
extern int encoder_level_B;
extern int encoder_lastGpio;
extern int encoder_pos;
extern int encoder_vel;
extern uint32_t encoder_tick;
extern enum encoder_focus_t encoder_focus;
extern enum btnA_func_t btnA_func;
extern enum btnB_func_t btnB_func;
extern enum btnC_func_t btnC_func;
extern enum btnD_func_t btnD_func;
extern int btnA_state;
extern int btnB_state;
extern int btnC_state;
extern int btnD_state;
extern struct europi Europi;
extern struct europi_hw Europi_hw;
extern enum display_page_t DisplayPage;
//extern struct screen_overlays ScreenOverlays;
extern uint32_t ActiveOverlays;
extern int kbd_char_selected;
extern struct MENU Menu[];
extern pthread_attr_t detached_attr;
extern pthread_mutex_t mcp23008_lock;
extern pthread_mutex_t pcf8574_lock;
extern pthread_t midiThreadId[];
extern int midiThreadLaunched[];
extern uint8_t mcp23008_state[16];
extern int test_v;
pthread_t ThreadId; // Pointer to detatched Thread Ids (re-used by each/every detatched thread)
extern Font font1;
extern Texture2D Splash;
extern Texture2D KeyboardTexture;
extern Texture2D DialogTexture;
extern Texture2D SmallDialogTexture;
extern Texture2D TextInputTexture;
extern Texture2D Text2chTexture;
extern Texture2D Text5chTexture;
extern Texture2D Text10chTexture;
extern Texture2D MainScreenTexture;
extern Texture2D TopBarTexture;
extern Texture2D ButtonBarTexture;
extern Texture2D VerticalScrollBarTexture;
extern Texture2D VerticalScrollBarShortTexture;
extern Texture2D ScrollHandleTexture;
extern int disp_menu;
extern char **files;
extern char *kbd_chars[4][11];
extern size_t file_count;
extern int file_selected;
extern int first_file;
/* Internal Clock
*
* This is the main timing loop for the
* whole programme, and which runs continuously
* from program start to end.
*
* The Master Clock runs at 96 * The BPM step
* frequency
*
* This drives PIN 10 (GPIO 15), which loops back
* via the Clock In jack
*/
void master_clock(int gpio, int level, uint32_t tick)
{
if (run_stop == RUN){
if (clock_counter++ > 95) {
clock_counter = 0;
gpioWrite(INT_CLK_OUT, 1);
}
if (clock_counter == 48) gpioWrite(INT_CLK_OUT,0);
}
}
/* Main Clock - this is the function that advances the
* sequnce on to the next step. The master clock is normalled
* back into this input via the external clock jack, thus
* inserting a jack into the Clock In input cuts out the
* internal clock and replaces it with the external clock
* signal
*/
void main_clock(int gpio, int level, uint32_t tick)
{
if (run_stop == RUN){
// Copy the clock signal to the Clock Out port
GATESingleOutput(Europi.tracks[0].channels[GATE_OUT].i2c_handle,CLOCK_OUT,DEV_PCF8574,level);
//GATESingleOutput(1,CLOCK_OUT,DEV_PCF8574,level);
if (level == 1) next_step();
}
}
/* run/stop switch input
* called whenever the switch changes state
*/
void runstop_input(int gpio, int level, uint32_t tick)
{
run_stop = !level;
}
/* Reset input
* Rising edge causes next step to be Step 1
*/
void reset_input(int gpio, int level, uint32_t tick)
{
if (level == 1) step_one = TRUE;
}
/* Hold input
* nOT QUITE SURE WHAT TO DO WITHH THIS YET!!
*/
void hold_input(int gpio, int level, uint32_t tick)
{
if(level == 1){
}
}
/*
* Manually force Step One to tbe the next step
*/
void set_step_one(void)
{
step_one = TRUE;
}
/* Function called to advance the sequence on to the next step */
void next_step(void)
{
uint32_t current_tick = gpioTick();
// first ever time it's run, there will be
// no value for step_tick, to the length of
// the first step will be indeterminate. So,
// set it to 200ms.
if (step_tick == 0) step_ticks = 250000; else step_ticks = current_tick - step_tick;
//log_msg("Step Ticks: %d\n",step_ticks);
step_tick = current_tick;
int previous_step, channel, track;
/* look for something to do */
//for (track = 0;track < MAX_TRACKS; track++){
for (track = 0;track < last_track; track++){
/* if this track is busy doing something else, then it won't advance to the next step */
if (Europi.tracks[track].track_busy == FALSE){
// Each track can be running at a Sub-Division of the main clock
// So, if we haven't reached the required repetitions for this track, don't move on to the next step
if (++Europi.tracks[track].clock_divisor_counter >= Europi.tracks[track].clock_divisor){
Europi.tracks[track].clock_divisor_counter = 0;
/* Each Track has its own end point */
previous_step = Europi.tracks[track].current_step;
/* Switch behaviour depending on the direction this track moves in */
switch (Europi.tracks[track].direction){
case Forwards:
default:
if(++Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter >= Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repetitions || step_one == TRUE){
Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter = 0;
if (++Europi.tracks[track].current_step >= Europi.tracks[track].last_step || step_one == TRUE){
Europi.tracks[track].current_step = 0;
/* IF we've got Europi hardware, trigger the Step 1 pulse as Track 0 passes through Step 0 */
if ((is_europi == TRUE) && (track == 0)){
/* Track 0 Channel 1 will have the GPIO Handle for the PCF8574 channel 3 is Step 1 Out*/
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[0].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[0].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = STEP1_OUT;
sGate.i2c_device = DEV_PCF8574;
sGate.gate_type = Trigger;
sGate.ratchets = 1;
sGate.fill = 1;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
}
break;
case Backwards:
if(++Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter >= Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repetitions || step_one == TRUE){
Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter = 0;
if(Europi.tracks[track].current_step == 0 && step_one == FALSE){
Europi.tracks[track].current_step = Europi.tracks[track].last_step -1;
}
else if (Europi.tracks[track].current_step == 1 || step_one == TRUE){
Europi.tracks[track].current_step = 0;
/* IF we've got Europi hardware, trigger the Step 1 pulse as Track 0 passes through Step 0 */
if ((is_europi == TRUE) && (track == 0)){
/* Track 0 Channel 1 will have the GPIO Handle for the PCF8574 channel 3 is Step 1 Out*/
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[0].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[0].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = STEP1_OUT;
sGate.i2c_device = DEV_PCF8574;
sGate.gate_type = Trigger;
sGate.ratchets = 1;
sGate.fill = 1;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
else Europi.tracks[track].current_step--;
}
break;
case Pendulum_F:
/* Advances forwards until it reaches the last step */
if(++Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter >= Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repetitions || step_one == TRUE){
Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter = 0;
if (++Europi.tracks[track].current_step >= Europi.tracks[track].last_step && step_one == FALSE){
Europi.tracks[track].current_step--;
Europi.tracks[track].direction = Pendulum_B;
}
else if (step_one == TRUE) {
Europi.tracks[track].current_step = 0;
/* IF we've got Europi hardware, trigger the Step 1 pulse as Track 0 passes through Step 0 */
if ((is_europi == TRUE) && (track == 0)){
/* Track 0 Channel 1 will have the GPIO Handle for the PCF8574 channel 3 is Step 1 Out*/
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[0].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[0].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = STEP1_OUT;
sGate.i2c_device = DEV_PCF8574;
sGate.gate_type = Trigger;
sGate.ratchets = 1;
sGate.fill = 1;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
}
break;
case Pendulum_B:
if(++Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter >= Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repetitions || step_one == TRUE){
Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter = 0;
if(Europi.tracks[track].current_step == 0 || step_one == TRUE){
Europi.tracks[track].direction = Pendulum_F;
Europi.tracks[track].current_step = 0;
/* IF we've got Europi hardware, trigger the Step 1 pulse as Track 0 passes through Step 0 */
if ((is_europi == TRUE) && (track == 0)){
/* Track 0 Channel 1 will have the GPIO Handle for the PCF8574 channel 3 is Step 1 Out*/
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[0].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[0].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = STEP1_OUT;
sGate.i2c_device = DEV_PCF8574;
sGate.gate_type = Trigger;
sGate.ratchets = 1;
sGate.fill = 1;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
else Europi.tracks[track].current_step--;
}
break;
case Random:
if(++Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter >= Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repetitions || step_one == TRUE){
Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].repeat_counter = 0;
Europi.tracks[track].current_step = rand() % Europi.tracks[track].last_step;
if(Europi.tracks[track].current_step == 0 || step_one == TRUE){
Europi.tracks[track].current_step = 0;
if ((is_europi == TRUE) && (track == 0)){
/* Track 0 Channel 1 will have the GPIO Handle for the PCF8574 channel 3 is Step 1 Out*/
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[0].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[0].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = STEP1_OUT;
sGate.i2c_device = DEV_PCF8574;
sGate.gate_type = Trigger;
sGate.ratchets = 1;
sGate.fill = 1;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
}
break;
}
/* Deal with the various different types of Analogue output
* In General, this launches a thread to deal with anything
* that isn't a simple static voltage, as this removes the
* processing load from the main program loop
*/
if(Europi.tracks[track].channels[CV_OUT].enabled == TRUE) {
//log_msg("CV Out, Trk: %d Chnl: %d, Val: %d\n",track,CV_OUT,Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].scaled_value);
switch(Europi.tracks[track].channels[CV_OUT].type){
default:
case CHNL_TYPE_CV:
// CV Channels implement static CV & Slew
if(Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].slew_type == Off){
// No Slew - just set the output CV
//log_msg("SnglChnlWr, Trk: %d Chnl: %d, i2c: %d Val: %d\n",track,CV_OUT,Europi.tracks[track].channels[CV_OUT].i2c_channel,Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].scaled_value);
DACSingleChannelWrite(track,Europi.tracks[track].channels[CV_OUT].i2c_handle, Europi.tracks[track].channels[CV_OUT].i2c_address, Europi.tracks[track].channels[CV_OUT].i2c_channel, Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].scaled_value);
}
else {
// Slew
//log_msg("slew\n");
struct slew sSlew;
sSlew.track = track;
sSlew.i2c_handle = Europi.tracks[track].channels[CV_OUT].i2c_handle;
sSlew.i2c_address = Europi.tracks[track].channels[CV_OUT].i2c_address;
sSlew.i2c_channel = Europi.tracks[track].channels[CV_OUT].i2c_channel;
sSlew.start_value = Europi.tracks[track].channels[CV_OUT].steps[previous_step].scaled_value;
sSlew.end_value = Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].scaled_value;
sSlew.slew_length = Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].slew_length;
sSlew.slew_type = Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].slew_type;
sSlew.slew_shape = Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].slew_shape;
struct slew *pSlew = malloc(sizeof(struct slew));
memcpy(pSlew, &sSlew, sizeof(struct slew));
if(pthread_create(&ThreadId, &detached_attr, &SlewThread, pSlew)){
log_msg("Slew thread creation error\n");
}
}
break;
case CHNL_TYPE_MIDI:
MIDISingleChannelWrite(Europi.tracks[track].channels[CV_OUT].i2c_handle, Europi.tracks[track].channels[CV_OUT].i2c_channel, 0x40, Europi.tracks[track].channels[CV_OUT].steps[Europi.tracks[track].current_step].raw_value);
break;
}
}
// MOD Channel
if(Europi.tracks[track].channels[MOD_OUT].enabled == TRUE) {
switch(Europi.tracks[track].channels[MOD_OUT].type){
default:
case CHNL_TYPE_MOD:
if(Europi.tracks[track].channels[MOD_OUT].steps[Europi.tracks[track].current_step].mod_shape == Mod_Off){
// Just Output Zero - no need for the thread launch overhead
DACSingleChannelWrite(track,Europi.tracks[track].channels[MOD_OUT].i2c_handle, Europi.tracks[track].channels[MOD_OUT].i2c_address, Europi.tracks[track].channels[MOD_OUT].i2c_channel, 0);
}
else{
// Launch a modulation thread for this step
struct modstep sMod;
sMod.track = track;
sMod.i2c_handle = Europi.tracks[track].channels[MOD_OUT].i2c_handle;
sMod.i2c_address = Europi.tracks[track].channels[MOD_OUT].i2c_address;
sMod.i2c_channel = Europi.tracks[track].channels[MOD_OUT].i2c_channel;
sMod.i2c_device = Europi.tracks[track].channels[MOD_OUT].i2c_device;
sMod.mod_shape = Europi.tracks[track].channels[MOD_OUT].steps[Europi.tracks[track].current_step].mod_shape;
sMod.min = Europi.tracks[track].channels[MOD_OUT].steps[Europi.tracks[track].current_step].min;
sMod.max = Europi.tracks[track].channels[MOD_OUT].steps[Europi.tracks[track].current_step].max;
sMod.duty_cycle = Europi.tracks[track].channels[MOD_OUT].steps[Europi.tracks[track].current_step].duty_cycle;
struct modstep *pModstep = malloc(sizeof(struct modstep));
memcpy(pModstep, &sMod, sizeof(struct modstep));
if(pthread_create(&ThreadId, &detached_attr, &ModThread, pModstep)){
}
}
break;
}
}
// Gate Channel
/* launch a thread to handle the gate function for each channel / step */
if (Europi.tracks[track].channels[GATE_OUT].enabled == TRUE ){
struct gate sGate;
sGate.track = track;
sGate.i2c_handle = Europi.tracks[track].channels[GATE_OUT].i2c_handle;
sGate.i2c_address = Europi.tracks[track].channels[GATE_OUT].i2c_address;
sGate.i2c_channel = Europi.tracks[track].channels[GATE_OUT].i2c_channel;
sGate.i2c_device = Europi.tracks[track].channels[GATE_OUT].i2c_device;
sGate.ratchets = Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].ratchets;
sGate.gate_type = Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].gate_type;
sGate.fill = Europi.tracks[track].channels[GATE_OUT].steps[Europi.tracks[track].current_step].fill;
struct gate *pGate = malloc(sizeof(struct gate));
memcpy(pGate, &sGate, sizeof(struct gate));
if(pthread_create(&ThreadId, &detached_attr, &GateThread, pGate)){
log_msg("Gate thread creation error\n");
}
}
}
}
}
/* anything that needed resetting back to step 1 will have done so */
if (step_one == TRUE) step_one = FALSE;
}
/*
* ADSR thread.
* ADSR Profile ramp generator. Always starts and ends at Zero, and
* the sustain level is expressed as a Percentage of the max Attack value
*/
void *AdsrThread(void *arg)
{
struct adsr *pADSR = (struct adsr *)arg;
uint16_t this_value;
uint32_t start_tick = gpioTick();
uint16_t sus_level;
int step_size;
int num_steps;
int i;
// Set Track Busy flag
Europi.tracks[pADSR->track].track_busy = TRUE;
sus_level = ((pADSR->a_end_value - pADSR->a_start_value) * pADSR->s_level)/100;
// A-ramp
this_value = pADSR->a_start_value;
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
if(pADSR->a_length >= slew_interval){
step_size = (pADSR->a_end_value - pADSR->a_start_value) / (pADSR->a_length / slew_interval);
num_steps = (pADSR->a_end_value - pADSR->a_start_value) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pADSR->a_end_value - pADSR->a_start_value);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value += step_size) <= Europi.tracks[pADSR->track].channels[CV_OUT].scale_max){
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, Europi.tracks[pADSR->track].channels[CV_OUT].scale_max);
}
}
}
// D-ramp
this_value = pADSR->a_end_value;
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
if(pADSR->d_length >= slew_interval){
step_size = (pADSR->a_end_value - sus_level) / (pADSR->d_length / slew_interval);
num_steps = (pADSR->a_end_value - sus_level) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pADSR->a_end_value - sus_level);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value -= step_size) >= Europi.tracks[pADSR->track].channels[CV_OUT].scale_zero){
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, Europi.tracks[pADSR->track].channels[CV_OUT].scale_zero);
}
}
}
// Sustain time
usleep(pADSR->s_length);
// Release ramp
this_value = sus_level;
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
if(pADSR->r_length >= slew_interval){
step_size = (sus_level - pADSR->r_end_value) / (pADSR->r_length / slew_interval);
num_steps = (sus_level - pADSR->r_end_value) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (sus_level - pADSR->r_end_value);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value -= step_size) >= Europi.tracks[pADSR->track].channels[CV_OUT].scale_zero){
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, Europi.tracks[pADSR->track].channels[CV_OUT].scale_zero);
}
}
}
DACSingleChannelWrite(pADSR->track,pADSR->i2c_handle, pADSR->i2c_address, pADSR->i2c_channel, pADSR->r_end_value);
// Clear Track Busy flag
Europi.tracks[pADSR->track].track_busy = FALSE;
free(pADSR);
return(0);
}
/*
* Attack-Decay thread.
* Simple AD ramp, which can be set to OneShot or Repeat
*/
void *AdThread(void *arg)
{
struct ad *pAD = (struct ad *)arg;
uint16_t this_value;
uint32_t start_tick = gpioTick();
int step_size;
int num_steps;
int i;
// don't bother if it's anything other than a 'normal' AD profile
if((pAD->a_end_value > pAD->a_start_value) && (pAD->d_end_value < pAD->a_end_value)){
// Set Track Busy flag
Europi.tracks[pAD->track].track_busy = TRUE;
// A-ramp
this_value = pAD->a_start_value;
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, this_value);
if(pAD->a_length >= slew_interval){
step_size = (pAD->a_end_value - pAD->a_start_value) / (pAD->a_length / slew_interval);
num_steps = (pAD->a_end_value - pAD->a_start_value) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pAD->a_end_value - pAD->a_start_value);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value += step_size) <= 60000){
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, 60000);
}
}
}
// D-ramp
this_value = pAD->a_end_value;
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, this_value);
if(pAD->d_length >= slew_interval){
step_size = (pAD->a_end_value - pAD->d_end_value) / (pAD->d_length / slew_interval);
num_steps = (pAD->a_end_value - pAD->d_end_value) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pAD->a_end_value - pAD->d_end_value);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value -= step_size) >= 0){
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, 0);
}
}
}
DACSingleChannelWrite(pAD->track,pAD->i2c_handle, pAD->i2c_address, pAD->i2c_channel, pAD->d_end_value);
// Clear Track Busy flag
Europi.tracks[pAD->track].track_busy = FALSE;
}
free(pAD);
return(0);
}
/*
* Slew Thread - launched for each Track / Step that
* has a slew value other than SLEW_OFF. This thread
* function lives just to perform the slew, then
* ends itself. It executes the slew by stepping through
* a pre-calculated array for each slew shape (Linear, Exponential etc)
*/
void *SlewThread(void *arg)
{
struct slew *pSlew = (struct slew *)arg;
uint16_t this_value = pSlew->start_value;
uint32_t start_tick = gpioTick();
int step_size;
int num_steps;
float profile_offset;
float profile_index;
float pitch_jump;
int slew_profile;
if (pSlew->slew_length == 0) {
// No slew length set, so just output this step and close the thread
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, pSlew->end_value);
free(pSlew);
return(0);
}
if ((pSlew->end_value > pSlew->start_value) && ((pSlew->slew_shape == Rising) || (pSlew->slew_shape == Both))) {
// Glide UP
switch(pSlew->slew_type){
case Exponential:
slew_profile = 1;
break;
case RevExp:
slew_profile = 2;
break;
case Linear:
default:
slew_profile = 0;
break;
}
num_steps = pSlew->slew_length / slew_interval;
profile_index = (float)100 / (float)num_steps;
pitch_jump = pSlew->end_value - pSlew->start_value;
profile_offset = 0;
while((num_steps > 0) && (this_value <= pSlew->end_value)){
this_value = pSlew->start_value + ((slew_profiles[0][slew_profile][(int)profile_offset] / (float)100) * pitch_jump);
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, this_value);
usleep(slew_interval / 2);
profile_offset += profile_index;
num_steps--;
}
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, pSlew->end_value);
}
else if ((pSlew->end_value < pSlew->start_value) && ((pSlew->slew_shape == Falling) || (pSlew->slew_shape == Both))){
// Glide Down
switch(pSlew->slew_type){
case Exponential:
slew_profile = 4;
break;
case RevExp:
slew_profile = 5;
break;
case Linear:
default:
slew_profile = 3;
break;
}
num_steps = pSlew->slew_length / slew_interval;
profile_index = (float)100 / (float)num_steps;
pitch_jump = pSlew->start_value - pSlew->end_value;
profile_offset = 0;
while((num_steps > 0) && (this_value >= pSlew->end_value)){
this_value = pSlew->end_value + ((slew_profiles[1][slew_profile][(int)profile_offset] / (float)100) * pitch_jump);
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, this_value);
usleep(slew_interval / 2);
profile_offset += profile_index;
num_steps--;
}
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, pSlew->end_value);
}
else {
// Slew set, but Rising or Falling are off, so just output the end value
DACSingleChannelWrite(pSlew->track,pSlew->i2c_handle, pSlew->i2c_address, pSlew->i2c_channel, pSlew->end_value);
}
free(pSlew);
return(0);
}
/*
* Modulation thread
* Is passed a structure containing everything it
* needs to know
*/
void *ModThread(void *arg)
{
struct modstep *pMod = (struct modstep *)arg;
uint16_t this_value;
int step_size;
int num_steps;
int a_length;
int d_length;
int i;
switch (pMod->mod_shape) {
default:
case Mod_Off:
// should never get here, but just in case, output the min value
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->min);
break;
case Mod_Square:
// Output the Max value, sleep according to Duty Cyle, outpu minimum
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->max);
//log_msg("Tr %d, Hn %d, Ad %d, Ch %d, Val %d\n",pMod->track,pMod->i2c_handle,pMod->i2c_address,pMod->i2c_channel,pMod->max);
usleep((step_ticks * pMod->duty_cycle)/100);
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->min);
//log_msg("Tr %d, Hn %d, Ad %d, Ch %d, Val %d\n",pMod->track,pMod->i2c_handle,pMod->i2c_address,pMod->i2c_channel,pMod->min);
break;
case Mod_Triangle:
// Attack & Decay ramp lengths depend on Duty cycle
a_length = (step_ticks * pMod->duty_cycle)/100;
d_length = step_ticks - a_length;
// A-ramp
this_value = pMod->min;
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, this_value);
if(a_length >= slew_interval){
step_size = (pMod->max - pMod->min) / (a_length / slew_interval);
num_steps = (pMod->max - pMod->min) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pMod->max - pMod->min);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value += step_size) <= pMod->max){
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->max);
}
}
}
// D-ramp
this_value = pMod->max;
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, this_value);
if(d_length >= slew_interval){
step_size = (pMod->max - pMod->min) / (d_length / slew_interval);
num_steps = (pMod->max - pMod->min) / step_size;
if (step_size == 0) {step_size = 1; num_steps = (pMod->max - pMod->min);}
for(i = 0;i < num_steps; i++){
usleep(slew_interval / 2);
// Rail clamp
if((this_value -= step_size) >= pMod->min){
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, this_value);
}
else {
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->min);
}
}
}
DACSingleChannelWrite(pMod->track,pMod->i2c_handle, pMod->i2c_address, pMod->i2c_channel, pMod->min);
break;
}
free(pMod);
return(0);
}
/*
* Gate Thread - launched for each Track / Step that
* has a Gate/Trigger. For normal Gsates, it uses gate_type
* to determine the length of the pulse.
* If a ratchet value is set, then it will output a series of
* pulses timed to fit within the known length of the Step. It
* uses a Fill value to determine how many of these ratchets actually
* sound. If Fill >= ratchets, then all will sound, otherwise it
* looks up the fill sequence in a pre-calculated Euclidian fill
* table, which gives quite musical rhythmic fills up to 32 ratchets
* per step. Above this, it just outputs 100% ratchets.
*
* The thread lives just to perform the gate, then ends itself
*
* Track swing perceentage is an amount from 50% (no swing) to 90%
* which delays the gate on Even-numbered steps by a percentage of
* the overall step length
*/
void *GateThread(void *arg)
{
struct gate *pGate = (struct gate *)arg;
uint32_t step_ticks_local = step_ticks;
uint32_t swing_delay;
// If global tuning is on, ignore all Gate info, just turn all the gates ON and quit
if(TuningOn == TRUE){
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
free(pGate);
return(0);
}
if ((Europi.tracks[pGate->track].swing_percent > 50) && (Europi.tracks[pGate->track].swing_percent <= 90)) {
//Track has a swing percent set
if(Europi.tracks[pGate->track].current_step % 2 == 0){
// Even-numbered step, with swing, so delay the onset of the Gate by the swing percentage
swing_delay = (step_ticks_local * (Europi.tracks[pGate->track].swing_percent - 50)) / 100;
// Reduce the remaiining step length by thhis amount (so that everything fits)
step_ticks_local -= swing_delay;
log_msg("Trk: %d, Pct: %d, STicks: %d, S_delay: %d",pGate->track,Europi.tracks[pGate->track].swing_percent,step_ticks,swing_delay);
usleep(swing_delay);
}
}
//log_msg("GateThread H: %d, Ch: %d, Dev: %d\n",pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device);
if (pGate->ratchets <= 1){
//Normal Gate
switch(pGate->gate_type){
case Gate_Off:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
case Gate_On:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
break;
case Trigger:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep(10000); //10ms Pulse
/* Gate Off */
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
case Gate_25:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep((step_ticks_local * 25)/100);
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
case Gate_50:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep((step_ticks_local * 50)/100);
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
case Gate_75:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep((step_ticks_local * 75)/100);
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
case Gate_95:
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep((step_ticks_local * 95)/100);
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
break;
}
}
// Ratchetting Gate
else {
/* this step is to be re-triggered, so work out the sleep length between triggers
* The measured Function Calling overhead averages at around 10k to 20k ticks
* whereas a typical Step length would be between 200k and, perhaps, 1m2, so taking
* off 10k for the function calling overhead feels about right
*/
int sleep_time = ((step_ticks_local - 10000) / pGate->ratchets)/2;
int i;
for (i = 0; i < pGate->ratchets; i++){
// Ratchet is ON
if(polyrhythm(pGate->ratchets,pGate->fill,i)){
/* Gate On */
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,1);
usleep(sleep_time);
/* Gate Off */
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
usleep(sleep_time);
}
else {
// Ratchet is OFF - make sure Gate is OFF just in case
// an Off Ratchet follows an ON gate!
GATESingleOutput(pGate->i2c_handle, pGate->i2c_channel,pGate->i2c_device,0);
usleep(sleep_time * 2);
}
}
}
free(pGate);
return(0);
}
/*
* MIDI Thread - Joinable thread launched
* for each MIDI Minion (ie up to 4 of these
* could be running)
*/
void *MidiThread(void *arg)
{
struct midiChnl *pMidiChnl = (struct midiChnl *)arg;
int fd = pMidiChnl->i2c_handle;
int ret_val;
while (!ThreadEnd){
if(i2cReadByteData(fd,SC16IS750_RXLVL) > 0) {
ret_val = i2cReadByteData(fd,SC16IS750_RHR);
}
}
return NULL;
}
/*
* Delay thread, which sleeps for the passed time
* then applies the passed bit-mask to the ActiveOverlays Global
* This is ued to Eg. turn off temporary small Dialogs
* after an amount of time
*/
void *OvlTimerThread(void *arg){
struct ovl_timer *pOvlTimer = (struct ovl_timer *)arg;
usleep(pOvlTimer->sleeptime);
ActiveOverlays &= pOvlTimer->overlays;
return NULL;
}
/*
* STARTUP
* Main initialisation function, called once when the
* prog starts.
*/
int startup(void)
{
// Initial state of the Screen Elements (Menus etc)
ClearScreenOverlays();
DisplayPage = GridView;
// Initialise the Deatched pThread attribute
pthread_attr_init(&detached_attr);
pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED);
int i;
// Make sure MIDI Listener threads aren't initialised
// they will be initialised when MIDI Minion(s) are detected
for(i=0;i<4;i++) {
midiThreadId[i] = (pthread_t)NULL;
midiThreadLaunched[i] = FALSE;
}
// establish a Mutex lock to protect the MCP23008
if (pthread_mutex_init(&mcp23008_lock, NULL) != 0){
log_msg("MCP23008 mutex init failed\n");
}
if (pthread_mutex_init(&pcf8574_lock, NULL) != 0){
log_msg("PCF8574 mutex init failed\n");
}
// Initialise the Europi structure
int channel;
for(channel=0;channel < MAX_CHANNELS;channel++){
}
// Check and note the hardware revision - this is
// important because the I2C bus is Bus 0 on the
// older boards, and Bus 1 on the later ones
hw_version = gpioHardwareRevision();
log_msg("Running on hw_revision: %d\n",hw_version);
// PIGPIO Function initialisation
if (gpioInitialise()<0) return 1;
// TEMP for testing with the K-Sharp screen
// Use one of the buttons to quit the app
gpioSetMode(BUTTON1_IN, PI_INPUT);
gpioGlitchFilter(BUTTON1_IN,100);
gpioSetPullUpDown(BUTTON1_IN, PI_PUD_UP);
gpioSetMode(BUTTON2_IN, PI_INPUT);
gpioGlitchFilter(BUTTON2_IN,100);
gpioSetPullUpDown(BUTTON2_IN, PI_PUD_UP);
gpioSetMode(BUTTON3_IN, PI_INPUT);
gpioGlitchFilter(BUTTON4_IN,100);
gpioSetPullUpDown(BUTTON3_IN, PI_PUD_UP);
gpioSetMode(BUTTON4_IN, PI_INPUT);
gpioGlitchFilter(BUTTON4_IN,100);
gpioSetPullUpDown(BUTTON4_IN, PI_PUD_UP);
gpioSetMode(ENCODER_BTN, PI_INPUT);
gpioGlitchFilter(ENCODER_BTN,100);
gpioSetPullUpDown(ENCODER_BTN, PI_PUD_UP);
//gpioSetMode(TOUCH_INT, PI_INPUT);
//gpioSetPullUpDown(TOUCH_INT, PI_PUD_UP);
gpioSetMode(MASTER_CLK, PI_OUTPUT);