From 9136a494e51933aebf092bc76eb2ef1d9d51813d Mon Sep 17 00:00:00 2001 From: Oleksiy Date: Sat, 15 Mar 2025 11:44:06 +0200 Subject: [PATCH] changed most declarations typedefs. the ones that are not changed yet need to be reviewed in detail --- Software/Gravity/Gravity.ino | 112 +++++++++++++++--------------- Software/Gravity/Interactions.ino | 8 +-- Software/Gravity/UI.ino | 28 ++++---- Software/Gravity/config.h | 4 +- 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/Software/Gravity/Gravity.ino b/Software/Gravity/Gravity.ino index edb655a..027adcc 100644 --- a/Software/Gravity/Gravity.ino +++ b/Software/Gravity/Gravity.ino @@ -11,7 +11,7 @@ #define VERSION "V:1.2A2" -byte memCode = '0'; //Change to different letter if you changed the data structure +uint8_t memCode = '0'; //Change to different letter if you changed the data structure uint16_t CV1Calibration = 512; uint16_t CV2Calibration = 512; @@ -19,17 +19,17 @@ bool showDone = false; const int subDivs[] = { -24, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 24, 32, 64, 128 }; //positive - divide, negative - multiply, 0 - off -byte bpm = 130; -byte bpmModulationChannel = 200; //0 - CV1, 1 - CV2, 255 - OFF -byte bpmModulationRange = 0; +uint8_t bpm = 130; +uint8_t bpmModulationChannel = 200; //0 - CV1, 1 - CV2, 255 - OFF +uint8_t bpmModulationRange = 0; struct channel { - byte mode : 3; //mv: 7. 0 - CLK, 1 - RND, 2 - SEQ, 3 - SWING, 4 - Gate - byte subDiv : 5; //mv: 31 - byte random : 4; //mv: 15 - byte seqPattern : 5; - byte CV1Target : 3; //0 - Off, 1 - Subdiv, 2 - RND, 3 - SeqPattern - byte CV2Target : 3; + uint8_t mode : 3; //mv: 7. 0 - CLK, 1 - RND, 2 - SEQ, 3 - SWING, 4 - Gate + uint8_t subDiv : 5; //mv: 31 + uint8_t random : 4; //mv: 15 + uint8_t seqPattern : 5; + uint8_t CV1Target : 3; //0 - Off, 1 - Subdiv, 2 - RND, 3 - SeqPattern + uint8_t CV2Target : 3; uint8_t swing : 3; uint8_t offset : 7; //mv: 127 uint8_t gate : 7; @@ -48,7 +48,7 @@ channel channels[7] = { //array of channel settings struct sequence { uint32_t sequence; - uint8_t length : 5 ; //don't forget to add 1, values 0 - 31 + uint8_t length : 5 ; //don't forget to add 1 where needed, values 0 - 31 }; sequence sequences[] { @@ -86,63 +86,63 @@ sequence sequences[] { {0b000000000000000000000000000000000, 15} }; -byte currentStep[7] = {0}; +uint8_t currentStep[7] = {0}; int8_t stepNumSelected = 0; -byte patternToEdit; +uint8_t patternToEdit; unsigned int channelPulseCount[7]; unsigned int channelPulsesPerCycle[7]; -byte sixteenthPulseCount = 0; +uint8_t sixteenthPulseCount = 0; int playingModes[7]; // should be renamed to currentSubdivs or something. Updated from channels object on beat and with applied CV modulation int playingModesOld[7]; -unsigned int pulsePeriod; -bool isPlaying;// = false; // replace to something like byte status where 1xxxxxx isPlaying, x1xxxxx isRecording etc +uint16_t pulsePeriod; +bool isPlaying;// = false; // replace to something like uint8_t status where 1xxxxxx isPlaying, x1xxxxx isRecording etc bool isRecording = false; bool recordToNextStep = false; bool MIDIClockReceived = false; -unsigned int tickCount = 0; -unsigned int pulseCount = 0; +uint16_t tickCount = 0; +uint16_t pulseCount = 0; -byte masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - MIDI -byte extClockPPQN = 0; // 0 - 24, 1 - 4 (1/16) -byte extraChannel = 0; // 0 - off, 1 - pulse out = 7th channel -unsigned long lastExtPulseTime = 0; -unsigned long newExtPulseTime = 0; +uint8_t masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - MIDI +uint8_t extClockPPQN = 0; // 0 - 24, 1 - 4 (1/16) +uint8_t extraChannel = 0; // 0 - off, 1 - pulse out = 7th channel +uint32_t lastExtPulseTime = 0; +uint32_t newExtPulseTime = 0; -bool needPulseReset[] = { true, true, true, true, true, true, true }; +bool needPulseReset[] = { true, true, true, true, true, true, true }; //to optimise, can be switched to uint8_t uint16_t gateLengthTime[] = {0, 0, 0, 0, 0, 0, 0}; uint16_t gateCountDown[7]; bool stepIsOdd = 1; -byte displayTab = 0; +uint8_t displayTab = 0; bool insideTab = false; -byte menuItem = 0; +uint8_t menuItem = 0; bool menuItemSelected = false; -byte lastMenuItem = 3; -byte displayScreen = 0; //0 - main, 1 - sequencer, 2 - settings +uint8_t lastMenuItem = 3; +uint8_t displayScreen = 0; //0 - main, 1 - sequencer, 2 - settings bool playBtnPushed = false; bool shiftBtnPushed = false; -int CV1Input = 0; -int CV2Input = 0; +int16_t CV1Input = 0; +int16_t CV2Input = 0; int encPositionOld = 0; -int encDirectionOld = 0; -unsigned long encPressedTime; -unsigned long encReleasedTime; -unsigned long playPressedTime; -unsigned long playReleasedTime; -unsigned long shiftPressedTime; -unsigned long shiftReleasedTime; +uint8_t encDirectionOld = 0; +uint32_t encPressedTime; +uint32_t encReleasedTime; +uint32_t playPressedTime; +uint32_t playReleasedTime; +uint32_t shiftPressedTime; +uint32_t shiftReleasedTime; bool encBtnPushed; int extResetCountdown; int extTriggerCount; -//unsigned long lastInteractionTime; // used for display timeout +//uint32_t lastInteractionTime; // used for display timeout U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE); RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03); @@ -161,7 +161,7 @@ void setup() { pinMode(EXT_INPUT_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(EXT_INPUT_PIN), externalClock, FALLING); - for (byte i = 0; i < 6; i++) { + for (uint8_t i = 0; i < 6; i++) { pinMode(outsPins[i], OUTPUT); } @@ -237,7 +237,7 @@ void clock() { } if (bpmModulationRange != 0) { calculateBPMTiming(); - for (byte i; i < 7; i++) { + for (uint8_t i; i < 7; i++) { if (channels[i].mode == 4) { calculateGate(i); } @@ -263,7 +263,7 @@ void clock() { } // pull low all outputs after set pulse length - for (byte i = 0; i < 7; i++) { + for (uint8_t i = 0; i < 7; i++) { if (channels[i].mode != 4 && tickCount >= PULSE_LENGTH) { //everything but gate mode digitalWrite(outsPins[i], LOW); } else if (channels[i].mode == 4 && tickCount >= gateCountDown[i]) { //gate mode gateLengthTime[i] < pulsePeriod @@ -328,7 +328,7 @@ void externalClock() { void sendTriggers() { //rename to onPulse or something - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { if (playingModes[i] != subDivs[channels[i].subDiv] && playingModesOld[i] != playingModes[i]) { needPulseReset[i] = true; playingModesOld[i] = playingModes[i]; @@ -337,11 +337,11 @@ void sendTriggers() { //rename to onPulse or something //16th notes for sequencer and swing if (sixteenthPulseCount == 0) { stepIsOdd = !stepIsOdd; - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { //pattern modulation int seqMod = 0; - byte seqPattern; + uint8_t seqPattern; if (channels[i].CV2Target == 3) { seqMod = map(CV2Input, -1, 1024, -8, 8); //-1 and 1024 are to try to make the last step not at max value (should make the range from -7 to +7) } else if (channels[i].CV1Target == 3) { @@ -372,7 +372,7 @@ void sendTriggers() { //rename to onPulse or something } } else { sixteenthPulseCount = 0; - for (byte i; i < 7; i++) { + for (uint8_t i; i < 7; i++) { if (channels[i].mode == 2 && currentStep[i] < sequences[channels[i].seqPattern].length) { currentStep[i] ++; } else { @@ -385,7 +385,7 @@ void sendTriggers() { //rename to onPulse or something //switching modes on the beat and resetting channel clock if (pulseCount == 0) { calculateCycles(); - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { if (needPulseReset[i] == true) { channelPulseCount[i] = 0; needPulseReset[i] = false; @@ -397,10 +397,10 @@ void sendTriggers() { //rename to onPulse or something } //multiplier - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { //RND modulation - byte randMod = 0; + uint8_t randMod = 0; if (channels[i].CV1Target == 2) { randMod = randMod + CV1Input; } @@ -410,7 +410,7 @@ void sendTriggers() { //rename to onPulse or something if (channels[i].CV1Target == 2 || channels[i].CV2Target == 2) { randMod = map(randMod, 0, 1023, -5, +5); } - byte randAmount = channels[i].random + randMod; + uint8_t randAmount = channels[i].random + randMod; if (randAmount > 100) { randAmount = 0; } else if (randAmount > 10) { @@ -441,7 +441,7 @@ void sendTriggers() { //rename to onPulse or something void calculateCycles() { - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { int mod = 0; //subdiv modulation happens here if (channels[i].CV1Target == 1) { mod = map(CV1Input, -1, 1024, -5, 5); @@ -476,9 +476,9 @@ void calculateBPMTiming() { } else if (bpmModulationRange != 0 && bpmModulationChannel == 1) { mod = map(CV2Input, 0, 1023, bpmModulationRange * -10, bpmModulationRange * 10); } - byte calcbpm = bpm + mod; + uint8_t calcbpm = bpm + mod; if (calcbpm > MAXBPM) { calcbpm = MAXBPM; }; - pulsePeriod = 600000 / (calcbpm * PPQN); + pulsePeriod = 600000 / (calcbpm * PPQN); //optimisation: try using floats, maybe it'll make it more precise, like 60000.0 / ((float)(calcbpm * PPQN)); } else if (masterClockMode == 1 && extClockPPQN == 1) { //for ext 1/16 clock pulsePeriod = (newExtPulseTime - lastExtPulseTime) * 10 / 6; @@ -486,21 +486,21 @@ void calculateBPMTiming() { } void resetClocks() { - for (byte i = 0; i < (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 0; i < (extraChannel ? 7 : 6); i++) { channelPulseCount[i] = 0; digitalWrite(outsPins[i], LOW); //to avoid stuck leds } pulseCount = 0; tickCount = 0; sixteenthPulseCount = 0; - for (byte i; i < 7; i++) { + for (uint8_t i; i < 7; i++) { currentStep[i] = 0; } stepIsOdd = 1; } void saveState() { - int addr = 0; + uint16_t addr = 0; EEPROM.put(addr, bpm); addr = addr + sizeof(bpm); EEPROM.put(addr, bpmModulationChannel); @@ -529,7 +529,7 @@ void saveState() { void loadState() { //check last bit in eeprom to know if the correct settings were stored if (EEPROM.read(1023) == memCode) { - int addr = 0; + uint16_t addr = 0; EEPROM.get(addr, bpm); addr = addr + sizeof(bpm); EEPROM.get(addr, bpmModulationChannel); diff --git a/Software/Gravity/Interactions.ino b/Software/Gravity/Interactions.ino index ca7a6ab..ee2aaeb 100644 --- a/Software/Gravity/Interactions.ino +++ b/Software/Gravity/Interactions.ino @@ -61,13 +61,13 @@ void checkInputs() { //encoder encoder.tick(); int encPosition = encoder.getPosition(); - int encDirection = (int)(encoder.getDirection()); + uint8_t encDirection = (int)(encoder.getDirection()); if (encPositionOld != encPosition) { - int change = encPositionOld - encPosition; + uint8_t change = encPositionOld - encPosition; if (reverseEnc) { change = change * -1; } - unsigned long ms = encoder.getMillisBetweenRotations(); + uint32_t ms = encoder.getMillisBetweenRotations(); /*if (encDirectionOld == encDirection && ms < 20) { //encoder acceleration change = change * 5; } else if (encDirectionOld == encDirection && ms < 80) { @@ -77,7 +77,7 @@ void checkInputs() { } encDirectionOld = encDirection; if (displayScreen == 0) { - byte channelCV; + uint8_t channelCV; if (!insideTab && !shiftBtnPushed) { //Change tab displayTab = displayTab + change; if (displayTab > 100) { //to address "negative" numbers diff --git a/Software/Gravity/UI.ino b/Software/Gravity/UI.ino index cef1728..b86cf31 100644 --- a/Software/Gravity/UI.ino +++ b/Software/Gravity/UI.ino @@ -2,8 +2,8 @@ void updateScreen() { u8g2.firstPage(); do { - byte leftOffset; - byte width; + uint8_t leftOffset; + uint8_t width; String valueStr; u8g2.setDrawColor(1); @@ -27,7 +27,7 @@ void updateScreen() { lastMenuItem = 1; } - for (byte i = 1; i <= lastMenuItem; i++) { + for (uint8_t i = 1; i <= lastMenuItem; i++) { if (i == 1) { valueStr = F("MODE:"); } else if (i == 2 && masterClockMode == 0) { @@ -48,7 +48,7 @@ void updateScreen() { } //Values - for (byte i = 1; i <= lastMenuItem; i++) { + for (uint8_t i = 1; i <= lastMenuItem; i++) { if (i == 1 && masterClockMode == 0) { //Channel mode valueStr = F("INT"); } else if (i == 1 && masterClockMode == 1) { @@ -127,7 +127,7 @@ void updateScreen() { } width = 32; leftOffset = 62; - for (byte i = 1; i <= lastMenuItem; i++) { + for (uint8_t i = 1; i <= lastMenuItem; i++) { if (i == 1) { valueStr = F("MODE:"); } else if (i == 2 && channels[displayTab - 1].mode == 0) { @@ -149,7 +149,7 @@ void updateScreen() { } //Values - for (byte i = 1; i <= lastMenuItem; i++) { + for (uint8_t i = 1; i <= lastMenuItem; i++) { if (i == 1 && channels[displayTab - 1].mode == 0) { //Channel mode valueStr = F("CLOCK"); } else if (i == 1 && channels[displayTab - 1].mode == 1) { @@ -243,7 +243,7 @@ void updateScreen() { valueStr = valueStr + String(channels[displayTab - 1].seqPattern - 23); } } else if (channels[displayTab - 1].mode == 3) { - byte swingVals[6] = {50, 58, 66, 75, 83, 92}; + uint8_t swingVals[6] = {50, 58, 66, 75, 83, 92}; valueStr = String(swingVals[channels[displayTab - 1].swing]) + "%"; } else if (channels[displayTab - 1].mode == 4) { valueStr = String(channels[displayTab - 1].gate) + "%"; @@ -261,8 +261,8 @@ void updateScreen() { //Tabs u8g2.drawHLine(0, 53, 128); u8g2.setFont(stkS); - byte yPos = 61; - byte xWidth = 12; + uint8_t yPos = 61; + uint8_t xWidth = 12; valueStr = F("w"); if (displayTab == 0) { if (insideTab == true || shiftBtnPushed == true) { @@ -274,7 +274,7 @@ void updateScreen() { u8g2.drawButtonUTF8(xWidth/2, yPos, U8G2_BTN_BW0|U8G2_BTN_HCENTER, xWidth, 0, 2, valueStr.c_str() ); } - for (int i = 1; i <= (extraChannel ? 7 : 6); i++) { + for (uint8_t i = 1; i <= (extraChannel ? 7 : 6); i++) { valueStr = String(i); if (displayTab == i) { if (insideTab == true || shiftBtnPushed == true) { @@ -299,7 +299,7 @@ void updateScreen() { //Edit Pattern Screen (Sequencer) else if (displayScreen == 1) { u8g2.setFont(stkS); - byte pattern = channels[displayTab - 1].seqPattern; + uint8_t pattern = channels[displayTab - 1].seqPattern; if (pattern < 8) { valueStr = F("PATTERN A"); valueStr = valueStr + String(pattern + 1); @@ -327,7 +327,7 @@ void updateScreen() { } u8g2.drawHLine(0, 8, 128); - for (byte i = 0; i <= sequences[patternToEdit].length; i++) { + for (uint8_t i = 0; i <= sequences[patternToEdit].length; i++) { u8g2.drawUTF8(19 + (i % 8)*12, 33 - ((sequences[patternToEdit].length / 8) * 5) + ((i / 8) * 11), (bitRead(sequences[patternToEdit].sequence, i) ? "q" : "p")); } if (!isRecording && stepNumSelected >= 0) { @@ -347,8 +347,8 @@ void updateScreen() { u8g2.drawStr(122 - (u8g2.getStrWidth(version.c_str())), 5, version.c_str() ); u8g2.drawHLine(0, 8, 128); lastMenuItem = 4; - byte width = 112; - for (byte i = 0; i <= lastMenuItem; i++) { + uint8_t width = 112; + for (uint8_t i = 0; i <= lastMenuItem; i++) { switch(i) { case 0: valueStr = F("CALIBRATE CV INS"); diff --git a/Software/Gravity/config.h b/Software/Gravity/config.h index 4773afc..a9a2ae2 100644 --- a/Software/Gravity/config.h +++ b/Software/Gravity/config.h @@ -16,8 +16,8 @@ #define EXT_INPUT_PIN 2 //needs to be an interrupt pin #define ANALOGUE_INPUT_1_PIN A7 #define ANALOGUE_INPUT_2_PIN A6 -const byte clockOutPin = 3; -const byte outsPins[] = { 7, 8, 10, 6, 9, 11, clockOutPin }; +const uint8_t clockOutPin = 3; +const uint8_t outsPins[] = { 7, 8, 10, 6, 9, 11, clockOutPin }; bool rotateScreen = false; bool reverseEnc = false;