-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchScreenMIDIController.ino
More file actions
139 lines (119 loc) · 5.81 KB
/
Copy pathTouchScreenMIDIController.ino
File metadata and controls
139 lines (119 loc) · 5.81 KB
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
#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_FT6206.h> // this is needed for FT6206 but we need this version from Blackkettler: https://github.com/blackketter/Adafruit_FT6206_Library (This will be fixed by this https://github.com/adafruit/Adafruit_FT6206_Library/pull/5)
#include <Adafruit_ILI9341.h> // this is needed for the display
#include <MIDI.h> // MIDI stuff
#include "Zac7.c"
#define BACKGROUND_COLOR ILI9341_WHITE // colour of the background
Adafruit_FT6206 TouchScreen = Adafruit_FT6206(); //create an instance of the touchscreen
Adafruit_ILI9341 tft = Adafruit_ILI9341(9, 7); //create an instance of the display (9 is the chip select pin and 7 is the data / control pin)
TS_Point p = TS_Point(0, 0, 0); //create a point for the point of touch
TS_Point p_old = TS_Point(0, 0, 0); //create a point for the previous point of touch
MIDI_CREATE_DEFAULT_INSTANCE(); //create an instance for MIDI data to be sent over
int CurrentMode = 0;
int PreviousMode = 0;
int CurrentNote = 0;
int PreviousNote = 0;
void setup(void) {
tft.begin(); //initalize the display
tft.setTextSize(2); //set the display text size
tft.setTextColor(ILI9341_BLACK); //set the display color
tft.fillScreen(BACKGROUND_COLOR); //set the display background colour
TouchScreen.begin(30); // pass in sensitivity coefficient and initalize the touch screen
MIDI.begin(4); //initalize the MIDI
pinMode(53, OUTPUT);
digitalWrite(53, HIGH); //Allows us to get 5V from pin 53, which is conveniently next to GND
}
void loop() {
uint32_t extraBits=0; // use an unsigned integer or the bit shifting goes wonky
for (int k = 0; k< 64; k++) {
extraBits = extraBits + analogRead(15);
}
int PotReading = ((float)extraBits / (float) 64);
if (PotReading <= 4) CurrentMode = 0;
if (PotReading > 4) CurrentMode = 1;
if (PotReading > 64) CurrentMode = 2;
if (PotReading > 128) CurrentMode = 3;
if (PotReading > 256) CurrentMode = 4;
if (PotReading > 384) CurrentMode = 5;
if (PotReading > 512) CurrentMode = 6;
if (PotReading > 640) CurrentMode = 7;
if (PotReading > 770) CurrentMode = 8;
if (PotReading > 950) CurrentMode = 9;
if (PotReading > 1005) CurrentMode = 10;
if(CurrentMode != PreviousMode){ //if we have changed mode
tft.fillScreen(BACKGROUND_COLOR); //reset the screen to white
tft.setCursor(150 , 190); //set the location of the text output of the mode string
tft.print("Mode " + String(CurrentMode)); //print it
if(CurrentMode == 9) {
PrintZac();
Piano();
}
PreviousMode = CurrentMode; //set the Previous Mode to the Current Mode
}
switch(CurrentMode){
case 9:
PianoListener();
break;
case 10:
tft.fillScreen(ILI9341_BLACK); //set the screen to black
break;
default:
MIDIpad(midi::EffectControl1, midi::EffectControl2, CurrentMode);
break;
}
}
void MIDIpad(int Channel1, int Channel2, int CtrlNumber) {
if ( TouchScreen.touched()) { //if the touch screen was touched this loop
p = TouchScreen.getPoint(); //get the point of the touch
if(p != p_old){ //if the point of touch has changed
int Effect1 = ((float)p.x / (float)ILI9341_TFTWIDTH * (float)127); //work out the new modulations
int Effect2 = ((float)p.y / (float)ILI9341_TFTHEIGHT * (float)127);//work out the new modulations
//pitch, velo, channel
//"1" is the unused(?) data 1 value
MIDI.send(midi::ControlChange, CtrlNumber, (int)Effect1, Channel1); //send the MIDI effect amount 1 on EffectControl1
MIDI.send(midi::ControlChange, CtrlNumber, (int)Effect2, Channel2); //send the MIDI effect amount 2 on EffectControl2
tft.drawFastHLine(0, p_old.y, ILI9341_TFTWIDTH, BACKGROUND_COLOR); //remove the old line by drawing over it
tft.drawFastVLine(p_old.x, 0, ILI9341_TFTHEIGHT, BACKGROUND_COLOR); //remove the old line by drawing over it
tft.drawFastHLine(0, p.y, ILI9341_TFTWIDTH, ILI9341_BLACK); //draw a new line
tft.drawFastVLine(p.x, 0, ILI9341_TFTHEIGHT, ILI9341_BLACK); //draw a new line
// Print out raw data from screen touch controller
tft.fillRect(150, 150, 36, 36, BACKGROUND_COLOR); //remove the previous effect numbers
tft.setCursor(150 , 150); //set the location of the text output of Effect1
tft.print(Effect1); //print it
tft.setCursor(150 , 170); //set the location of the text output of Effect2
tft.print(Effect2); //print it
tft.setCursor(150 , 190); //set the location of the text output of the mode string
tft.print("Mode " + String(CurrentMode)); //print the mode
}
p_old = p; //set the old point to the current point
}
}
void Piano(){
int KeyWidth = (float)ILI9341_TFTHEIGHT / (float)8;
int KeyHeight = (float)ILI9341_TFTWIDTH / (float)2;
for(int i = 0; i < 8; i++){
tft.fillRect(0, i*KeyWidth, KeyHeight, KeyWidth, ILI9341_BLACK);
tft.fillRect(5, i*KeyWidth + 5, KeyHeight - 5, KeyWidth - 5, ILI9341_WHITE);
}
}
void PianoListener(){
int KeyWidth = (float)ILI9341_TFTHEIGHT / (float)8;
int KeyHeight = (float)ILI9341_TFTWIDTH / (float)2;
if ( TouchScreen.touched()) { //if the touch screen was touched this loop
p = TouchScreen.getPoint(); //get the point of the touch
CurrentNote = floor((float)p.y / (float)ILI9341_TFTHEIGHT * (float)8);
if(CurrentNote != PreviousNote){
MIDI.sendNoteOff(30 + PreviousNote, 127, 1);
tft.fillRect(5, CurrentNote*KeyWidth + 5, KeyHeight - 5, KeyWidth - 5, ILI9341_RED);
MIDI.sendNoteOn(30 + CurrentNote, 127, 1);
}
PreviousNote = CurrentNote;
}
else{
MIDI.sendNoteOff(30 + PreviousNote, 127, 1);
}
}
void PrintZac(){
tft.drawRGBBitmap(ILI9341_TFTWIDTH/2 - 110, ILI9341_TFTHEIGHT/2 - 120, Zac7, 110, 120); //http://www.rinkydinkelectronics.com/t_imageconverter565.php
}