cvalente

10ch.ino

May 30th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 13.05 KB | None | 0 0
  1. /*
  2.  *  10 lights
  3.  *  -------------
  4.  *  Summer project with Stu, lighting console for 10 LEDs
  5.  *
  6.  *  Code by Carlos Valente
  7.  *  
  8.  *  TODO:
  9.  *  Gen
  10.  *  - debug modes: verbose
  11.  *  Functionality
  12.  *  - the highlight button
  13.  *  INPUT
  14.  *  - ADC prescaler
  15.  */
  16.  
  17. #define DEBUG
  18. #include "def.h"
  19.  
  20. /* Gen - State Machine */
  21. enum states {MODE_1, MODE_2, MODE_3};
  22. uint8_t state = MODE_1;
  23. uint32_t timeNow, lastBtnRead;
  24. bool bInitSequence;
  25.  
  26. /* Gen - Lighting States */
  27. uint8_t lightingData[NUM_CUES][NUM_FADERS];
  28. uint8_t selectedCue, runningCue;
  29. uint32_t fadeTime;
  30. bool bFading;               // initiate fade
  31. bool bRecordCue;            // record fader data in cue
  32. bool bClearEEPROM;          // reset EEPROM data
  33. bool bClearRunningData;     // clear running data
  34. bool bClearIndicators;      // clear indicator LEDs
  35. bool bClearTimer;           // clear timing indicator LEDs
  36. bool bModeChanged;          // mode change flag
  37. bool bCueChanged;           // cue change flag
  38. bool bModeSelect;           // mode select flag
  39. bool bStandby;              // standby mode, no cue
  40.  
  41. /* Gen - Pin Values Buttons */
  42. bool store, back, go;
  43. bool prevStore, prevBack, prevGo;
  44. uint32_t lastStore, lastBack, lastGo;
  45.  
  46. /* Gen - Pin Values Faders */
  47. uint8_t faderValues[NUM_FADERS];     // values from faders at each iteration
  48. uint8_t values[NUM_FADERS];          // values for output
  49. uint8_t zeroValue[NUM_FADERS];       // initial fade values
  50. UILed   cueLeds[NUM_CUES];           // values for indicator leds
  51. uint8_t timerLeds[NUM_TIMER];        // values for timing indicators
  52.  
  53. /* Include code files */
  54. #include "io.h"
  55. #include "eeprom.h"
  56.  
  57.  
  58. void setup(){
  59.     /* Pin Assignments */
  60.     init_io();
  61.  
  62.      /* LED initialize */
  63.     init_UILed();
  64.  
  65.     /* Serial */
  66.     Serial.begin(SERIAL_BAUD);
  67.  
  68.     /* initialize data from EEPROM memory */
  69.     init_from_eeprom(EEPROM_ADDRESS);
  70.  
  71.     /* Initialize aux */
  72.     selectedCue = runningCue = 0;    // runningCue used in mode 3
  73.  
  74.     bRecordCue = bClearEEPROM = bClearRunningData = false;
  75.     bClearIndicators = bClearTimer = bModeChanged = bModeSelect = false;
  76.     bInitSequence = bCueChanged = true;
  77.  
  78.     lastBtnRead = 0;                // button sw debounce
  79. }
  80.  
  81. void loop(){
  82.     if (bInitSequence)
  83.         init_sequence(UI_SLOW);     // init sequence on startup
  84.  
  85.     timeNow = millis();             // get iteration time
  86.     read_inputs();                  // this should only happen if needed
  87.     loop_execute( check_mode() );   // call state machine
  88.     called_actions();               // run user actions
  89.     refresh_outputs();              // refresh LEDs
  90.  
  91.     /* iterate flags */
  92.     prevGo      = go;
  93.     prevBack    = back;
  94.     prevStore   = store;
  95.  
  96.     /* reset flags */
  97.     bRecordCue          = false;
  98.     bClearEEPROM        = false;
  99.     bClearRunningData   = false;
  100.     bClearIndicators    = false;
  101.     bClearTimer         = false;
  102.     bModeChanged        = false;
  103.     bCueChanged         = false;
  104.     bInitSequence       = false;
  105.     bModeSelect         = false;
  106.  
  107.     /* aux */
  108.     DEBUG_PLOT("\n");
  109. }
  110.  
  111. void init_sequence(int time){
  112.     /* on init all leds flash in order
  113.      * all pins by hand, set visually  */
  114.  
  115.     // cue indicators
  116.     uint8_t pinOrder1[NUM_CUES] = {22,27,23,28,24,31,26,30,25,29};
  117.     for (int i = 0; i < NUM_CUES; ++i) {
  118.         blink(pinOrder1[i], time);
  119.     }
  120.  
  121.     // PWM lights
  122.     uint8_t pinOrder2[NUM_FADERS] = {2,3,4,5,6,7,8,9,10,11,12};
  123.     for (int i = 0; i < NUM_FADERS; ++i) {
  124.         fade(pinOrder2[i], time * 2);
  125.     }
  126.  
  127.     // mode and timing indicators
  128.     uint8_t pinOrder3[NUM_MODES + NUM_TIMER] = {19,20,21,41,40,39,38,37,36,35,34,33,32};
  129.     for (int i = 0; i < NUM_MODES + NUM_TIMER; ++i) {
  130.         blink(pinOrder3[i], time);
  131.     }
  132. }
  133.  
  134. uint8_t check_mode(){
  135.     /* reads mode selection input */
  136.     uint8_t called_mode = state;
  137.     bool bStartFade = false;
  138.  
  139.     // store changes between modes, check everytime
  140.     if (store) {
  141.         // Store button is pressed
  142.         if (prevStore) {
  143.             // Store button was pressed in previous iteration
  144.             if (timeNow - lastStore > ACTION_TIME) {
  145.                 /* Store button has been pressed long enough to call action,
  146.                  * Enter cycle Mode */
  147.                 bModeSelect = true;
  148.  
  149.                 if (go & !prevGo) {
  150.                     called_mode = (called_mode + 1) % NUM_MODES;
  151.                     bModeChanged = bCueChanged = true;
  152.                 }
  153.                 if (back & !prevBack) {
  154.                     called_mode --;
  155.                     bModeChanged = bCueChanged = true;
  156.                 }
  157.                 if (called_mode >= NUM_MODES)   called_mode = NUM_MODES - 1;
  158.                
  159.                 return called_mode;
  160.             }
  161.         } else {
  162.             // start counter
  163.             lastStore = timeNow;
  164.         }
  165.     } else {
  166.         if (prevStore && (called_mode == MODE_2)) {
  167.             bRecordCue = true;
  168.         }
  169.     }
  170.  
  171.     if (called_mode == MODE_1) {
  172.         // delete only in mode 1
  173.         if (back && go) {
  174.             // both buttons are pressed
  175.             if (!prevGo && !prevBack) {
  176.                 // were not pressed before, start counting
  177.                 lastGo = lastBack = timeNow;
  178.             } else {
  179.                 // pressed in previous iteration, check time
  180.                 if ((timeNow - lastGo > ACTION_TIME) && (timeNow - lastBack > ACTION_TIME)) {
  181.                     // if both buttons where pressed for ACTION TIME we call clear
  182.                     bClearEEPROM = true;
  183.                     bClearRunningData = true;
  184.                     // and reset flags
  185.                     back = go = false;
  186.                 }
  187.             }
  188.         }
  189.     } else {
  190.         // go and go backwards only work on mode 2 and 3
  191.  
  192.         // go backwards
  193.         if (back && !prevBack) {
  194.             if (!bStandby) {
  195.                 selectedCue -= 1;
  196.                 if (selectedCue >= NUM_CUES) {
  197.                     selectedCue = NUM_CUES - 1;
  198.                 }
  199.             }
  200.             if (called_mode == MODE_3) bStartFade = true;
  201.             bCueChanged = true;
  202.         }
  203.  
  204.         // go forwards
  205.         if (go && !prevGo) {
  206.             if (!bStandby) {
  207.                 selectedCue = (selectedCue + 1) % NUM_CUES;
  208.             }
  209.             if (called_mode == MODE_3) bStartFade = true;
  210.             bCueChanged = true;
  211.         }
  212.  
  213.         if (bStartFade) {
  214.             memcpy (zeroValue, values, sizeof(zeroValue));       // initial fade values
  215.             lastGo = timeNow;
  216.             bFading = true;
  217.             bStandby = false;
  218.         }
  219.     }
  220.     return called_mode;
  221. }
  222.  
  223. void loop_execute(uint8_t called_mode){
  224.  
  225.     if (called_mode < 3) state = called_mode;
  226.  
  227.     DEBUG_PRINT("Mode: ");
  228.     DEBUG_PRINTLN(state + 1);
  229.  
  230.     switch (state) {
  231.  
  232.         case MODE_1:        // Fader is value
  233.             // call clear indicator LEDs
  234.             if (bModeChanged) bClearIndicators = true;
  235.  
  236.             // calculate values to pass
  237.             for (int i = 1; i < NUM_FADERS; ++i) {
  238.                 values[i] = cap(faderValues[i], faderValues[0]);
  239.             }
  240.             values[0] = faderValues[0]; // master not affected
  241.  
  242.             // calculate values for time indicator LEDs
  243.             leds_from_value(values[0]);
  244.  
  245.         break;
  246.  
  247.         case MODE_2:        // Record
  248.             // mode change resets cue select
  249.             if (bModeChanged) {
  250.                 selectedCue = 0;
  251.                 bStandby = false;
  252.             }
  253.             runningCue = selectedCue;       // no fading in mode 2
  254.  
  255.             // calculate values for leds
  256.             for (int i = 1; i < NUM_FADERS; ++i) {
  257.                 values[i] = faderValues[i];
  258.             }
  259.             values[0] = faderValues[0]; // master not affected
  260.  
  261.             // calculate values for time indicator LEDs
  262.             leds_from_value(values[0]);
  263.  
  264.         break;
  265.  
  266.         case MODE_3:        // Playback
  267.  
  268.             float step = 1.0f;
  269.  
  270.             // mode initializes from empty
  271.             if (bModeChanged) {
  272.                 // need to have an extra uint for running cue
  273.                 memset(values, 0, sizeof(values));  // clear running values at start
  274.                 bClearIndicators = bCueChanged = true;
  275.                 selectedCue = runningCue = 0;
  276.                 bFading  = false;
  277.                 bStandby = true;
  278.             }
  279.  
  280.             // calculate cue transition position
  281.             if (bFading) {      // this should be replaced with proper maths
  282.                 uint32_t fadeTimeElapsed;
  283.  
  284.                 fadeTime = lightingData[selectedCue][0];
  285.                 DEBUG_PRINT("Cue fade time (decimal)");
  286.                 DEBUG_PRINT(fadeTime);
  287.  
  288.                 fadeTime = map(fadeTime, 0, 255, 0, MAX_FADE);
  289.                 DEBUG_PRINT(" corresponding to (ms) ");
  290.                 DEBUG_PRINTLN(fadeTime);
  291.  
  292.                 fadeTimeElapsed = timeNow - lastGo;
  293.  
  294.                 if (fadeTimeElapsed < fadeTime) {
  295.                     leds_from_time_delta(fadeTimeElapsed, fadeTime);
  296.                     step = ((float)fadeTimeElapsed / fadeTime);
  297.                    
  298.                     DEBUG_PRINT("Fading ");
  299.                     DEBUG_PRINT(step);
  300.                     DEBUG_PRINT(" percent of ");
  301.                     DEBUG_PRINT(fadeTimeElapsed);
  302.                     DEBUG_PRINT(" out of  ");
  303.                     DEBUG_PRINTLN(fadeTime);
  304.                 } else {
  305.                     bFading = false;
  306.                     bCueChanged = true;
  307.                     runningCue = selectedCue;
  308.                 }
  309.             }
  310.  
  311.             // calculate values to pass
  312.             for (int i = 1; i < NUM_FADERS; ++i) {
  313.                 if (bStandby) {
  314.                     values[i] = cap(faderValues[i], faderValues[0]);
  315.                 } else {
  316.                     uint8_t target = lightingData[selectedCue][i];
  317.                     uint8_t v;
  318.  
  319.                     if (bFading) {
  320.                         v = (1 - step) * zeroValue[i] + step * target;
  321.                         DEBUG_PRINT("Channel ");
  322.                         DEBUG_PRINT(i);
  323.                         DEBUG_PRINT(" Currently at ");
  324.                         DEBUG_PRINT(values[i]);
  325.                         DEBUG_PRINT(" fading to: ");
  326.                         DEBUG_PRINT(v);
  327.                         DEBUG_PRINT(" of target ");
  328.                         DEBUG_PRINTLN(target);
  329.                     }
  330.                     values[i] = largest(v , cap(faderValues[i], faderValues[0] ) );
  331.                 }
  332.             }
  333.             values[0] = faderValues[0]; // master not affected
  334.         break;
  335.     }
  336.  
  337.     if (bStandby) {
  338.         DEBUG_PRINTLN("Standing by for cue");
  339.     } else {
  340.         DEBUG_PRINT("Selected Cue: ");
  341.         DEBUG_PRINTLN(selectedCue + 1);
  342.  
  343.         DEBUG_PRINT("Running Cue: ");
  344.         DEBUG_PRINTLN(runningCue + 1);
  345.     }
  346. }
  347.  
  348. void called_actions() {
  349.  
  350.     /* record cue called */
  351.     if (bRecordCue) {
  352.         DEBUG_PRINTLN("Recording cue..");
  353.  
  354.         uint16_t address = EEPROM_ADDRESS + (selectedCue * NUM_FADERS) ;
  355.         for (int i = 0; i < NUM_FADERS; ++i) {
  356.             // write to running memory
  357.             lightingData[selectedCue][i] = values[i];
  358.             // write data to EEPROM
  359.             write_to_eeprom(values[i], address);
  360.             address++;
  361.         }
  362.         bCueChanged = true;
  363.     }
  364.  
  365.     /* reset EEPROM */
  366.     if (bClearEEPROM) {
  367.         DEBUG_PRINTLN("Calling EEPROM clear..");
  368.         clear_eeprom();
  369.         // give visual feedback
  370.         init_sequence(UI_BLINK);
  371.     }
  372.  
  373.     /* clear running data called */
  374.     if (bClearRunningData) {
  375.         DEBUG_PRINTLN("Clearing running data..");
  376.         memset(lightingData, 0, sizeof(lightingData));  // clear running data
  377.     }
  378.  
  379.     /* clear indicators called */
  380.     if (bClearIndicators) {
  381.         // clear indicators from previous mode
  382.         for (int i = 0; i < NUM_CUES; ++i) {
  383.             cueLeds[i].setMode(0);                      // all cue LEDs off
  384.         }
  385.     }
  386.     if (bClearTimer) {
  387.         // clear indicators from previous mode
  388.         memset(timerLeds, 0, sizeof(timerLeds));        // all timer LEDS off
  389.     }
  390.     /* UI management */
  391.     if (bCueChanged) {
  392.         // calculate values for indicator LEDs
  393.         if (state == MODE_2) {
  394.             bool bEmpty = true;
  395.             for (int i = 0; i < NUM_FADERS; ++i) {
  396.                 if (lightingData[selectedCue][i] != 0) bEmpty = false;
  397.             }
  398.             DEBUG_PRINT("Mode 2: Cue ");
  399.             DEBUG_PRINT(selectedCue);
  400.             if (bEmpty) DEBUG_PRINT(" Empty");
  401.             led_from_selected_cue(selectedCue, bEmpty);
  402.         } else if (state == MODE_3) {
  403.             if (bStandby) {
  404.                 led_running(runningCue);
  405.             } else {
  406.                 led_up_to_cue(selectedCue);
  407.                 if (bFading) led_running(selectedCue);
  408.             }
  409.         }
  410.     }
  411. }
  412.  
  413. void refresh_outputs() {
  414.     /* Refresh LED outputs */
  415.     write_to_leds();                        // write outputs
  416.     write_to_indicators();                  // write to indicator LEDs
  417. }
Add Comment
Please, Sign In to add comment