Skip to content

Instantly share code, notes, and snippets.

@MitchRatquest
Created July 6, 2018 00:43
Show Gist options
  • Save MitchRatquest/1f63300939ec434454d28bcc7a7cd5c4 to your computer and use it in GitHub Desktop.
Save MitchRatquest/1f63300939ec434454d28bcc7a7cd5c4 to your computer and use it in GitHub Desktop.
organelle clone controller stm32 blue pill
#include <OSCBundle.h>
#include <SLIPEncodedSerial.h>
#include <OSCMessage.h>
SLIPEncodedSerial SLIPSerial(Serial);
// encoder stuff below vvvvv
#define MAXENCODERS 1
volatile uint32_t encstate[MAXENCODERS];
volatile uint32_t encflag[MAXENCODERS];
boolean A_set[MAXENCODERS];
boolean B_set[MAXENCODERS];
volatile uint16_t encoderpos[MAXENCODERS];
volatile uint32_t encodertimer = millis(); // acceleration measurement
uint32_t encoderpinA[MAXENCODERS] = {PB5}; // pin array of all encoder A inputs
uint32_t encoderpinB[MAXENCODERS] = {PB4}; // pin array of all encoder B inputs
uint32_t lastEncoderPos[MAXENCODERS];
// timer
#define ENCODER_RATE 1000 // in microseconds;
HardwareTimer timer(1);
const uint8_t encButton = PA15;
volatile uint8_t previousButton=0;
uint32_t prevTime=0;
//encoder stuff above ^^^^^
//keyboard stuff below vvvv
const uint8_t numberOfButtons=26;
uint8_t keyValuesRaw[numberOfButtons];
uint8_t keyValues[4][numberOfButtons];
uint8_t keyValuesLast[numberOfButtons];
/*
* B12 - EN
* B13 - S0
* B14 - S1
* B15 - S2
* A8 - S3
* A11 - IN1
* A7 - IN2
*/
uint8_t muxEnable = PB12;
uint8_t muxPins[4] = {PB13, PB14, PB15, PA8};
//keyboard stuff above ^^^^^^
//knobs below vvvvv
/*
* PA0-4 are knobs
*/
const uint8_t numberOfKnobs=5;
const uint8_t knobs[numberOfKnobs]={PA0,PA1,PA2,PA3,PA4};
uint32_t knobValues[numberOfKnobs];
//knobs above ^^^^^^
void setup()
{
Serial.begin(115200);
Serial.setTimeout(50);
initEncoders();
initKeyboard();
initKnobs();
}
void loop()
{
getOSC();
sendEncoder();
encoderButton(100);
keyDebounce();
keyEvent();
readKnobs();
}
void getOSC()
{
OSCMessage msgIn; //declares buffer
uint32_t size = SLIPSerial.available();
//uint32_t msgInSize = 0;
while((!SLIPSerial.endofPacket()) && (size > 0))
{
uint32_t size = SLIPSerial.available();
while(size--)
{
msgIn.fill(SLIPSerial.read());
}
}
if (!msgIn.hasError())
{
msgIn.dispatch("/getknobs", sendKnobs, 0);
msgIn.empty();
}
else{msgIn.empty();}
}
void encoderButton( uint16_t delaytime)
{
uint32_t currentTime = millis();
if(currentTime - prevTime >=delaytime) //bad debounce time on crap rotary encoder
{
prevTime = currentTime;
uint8_t currentButton = digitalRead(encButton);
if(currentButton != previousButton)
{
previousButton=currentButton;
OSCMessage msgEncoder("/encbut");
msgEncoder.add((int32_t) currentButton);
SLIPSerial.beginPacket();
msgEncoder.send(SLIPSerial);
SLIPSerial.endPacket();
msgEncoder.empty();
}
}
}
void sendEncoder()
{
int32_t val;
if ((lastEncoderPos[0] != encoderpos[0]))
{
if((lastEncoderPos[0] > encoderpos[0])) val=-1;
else val=1;
OSCMessage msgEncoder("/enc"); //not exactly sure what organelle is sending here?
msgEncoder.add((int32_t) val); // is it a 1 or 0?
SLIPSerial.beginPacket();
msgEncoder.send(SLIPSerial);
SLIPSerial.endPacket();
msgEncoder.empty();
encflag[0] = LOW;
lastEncoderPos[0] = encoderpos[0];
}
}
void readEncoders()
{
for (uint8_t counter = 0; counter < MAXENCODERS; counter++)
{
if ( (gpio_read_bit(PIN_MAP[encoderpinA[counter]].gpio_device, PIN_MAP[encoderpinA[counter]].gpio_bit) ? HIGH : LOW) != A_set[counter] )
{
A_set[counter] = !A_set[counter];
if ( A_set[counter] && !B_set[counter] )
{
if (millis() - encodertimer > 3)
{
encoderpos[counter] += 1;
}
else
encoderpos[counter] += 5;
}
encodertimer = millis();
}
if ( (gpio_read_bit(PIN_MAP[encoderpinB[counter]].gpio_device, PIN_MAP[encoderpinB[counter]].gpio_bit) ? HIGH : LOW) != B_set[counter] )
{
B_set[counter] = !B_set[counter];
if ( B_set[counter] && !A_set[counter] )
if (millis() - encodertimer > 3)
{
encoderpos[counter] -= 1;
}
else
encoderpos[counter] -= 5;
encodertimer = millis();
}
}
}
void initEncoders()
{
pinMode(encButton, INPUT_PULLDOWN);
encodertimer = millis(); // acceleration measurement
for (byte counter = 0; counter < MAXENCODERS; counter++)
{
encstate[counter] = HIGH;
encflag[counter] = HIGH;
A_set[counter] = false;
B_set[counter] = false;
encoderpos[counter] = 0;
pinMode(encoderpinA[counter], INPUT_PULLUP);
pinMode(encoderpinB[counter], INPUT_PULLUP);
lastEncoderPos[counter] = 1;
}
// timer setup for encoder
timer.pause();
timer.setPeriod(ENCODER_RATE); // in microseconds
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
timer.attachCompare1Interrupt(readEncoders);
timer.refresh();
timer.resume();
}
void initKeyboard()
{
pinMode(PB12, OUTPUT); //active low
pinMode(PA11, INPUT_PULLDOWN);
pinMode(PA7, INPUT_PULLDOWN);
for(uint8_t b = 0; b<sizeof(muxPins); b++)
{
pinMode(muxPins[b], OUTPUT);
}
for(uint8_t a=0;a<sizeof(keyValuesLast);a++)
{
keyValuesLast[a] = 0;
}
}
void keyEvent()
{
for(uint8_t x=0;x<numberOfButtons;x++)
{
if (keyValues[0][x] && keyValues[1][x] && keyValues[2][x] && keyValues[3][x])
{
if(!keyValuesLast[x])
{
//Serial.println(x);
OSCMessage msgKey("/key");
msgKey.add((int32_t) x);
msgKey.add((int32_t) 100);
SLIPSerial.beginPacket();
msgKey.send(SLIPSerial);
SLIPSerial.endPacket();
msgKey.empty();
keyValuesLast[x] = 1;
}
}
if (keyValues[0][x]==0 && keyValues[1][x]==0 && keyValues[2][x]==0 && keyValues[3][x]==0)
{
if(keyValuesLast[x])
{
OSCMessage msgKey("/key");
msgKey.add((int32_t) x);
msgKey.add((int32_t) 0);
SLIPSerial.beginPacket();
msgKey.send(SLIPSerial);
SLIPSerial.endPacket();
msgKey.empty();
keyValuesLast[x] = 0;
}
}
}
}
void keyDebounce() //debouncing method from organelle
{
for(uint8_t t=0;t<4;t++)
{
scanKeys();
for(uint8_t x =0;x<numberOfButtons;x++)keyValues[t][x] = keyValuesRaw[x];
}
}
void scanKeys()
{
digitalWrite(muxEnable, LOW); //inhibit active low
for(uint8_t t= 0; t < 16; t++) //mux is 1to16
{
for(uint8_t pins = 0; pins < sizeof(muxPins); pins++)
{
digitalWrite(muxPins[pins], (t>>pins)&1);//basic mux bitmath
}
delayMicroseconds(50); //depending on # of keys pressed, could be 1ma of current (50 seemed stable)
if(digitalRead(PA11)) keyValuesRaw[t] = 1; else keyValuesRaw[t] = 0;
if(digitalRead(PA7)) keyValuesRaw[t+16] = 1; else keyValuesRaw[t+16] = 0;
}
}
void disableMux()
{
digitalWrite(muxEnable, HIGH);//inhibits mux
}
void initKnobs()
{
for(uint8_t x=0;x<numberOfKnobs;x++)
{
pinMode(knobs[x], INPUT_ANALOG);
}
}
void readKnobs()
{
for(uint8_t x=0;x<numberOfKnobs;x++)
{
knobValues[x] = analogRead(knobs[x]);
}
}
void sendKnobs(OSCMessage &msg)
{
OSCMessage msgKnobs("/knobs");
for(uint8_t i = 0; i<6;i++)
{
msgKnobs.add((int32_t) knobValues[i]);
}
SLIPSerial.beginPacket();
msgKnobs.send(SLIPSerial);
SLIPSerial.endPacket();
msgKnobs.empty();
}
#define blue 1
#define green 2
#define red 4
uint8_t BLUELED =11;
uint8_t REDLED= 12;
uint8_t GREENLED =13;
void ledControl(OSCMessage &msg)
{
int stat;
if(msg.isInt(0))
{
stat = msg.getInt(0);
stat %= 8;
uint8_t ledstate=0;
switch (stat)
{
case 0:
break; //ledstate is already off
case 1:
ledstate = blue;
break;
case 2:
ledstate = green;
break;
case 3:
ledstate = blue | green;
break;
case 4:
ledstate = red;
break;
case 5:
ledstate = red | blue;
break;
case 6:
ledstate = red | green;
break;
case 7:
ledstate = red | green | blue;
break;
}
digitalWrite(BLUELED,(ledstate & blue));
digitalWrite(GREENLED,(ledstate & green));
digitalWrite(REDLED,(ledstate & red));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment