From 85b0a02d826d1de0ae034cb91102bdeae59ff534 Mon Sep 17 00:00:00 2001 From: mickabrig7 Date: Wed, 12 Apr 2017 23:28:30 +0200 Subject: [PATCH] Add support for the 6th RGB LED thanks to a Soft PWM RGB library, change the way knobs positions are calculated and add missing "#ifndef... #define" directives in some libraries. --- acrealio/CardDispenser.h | 5 ++ acrealio/Ddr.h | 5 ++ acrealio/IoBoard.cpp | 150 ++++++++++++++++++++++----------------- acrealio/IoBoard.h | 17 +++-- acrealio/LedBoard.h | 5 ++ acrealio/SoftPWMRGB.cpp | 58 +++++++++++++++ acrealio/SoftPWMRGB.h | 23 ++++++ acrealio/acrealio.ino | 1 + acrealio/pinoutconfig.h | 30 +++++--- 9 files changed, 213 insertions(+), 81 deletions(-) create mode 100644 acrealio/SoftPWMRGB.cpp create mode 100644 acrealio/SoftPWMRGB.h diff --git a/acrealio/CardDispenser.h b/acrealio/CardDispenser.h index b095ac8..e4fda4c 100644 --- a/acrealio/CardDispenser.h +++ b/acrealio/CardDispenser.h @@ -1,3 +1,6 @@ +#ifndef CARDDISPENSER_H +#define CARDDISPENSER_H + #include "Arduino.h" #include "Node.h" @@ -15,3 +18,5 @@ private: }; +#endif + diff --git a/acrealio/Ddr.h b/acrealio/Ddr.h index 4747456..551003b 100644 --- a/acrealio/Ddr.h +++ b/acrealio/Ddr.h @@ -1,3 +1,6 @@ +#ifndef DDR_H +#define DDR_H + #include "Arduino.h" #include "Node.h" @@ -15,3 +18,5 @@ private: }; +#endif + diff --git a/acrealio/IoBoard.cpp b/acrealio/IoBoard.cpp index 49496eb..9ec71ef 100644 --- a/acrealio/IoBoard.cpp +++ b/acrealio/IoBoard.cpp @@ -1,7 +1,9 @@ #include "Arduino.h" #include "IoBoard.h" +#include "SoftPWMRGB.h" byte lightPin[] = {LT_START, LT_A, LT_B, LT_C, LT_D, LT_FXL, LT_FXR}; +SoftPWMRGB LED6(LED6_R, LED6_G, LED6_B); //contructor IoBoard::IoBoard(char* rCode) @@ -66,9 +68,15 @@ IoBoard::IoBoard(char* rCode) pinMode(LED3_R, OUTPUT); pinMode(LED3_G, OUTPUT); pinMode(LED3_B, OUTPUT); - - - + pinMode(LED4_R, OUTPUT); + pinMode(LED4_G, OUTPUT); + pinMode(LED4_B, OUTPUT); + pinMode(LED5_R, OUTPUT); + pinMode(LED5_G, OUTPUT); + pinMode(LED5_B, OUTPUT); + pinMode(LED6_R, OUTPUT); + pinMode(LED6_G, OUTPUT); + pinMode(LED6_B, OUTPUT); } void IoBoard::init() @@ -77,19 +85,15 @@ void IoBoard::init() keysLights = 0x00; test =0; + valLED6_R = 0; + valLED6_G = 0; + valLED6_B = 0; + //for volume encoders - aVolRlast=1; - bVolRlast=1; - volR=0; - - aVolLlast=1; - bVolLlast=1; - volL=0; - - - - - + volR = 0; + volRState = 0; + volL = 0; + volLState = 0; } void IoBoard::update() @@ -116,50 +120,58 @@ void IoBoard::update() //read volume encoders - int aVolR = digitalRead(VOLR_A); - int bVolR = digitalRead(VOLR_B); - - if (aVolR != aVolRlast)//position changed + byte volRS = volRState & 3; + + if (digitalRead(VOLR_B)) volRS |= 4; + if (digitalRead(VOLR_A)) volRS |= 8; + + volRState = (volRS >> 2); + + switch (volRS) { - if (bVolR != aVolR) - volR++; - else - volR--; + case 0: case 5: case 10: case 15: + break; + case 1: case 7: case 8: case 14: + volR++; + break; + case 2: case 4: case 11: case 13: + volR--; + break; + case 3: case 12: + volR += 2; + break; + default: + volR -= 2; + break; } - - if (bVolR != bVolRlast)//position changed + + byte volLS = volLState & 3; + + if (digitalRead(VOLL_B)) volLS |= 4; + if (digitalRead(VOLL_A)) volLS |= 8; + + volLState = (volLS >> 2); + + switch (volLS) { - if (bVolR != aVolR) - volR--; - else - volR++; + case 0: case 5: case 10: case 15: + break; + case 1: case 7: case 8: case 14: + volL++; + break; + case 2: case 4: case 11: case 13: + volL--; + break; + case 3: case 12: + volL += 2; + break; + default: + volL -= 2; + break; } - - aVolRlast = aVolR; - bVolRlast = bVolR; - - - int aVolL = digitalRead(VOLL_A); - int bVolL = digitalRead(VOLL_B); - - if (aVolL != aVolLlast)//position changed - { - if (bVolL != aVolL) - volL++; - else - volL--; - } - - if (bVolL != bVolLlast)//position changed - { - if (bVolL != aVolL) - volL--; - else - volL++; - } - - aVolLlast = aVolL; - bVolLlast = bVolL; + + // Update the software PWM RGB light + LED6.setPWM(valLED6_R, valLED6_G, valLED6_B); } @@ -221,15 +233,25 @@ short IoBoard::processRequest(byte* request, byte* answer) //rgb leds - analogWrite(LED1_R,request[5+19]<<1); - analogWrite(LED1_G,request[5+20]<<1); - analogWrite(LED1_B,request[5+21]<<1); - analogWrite(LED2_R,request[5+16]<<1); - analogWrite(LED2_G,request[5+17]<<1); - analogWrite(LED2_B,request[5+18]<<1); - analogWrite(LED3_R,request[5+13]<<1); - analogWrite(LED3_G,request[5+14]<<1); - analogWrite(LED3_B,request[5+15]<<1); + analogWrite(LED1_R,request[5+4]<<1); + analogWrite(LED1_G,request[5+5]<<1); + analogWrite(LED1_B,request[5+6]<<1); + analogWrite(LED2_R,request[5+7]<<1); + analogWrite(LED2_G,request[5+8]<<1); + analogWrite(LED2_B,request[5+9]<<1); + analogWrite(LED3_R,request[5+10]<<1); + analogWrite(LED3_G,request[5+11]<<1); + analogWrite(LED3_B,request[5+12]<<1); + analogWrite(LED4_R,request[5+13]<<1); + analogWrite(LED4_G,request[5+14]<<1); + analogWrite(LED4_B,request[5+15]<<1); + analogWrite(LED5_R,request[5+16]<<1); + analogWrite(LED5_G,request[5+17]<<1); + analogWrite(LED5_B,request[5+18]<<1); + // Get the requested values for the software PWM RGB light + valLED6_R = request[5+19]<<1; + valLED6_G = request[5+20]<<1; + valLED6_B = request[5+21]<<1; /* input control format: byte 0 : 8 high bits of L vol diff --git a/acrealio/IoBoard.h b/acrealio/IoBoard.h index 6ff1212..9348500 100644 --- a/acrealio/IoBoard.h +++ b/acrealio/IoBoard.h @@ -1,3 +1,6 @@ +#ifndef IOBOARD_H +#define IOBOARD_H + #include "Arduino.h" #include "Node.h" @@ -20,13 +23,15 @@ private: boolean test; boolean svc; //for volume encoders - int aVolRlast; - int bVolRlast; unsigned int volR; - - int aVolLlast; - int bVolLlast; + byte volRState; unsigned int volL; - + byte volLState; + // Values for the software PWM RGB light + int valLED6_R; + int valLED6_G; + int valLED6_B; }; +#endif + diff --git a/acrealio/LedBoard.h b/acrealio/LedBoard.h index 88ab54b..4b3e108 100644 --- a/acrealio/LedBoard.h +++ b/acrealio/LedBoard.h @@ -1,3 +1,6 @@ +#ifndef LEDBOARD_H +#define LEDBOARD_H + #include "Arduino.h" #include "Node.h" @@ -16,3 +19,5 @@ private: }; +#endif + diff --git a/acrealio/SoftPWMRGB.cpp b/acrealio/SoftPWMRGB.cpp new file mode 100644 index 0000000..4abdf58 --- /dev/null +++ b/acrealio/SoftPWMRGB.cpp @@ -0,0 +1,58 @@ +#include "Arduino.h" +#include "SoftPWMRGB.h" + +SoftPWMRGB::SoftPWMRGB(int pinR, int pinG, int pinB) +{ + period = 2000; // Period set to 2000us to obtain a 500Hz signal) + + pin[0] = pinR; + pin[1] = pinG; + pin[2] = pinB; + + for(int i=0;i<3;i++) + { + val[i] = 0; + previousVal[i] = -1; + onTime[i] = 0; + offTime[i] = 0; + previousMicros[i] = 0; + state[i] = LOW; + } +} + +void SoftPWMRGB::setPWM(int valR, int valG, int valB) +{ + unsigned long currentMicros = micros(); // Get the current time + + val[0] = valR; + val[1] = valG; + val[2] = valB; + + for(int i=0;i<3;i++) + { + if(previousVal[i] != val[i]) // Only calculate the ON and OFF time if the value has changed + { + onTime[i] = (period*(unsigned long)val[i])/255UL; // Not sure if that much variable type casting is necessary, but it works + offTime[i] = period - onTime[i]; + + previousVal[i] = val[i]; + } + + if(state[i] == LOW && currentMicros - previousMicros[i] >= offTime[i] && offTime[i] != period) // If the LED is OFF AND we exceeded the OFF time AND the OFF time isn't equal to the period, turn the LED ON + { + state[i] = HIGH; + previousMicros[i] = currentMicros; + } + else if(state[i] == HIGH && currentMicros - previousMicros[i] >= onTime[i] && onTime[i] != period) // Else, if the LED is ON AND we exceeded the ON time AND the ON time isn't equal to the period, turn the LED OFF + { + state[i] = LOW; + previousMicros[i] = currentMicros; + } + } + + // Write to the pins + digitalWrite(pin[0], state[0]); + digitalWrite(pin[1], state[1]); + digitalWrite(pin[2], state[2]); +} + diff --git a/acrealio/SoftPWMRGB.h b/acrealio/SoftPWMRGB.h new file mode 100644 index 0000000..79ddb6b --- /dev/null +++ b/acrealio/SoftPWMRGB.h @@ -0,0 +1,23 @@ +#ifndef SOFTPWMRGB_H +#define SOFTPWMRGB_H + +class SoftPWMRGB +{ +public: + SoftPWMRGB(int pinR, int pinG, int pinB); + void setPWM(int valR, int valG, int valB); // Call this function in a loop to be sure that LED states are updated + +private: + unsigned long period; // Period of the PWM signal in microseconds + + int pin[3]; // We'll store the RGB LED's pins here + int val[3]; // We'll store our requested RGB values here + int previousVal[3]; // Used to check if the value has changed + unsigned long onTime[3]; // Calculated ON times + unsigned long offTime[3]; // Calculated OFF times + unsigned long previousMicros[3]; // Used to keep track of time between each change of state + int state[3]; // We'll store the RGB LEDs states here +}; + +#endif + diff --git a/acrealio/acrealio.ino b/acrealio/acrealio.ino index 48cb325..098fad8 100644 --- a/acrealio/acrealio.ino +++ b/acrealio/acrealio.ino @@ -2,6 +2,7 @@ #include "Reader.h" #include "LedBoard.h" #include "IoBoard.h" +#include "SoftPWMRGB.h" #include "CardDispenser.h" #include "RR10.h" #include "SL015M.h" diff --git a/acrealio/pinoutconfig.h b/acrealio/pinoutconfig.h index b9829da..9a29d83 100644 --- a/acrealio/pinoutconfig.h +++ b/acrealio/pinoutconfig.h @@ -18,24 +18,24 @@ #define BT_B A3 #define BT_C A4 #define BT_D A5 -#define BT_START 50 -#define BT_TEST 52 -#define BT_SVC 40 +#define BT_START 52 +#define BT_TEST 50 +#define BT_SVC 48 //input pins for volume encoders (phase A and phase B for each) -#define VOLR_A 42 -#define VOLR_B 44 -#define VOLL_A 46 -#define VOLL_B 48 +#define VOLR_A 36 +#define VOLR_B 38 +#define VOLL_A 40 +#define VOLL_B 42 //pins for sdvx lights (outputs) #define LT_START 53 #define LT_A 51 #define LT_B 49 #define LT_C 47 -#define LT_D 45 -#define LT_FXL 43 -#define LT_FXR 41 +#define LT_D 43 +#define LT_FXL 41 +#define LT_FXR 39 //pins for sdvx/jubeat RGB LEDs (use PWM pins) #define LED1_R 7 @@ -50,9 +50,17 @@ #define LED3_G 12 #define LED3_B 11 +#define LED4_R 4 +#define LED4_G 3 +#define LED4_B 2 +#define LED5_R 46 +#define LED5_G 45 +#define LED5_B 44 - +#define LED6_R A8 +#define LED6_G A9 +#define LED6_B A10 //pins for card reader 1 keypad (colls ABC, rows 1234) /* ### Keypad 3x4 Matrix ###