|
| 1 | +#include <SPI.h> |
| 2 | +#include <Wire.h> |
| 3 | +#include <avr/io.h> |
| 4 | +#include <avr/interrupt.h> |
| 5 | +#include <HardwareSerial.h> |
| 6 | +#include <Adafruit_GFX.h> |
| 7 | +#include <Adafruit_SSD1306.h> |
| 8 | + |
| 9 | +/* RN-52 Bits for S%, 1EF6 |
| 10 | + My bits: 001 1110 1111 0110 -> to HEX -> 0x1EF6 |
| 11 | + Set with "S%,1EF6" |
| 12 | +
|
| 13 | + [0] Bit 0 - Enable AVRCP |
| 14 | + [1] Bit 1 - Enable reconnect on power-on |
| 15 | + [1] Bit 2 - bluetooth discoverable on start |
| 16 | + [0] Bit 3 - Coded indicators |
| 17 | + |
| 18 | + [1] Bit 4 - Reboot after disconnect |
| 19 | + [1] Bit 5 - Mute vol up/down tones |
| 20 | + [1] Bit 6 - Enable voice command on PIO4 |
| 21 | + [1] Bit 7 - Disable system tones |
| 22 | + |
| 23 | + [0] Bit 8 - Power off after pairing timeout |
| 24 | + [1] Bit 9 - Reset after power off |
| 25 | + [1] Bit 10 - Enable list reconnect after panic |
| 26 | + [1] Bit 11 - Enable latch even indicator PIO2 (used for holding interrupt low) |
| 27 | + |
| 28 | + [1] Bit 12 - Enable track change event |
| 29 | + [0] Bit 13 - Enable tones playback at fixed vol |
| 30 | + [0] Bit 14 - Enable auto-accept |
| 31 | +*/ |
| 32 | + |
| 33 | +#define OLED_RESET 4 |
| 34 | +Adafruit_SSD1306 display(OLED_RESET); |
| 35 | + |
| 36 | +#define DELTAY 2 |
| 37 | +#define HEART 23 |
| 38 | +#define SEN 22 |
| 39 | +#define LED 2 |
| 40 | + |
| 41 | +void setup() { |
| 42 | + Serial.begin(38400); |
| 43 | + Serial2.begin(9600); |
| 44 | + Serial4.begin(38400); |
| 45 | + |
| 46 | + Serial4.println("Q"); |
| 47 | + delay(100); |
| 48 | + |
| 49 | + pinMode(SEN, INPUT); |
| 50 | + pinMode(HEART, OUTPUT); |
| 51 | + pinMode(LED, OUTPUT); |
| 52 | + pinMode(30, INPUT); |
| 53 | + |
| 54 | + // interrupt for rn-52 pin 3. |
| 55 | + attachInterrupt(30, change, FALLING); |
| 56 | + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); |
| 57 | + |
| 58 | + digitalWrite(LED, HIGH); |
| 59 | + digitalWrite(HEART, HIGH); |
| 60 | + delay(1000); |
| 61 | + digitalWrite(LED, LOW); |
| 62 | + digitalWrite(HEART, LOW); |
| 63 | + |
| 64 | + delay(1000); |
| 65 | + display.clearDisplay(); |
| 66 | + display.setTextSize(1); |
| 67 | + display.setTextColor(WHITE); |
| 68 | + display.println("Initializing ..."); |
| 69 | + display.display(); |
| 70 | + Serial.println("My Ports are Ready"); |
| 71 | +} |
| 72 | + |
| 73 | +String meta = ""; |
| 74 | + |
| 75 | +void loop() { |
| 76 | + _time = millis(); |
| 77 | + char sequenceBytes[80]; |
| 78 | + char checkSum; |
| 79 | + char serialByte; |
| 80 | + String resp; |
| 81 | + |
| 82 | + delay(1000); |
| 83 | + digitalWrite(23, digitalRead(30)); // 30 -> pin 3 from rn-52 |
| 84 | + //if (!digitalRead(30)) { |
| 85 | + // change(); |
| 86 | + //} |
| 87 | +} |
| 88 | + |
| 89 | +void change() { |
| 90 | + long stat = getStatus(); |
| 91 | + if (stat) { |
| 92 | + meta = getMetaData(); |
| 93 | + display.clearDisplay(); |
| 94 | + display.setCursor(0,0); |
| 95 | + display.print(trackInfo("Title")); |
| 96 | + display.setCursor(0,24); |
| 97 | + display.print(trackInfo("Artist")); |
| 98 | + display.setCursor(0,36); |
| 99 | + display.print(trackInfo("Album")); |
| 100 | + display.display(); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + while (Serial4.available()) { |
| 105 | + char q = Serial4.read(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +String trackInfo(String type) { |
| 110 | + int n = meta.indexOf(type) + type.length() + 1; |
| 111 | + String _meta; |
| 112 | + |
| 113 | + if (n != -1) { |
| 114 | + _meta = meta.substring(n); |
| 115 | + n = _meta.indexOf('\n'); |
| 116 | + _meta = _meta.substring(0, n); |
| 117 | + } |
| 118 | + else { |
| 119 | + _meta = ""; |
| 120 | + } |
| 121 | + |
| 122 | + Serial.println(_meta); |
| 123 | + return _meta; |
| 124 | +} |
| 125 | + |
| 126 | +short getStatus() { |
| 127 | + short stat = 0; |
| 128 | + char c; |
| 129 | + |
| 130 | + while (Serial4.available()) { |
| 131 | + char q = Serial4.read(); |
| 132 | + } |
| 133 | + delay(50); |
| 134 | + |
| 135 | + Serial4.println("Q"); |
| 136 | + while(Serial4.available() == 0); |
| 137 | + |
| 138 | + while (c != '\r') { |
| 139 | + while (Serial4.available() == 0) { |
| 140 | + delay(5); |
| 141 | + } |
| 142 | + |
| 143 | + c = Serial4.read(); |
| 144 | + |
| 145 | + if (c >= '0' && c <= '9') { |
| 146 | + stat *= 16; |
| 147 | + stat += (c - '0'); |
| 148 | + } |
| 149 | + else if (c >= 'A' && c <= 'F') { |
| 150 | + stat *= 16; |
| 151 | + stat += (c - 'A') + 10; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + Serial.println(stat, HEX); |
| 156 | + return stat; |
| 157 | +} |
| 158 | + |
| 159 | +String getMetaData() { |
| 160 | + String _meta; |
| 161 | + int i = 6; |
| 162 | + long count; |
| 163 | + |
| 164 | + while (Serial4.available() > 0) { |
| 165 | + char q = Serial4.read(); |
| 166 | + } |
| 167 | + delay(100); |
| 168 | + |
| 169 | + Serial4.println("AD"); |
| 170 | + |
| 171 | + while(Serial4.available() == 0); |
| 172 | + |
| 173 | + while(i != 0) { |
| 174 | + if (Serial4.available() > 0) { |
| 175 | + char c = Serial4.read(); |
| 176 | + count = millis(); |
| 177 | + _meta += c; |
| 178 | + if (c == '\n') i--; |
| 179 | + } |
| 180 | + if ((millis() - count) > 500) i--; |
| 181 | + } |
| 182 | + |
| 183 | + meta = _meta; |
| 184 | + return meta; |
| 185 | +} |
0 commit comments