Version:0.9 StartHTML:0000000105 EndHTML:0000312329 StartFragment:0000001499 EndFragment:0000312313 mikroIDE
#include "internal_adc.h"
//#include "uart.h"


//misc
unsigned int pwm_period1;
unsigned int pwm_period2;

//main lfo wavetables
const unsigned char table_sine[256] = {128,131,134,137,140,143,146,149,152,156,159,162,165,168,171,174,176,179,182,185,188,191,193,196,199,201,204,206,209,211,213,216,218,220,222,224,226,228,230,232,234,236,237,239,240,242,243,245,246,247,248,249,250,251,252,252,253,254,254,255,255,255,255,255,255,255,255,255,255,255,254,254,253,252,252,251,250,249,248,247,246,245,243,242,240,239,237,236,234,232,230,228,226,224,222,220,218,216,213,211,209,206,204,201,199,196,193,191,188,185,182,179,176,174,171,168,165,162,159,156,152,149,146,143,140,137,134,131,128,124,121,118,115,112,109,106,103,99,96,93,90,87,84,81,79,76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,19,18,16,15,13,12,10,9,8,7,6,5,4,3,3,2,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,2,3,3,4,5,6,7,8,9,10,12,13,15,16,18,19,21,23,25,27,29,31,33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,79,81,84,87,90,93,96,99,103,106,109,112,115,118,121,124};
const unsigned char table_triangle[256] = {128,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,215,217,219,221,223,225,227,229,231,233,235,237,239,241,243,245,247,249,251,253,255,253,251,249,247,245,243,241,239,237,235,233,231,229,227,225,223,221,219,217,215,213,211,209,207,205,203,201,199,197,195,193,191,189,187,185,183,181,179,177,175,173,171,169,167,165,163,161,159,157,155,153,151,149,147,145,143,141,139,137,135,133,131,129,128,126,124,122,120,118,116,114,112,110,108,106,104,102,100,98,96,94,92,90,88,86,84,82,80,78,76,74,72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126};
const unsigned char table_square[256] = {254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}; 
const unsigned char table_ramp_up[256] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,255};
const unsigned char table_ramp_down[256] = {255,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
const unsigned char table_bumpy_pinch[256] = {142,154,161,168,176,181,186,191,195,199,202,205,208,211,213,216,218,220,222,224,226,228,229,231,232,234,235,236,237,238,239,240,241,242,243,244,245,245,246,247,248,248,249,249,250,250,251,251,251,252,252,252,253,253,253,254,254,254,254,254,254,254,254,255,255,255,254,254,254,254,254,254,254,254,253,253,253,253,253,252,252,252,251,251,251,250,250,249,249,248,248,247,247,246,246,245,244,244,243,242,242,241,240,239,239,238,237,236,235,234,233,232,232,231,229,228,227,226,225,224,223,221,220,219,218,216,215,214,212,211,209,208,207,205,203,202,200,198,197,195,193,191,190,188,186,184,182,180,178,176,174,172,169,167,164,162,160,157,155,153,150,147,144,141,138,136,133,130,126,123,120,117,113,110,106,102,98,94,90,86,81,77,72,67,62,56,50,44,37,30,22,12,16,26,33,40,45,50,54,59,62,65,68,71,74,76,78,81,83,85,87,88,90,92,93,95,96,97,98,100,101,102,103,104,105,106,107,107,108,109,110,110,111,112,112,113,114,114,115,115,116,116,117,117,118,118,119,119,120,120,121,122,122,124,125,128};


//other
//const unsigned char weird1[256] = {127,127,127,127,127,127,127,127,127,128,128,129,129,130,131,131,132,133,134,135,136,137,139,140,142,144,145,147,148,150,151,153,154,155,156,156,157,158,158,158,158,158,159,159,159,159,159,159,159,159,159,159,160,160,160,160,160,159,158,157,156,154,153,151,149,147,146,143,140,138,134,130,116,112,108,106,104,102,100,99,99,98,97,97,96,96,95,95,94,94,94,94,94,94,94,94,95,95,96,97,98,100,101,102,104,105,107,109,110,112,114,116,120,122,126,129,143,151,157,163,169,172,179,195,227,234,234,205,145,136,114,108,106,103,101,100,99,98,97,96,95,94,94,94,93,93,93,93,93,93,93,93,93,93,93,94,94,95,96,96,98,99,100,100,101,102,103,104,106,107,109,110,112,114,116,117,119,120,122,124,128,130,133,136,138,139,141,142,144,145,146,147,149,150,152,153,153,154,155,155,155,156,156,156,156,156,156,156,156,156,156,156,156,156,155,154,154,152,152,151,150,149,147,147,145,145,144,143,142,141,140,139,138,137,136,135,134,134,134,133,132,132,131,131,130,130,130,129,129,129,128,128,128,127,127,127};
//const unsigned char table_weird2[256] = {127,123,115,109,104,98,89,83,81,78,76,74,72,71,71,70,69,69,68,68,67,67,66,66,67,67,68,68,69,70,71,73,76,79,85,121,134,147,153,158,165,174,181,189,210,219,232,237,241,247,251,253,210,194,183,146,130,113,92,81,76,70,68,63,59,57,52,50,48,45,44,42,41,40,39,38,36,36,35,35,35,35,35,35,35,36,36,36,38,39,40,40,41,42,43,45,46,48,49,51,52,54,58,60,64,69,72,76,82,86,89,91,96,101,106,109,111,116,121,125,127,130,134,135,137,139,142,144,145,147,148,150,152,153,154,155,156,156,157,159,159,160,160,161,161,162,162,162,162,163,163,163,163,163,163,163,163,163,162,162,161,161,158,157,156,154,152,151,148,145,143,139,136,134,132,129,126,125,123,121,119,118,117,117,117,117,117,116,116,116,116,116,116,116,116,116,116,116,116,116,117,117,118,118,119,119,119,120,120,120,122,122,123,123,124,125,125,126,126,127,128,129,129,131,131,132,132,133,133,133,134,134,134,134,135,135,135,135,135,135,135,135,135,135,135,135,135,135,135,135,135,135,135,134,134,133};
//const unsigned char table_left_skew[256] = {147,163,180,193,203,214,223,229,233,236,237,238,237,236,235,232,229,226,223,219,215,211,207,202,198,194,190,186,182,178,174,170,166,162,158,154,151,147,143,140,137,133,130,127,124,121,118,115,112,109,106,104,101,98,96,93,91,89,86,84,82,80,77,75,73,71,70,68,66,64,62,60,59,57,56,54,53,51,50,48,47,45,44,43,42,40,39,38,37,36,35,34,33,32,31,30,29,29,28,27,26,26,25,24,24,23,23,22,22,21,21,20,20,19,19,19,18,18,18,18,17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,18,18,18,18,19,19,19,20,20,20,21,21,22,22,22,23,23,24,24,25,25,26,27,27,28,28,29,30,30,31,32,32,33,34,34,35,36,37,38,38,39,40,41,42,43,44,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,64,65,66,67,68,69,70,72,73,74,75,77,78,79,80,81,83,84,85,87,88,89,91,92,93,95,96,97,99,100,102,103,105,106,107,109,110,112,113,115,116,118,119,121,122,124,125,127,128};
//const unsigned char table_right_skew[256] = {128,127,125,124,122,121,119,118,116,115,113,112,110,109,107,106,105,103,102,100,99,97,96,95,93,92,91,89,88,87,85,84,83,81,80,79,78,77,75,74,73,72,70,69,68,67,66,65,64,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,44,43,42,41,40,39,38,38,37,36,35,34,34,33,32,32,31,30,30,29,28,28,27,27,26,25,25,24,24,23,23,22,22,22,21,21,20,20,20,19,19,19,18,18,18,18,17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,18,18,18,18,19,19,19,20,20,21,21,22,22,23,23,24,24,25,26,26,27,28,29,29,30,31,32,33,34,35,36,37,38,39,40,42,43,44,45,47,48,50,51,53,54,56,57,59,60,62,64,66,68,70,71,73,75,77,80,82,84,86,89,91,93,96,98,101,104,106,109,112,115,118,121,124,127,130,133,137,140,143,147,151,154,158,162,166,170,174,178,182,186,190,194,198,202,207,211,215,219,223,226,229,232,235,236,237,238,237,236,233,229,223,214,203,193,180,163,147};

//pinch wavetables
const unsigned char table_bubble[256] = {234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,247,247,247,248,248,249,249,249,250,250,250,251,251,251,252,252,252,253,253,253,253,253,254,254,254,254,254,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,254,254,254,254,254,253,253,253,253,253,252,252,252,252,251,251,251,250,250,250,249,249,248,248,248,247,247,246,246,245,245,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,233,232,231,231,230,229,228,226,225,224,223,222,221,219,218,217,216,214,213,211,210,208,206,205,203,201,199,197,195,193,191,189,186,184,181,179,177,174,171,168,165,162,159,155,151,147,143,139,135,130,125,120,115,109,103,97,90,82,75,66,57,48,36,25,13,14,26,38,50,60,69,78,86,94,101,107,114,119,125,130,135,139,143,147,151,155,159,162,165,168,171,174,177,180,182,184,187,189,192,194,196,198,200,202,203,205,207,209,210,212,213,215,216,217,219,220,221,222,223,225,226,227,228,229,230,231,232,233,234};
const unsigned char table_bubble_inverted[256] = {24,25,26,27,28,29,30,31,32,33,34,35,37,38,39,41,42,44,45,47,48,50,51,53,55,57,59,61,63,65,67,70,72,75,77,79,82,85,88,92,95,98,102,106,110,114,118,122,127,133,138,144,150,157,163,171,179,188,198,208,220,232,244,255,255,245,233,222,211,201,192,183,175,168,160,154,148,142,137,132,127,122,117,114,110,106,102,98,95,92,89,86,83,80,78,75,73,70,68,66,63,61,59,57,56,54,52,50,49,47,46,44,43,41,40,38,37,36,35,33,32,31,30,29,28,27,26,25,24,23,22,22,21,20,19,18,18,17,16,16,15,15,14,13,13,12,12,11,11,10,10,9,9,9,8,8,7,7,7,6,6,6,5,5,5,5,4,4,4,4,4,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,8,8,9,9,9,10,10,11,11,12,12,13,13,14,15,15,16,16,17,18,18,19,20,21,22,22,23};

//custom wavetable variables
unsigned char table_custom[3072];
unsigned char table_custom_ready=0;
unsigned int table_custom_count;
unsigned int table_custom_max;
unsigned int table_custom_max_fixed;


unsigned long table_custom_point_diff;
unsigned long table_custom_point_incr;
bit table_custom_plus;

//tap tempo
unsigned int tt_state=0;
unsigned long tt_count=0;
bit tt_reset;


//lfo
unsigned long lfo_accumulator;
unsigned long lfo_accumulator_led;
unsigned long lfo_accumulator_blend;
unsigned char lfo_table_count;
unsigned int lfo_table_count_mod;
unsigned long lfo_table_count_long;
bit lfo_table_count_toggle;

//controls
unsigned char adc_turn;

unsigned long control_speed;

unsigned long control_depth;

unsigned long control_pinch=0;
bit control_pinch_sens;
unsigned long pinch_result;

unsigned long control_random;
signed long control_random_compare;
char control_random_compare_state;
bit control_random_sens;
bit control_random_canrefresh;



unsigned int random_count;
unsigned int random_count_mode=256;
unsigned int random_rand_shift=128;
unsigned long random_accumulator=65280;
unsigned long random_accumulator_temp=65280;
unsigned long random_accumulator_blend=127;
unsigned long random_accumulator_step=16;
bit random_accumulator_sens;


unsigned long control_shape;

unsigned char control_shape_start;
unsigned long control_shape_sample;
unsigned char control_shape_custom=0;

unsigned char control_shape_mode;
unsigned char control_shape_mode_compare;




unsigned char temp_count;

sbit PIN_soft_tap at RB11_bit;

sbit PIN_soft_bypass at RB4_bit;
sbit PIN_led_bypass at RB6_bit;
unsigned char led_bypass_state;
unsigned char led_bypass_state_count=0;
sbit PIN_relay_1 at RB10_bit;
sbit PIN_relay_2 at RB13_bit;
bit bypass_relay_state;
unsigned int bypass_relay_state_count=0;
unsigned char bypass_relay_state_reset;

unsigned int soft_tap_count=0;



//###############################################################################
//###############################################################################
//###############################################################################


void slow_timer() iv IVT_TIMER_1 ilevel 7 ics ICS_SOFT {
  T1IF_bit         = 0;
  DisableInterrupts();
  
  if (adc_turn == 0) {
     //###############################################################################
     InitADC(0);                                                     //speed adc
     //###############################################################################
  } else if (adc_turn == 1) {


    control_speed = GetADC(void);
    //control_speed = 1024 - control_speed;               //reverse
  
  
    //lin->log hack
    control_speed = control_speed * 100;
    control_speed = control_speed * (control_speed / 100);
    control_speed = control_speed / 102400;

    control_speed = control_speed << 6;
    if (control_speed < 128) {control_speed = 128;}



    if (tt_State != 2) {
       PR4 = control_speed;
    }



  } else if (adc_turn == 2) {
    //###############################################################################
    InitADC(4);                                                     //pinch adc
    //###############################################################################
  } else if (adc_turn == 3) {
  

    if (led_bypass_state == 2) {
       control_random_canrefresh = 0;
       control_random_compare_state = 1;
       

    control_pinch = GetADC(void);

    //control bracketing
    control_pinch = control_pinch * 100;
    control_pinch = control_pinch / 128;
    control_pinch = control_pinch + 100;

    if (control_pinch < 512) {
     control_pinch = 512 - control_pinch;
     control_pinch_sens = 0;
     } else {
     control_pinch = control_pinch - 512;
     control_pinch_sens = 1;
     }

     
     
     } else {
     
       if (control_random_canrefresh == 1) {
       
          control_random = GetADC(void);
          if (control_random < 512) {
             control_random = 512 - control_random;
             control_random_sens = 0;
          } else {
             control_random = control_random - 512;
             control_random_sens = 1;
          }
          
       } else {
       
          if (control_random_compare_state == 1) {
            control_random_compare = GetADC(void);        //only once
            control_random_compare_state = 0;
          }
          
            if (GetADC(void) >= (control_random_compare + 12) || GetADC(void) < (control_random_compare - 12)) {
               control_random_canrefresh = 1;
            }

       }
      
      
      
       
     }




  } else if (adc_turn == 4) {
    //###############################################################################
    InitADC(1);                                                    //depth adc
    //###############################################################################
  } else if (adc_turn == 5) {



     control_shape = GetADC(void);
     
     control_shape_sample = control_shape;
     control_shape_sample = control_shape_sample >> 2;


     
     if (control_shape <= 11) {
        control_shape_mode = 1;
        control_shape = 0;
        
     } else if (control_shape > 11 && control_shape <= 211) {
        control_shape_mode = 1;
        control_shape = control_shape - 12;
     } else if (control_shape > 211 && control_shape <= 411) {
        control_shape_mode = 2;
        control_shape = control_shape - 212;
     } else if (control_shape > 411 && control_shape <= 611) {
        control_shape_mode = 3;
        control_shape = control_shape - 412;
     } else if (control_shape > 611 && control_shape <= 811) {
        control_shape_mode = 4;
        control_shape = control_shape - 612;
     } else if (control_shape > 811 && control_shape <= 1011) {
        control_shape_mode = 5;
        control_shape = control_shape - 812;
        
     } else if (control_shape > 1011) {
        control_shape_mode = 5;
        control_shape = 199;
     }
     

     
     
     
  } else if (adc_turn == 6) {
    //###############################################################################
    InitADC(9);                                                    //shape adc
    //###############################################################################
  } else if (adc_turn == 7) {
  

    control_depth = GetADC(void);

  

  }
  
  
  


  //adc turn counter
  if (adc_turn == 10) {
     adc_turn = 0;
  } else {
    adc_turn++;
  }








  //bypass switch state change
  if (bypass_relay_state_count > 10 && bypass_relay_state_count < 1000) {
     if (PIN_soft_bypass == 1) {
        bypass_relay_state_reset = 1;
     }
  } else if (bypass_relay_state_count >= 1000) {
        led_bypass_state = 2;
  }

  //counter & led refresh
  if (PIN_soft_bypass == 0) {
    bypass_relay_state_count++;
  } else {
    bypass_relay_state_count = 0;
        if (bypass_relay_state == 0) {
           led_bypass_state = 0;
        } else {
          led_bypass_state = 1;
        }
  }

     //actual relay toggle
     if (bypass_relay_state_reset == 1) {
        if (bypass_relay_state == 0) {
           led_bypass_state = 1;
           PIN_relay_1 = 1;
           PIN_relay_2 = 0;
           bypass_relay_state = 1;
        } else {
          led_bypass_state = 0;
          PIN_relay_1 = 0;
          PIN_relay_2 = 1;
          bypass_relay_state = 0;
        }
        delay_ms(10);
        PIN_relay_1 = 0;
        PIN_relay_2 = 0;
        bypass_relay_state_reset = 0;
     }
     
     
     

  if (bypass_relay_state_count >= 1000) {               //temp tt reset
        tt_State = 0;
        tt_count = 0;
        tt_reset = 0;
  }

  
     

//tap tempo
  if (soft_tap_count > 20 && soft_tap_count <= 500) {
     if (PIN_soft_tap == 1 && tt_reset == 1) {               //if tap
        if (tt_State == 0) {                //1st tap
           tt_State = 1;
           tt_count = 0;
        } else if (tt_State == 1) {         //2nd tap
           tt_State = 2;                    //lock lfo speed
           if (tt_count == 0) {tt_count = 1;}
           if (tt_count > 4000) {tt_count = 4000;}
           tt_count = tt_count * 98;
           tt_count = tt_count / 6;
           if (tt_count < 128) {tt_count = 128;}
           if (tt_count > 65535) {tt_count = 65535;}
           PR4 = tt_count;
        }
        tt_reset = 0;
     }
  }

  if (tt_State == 1) {                     //start counting
     if (soft_tap_count <= 4000) {
     tt_count++;
     }
  }



  
  
  
  //tap switch state change
  if (soft_tap_count == 1000) {
        led_bypass_state = 3;
        control_shape_custom = 1;
        table_custom_ready = 0;
        table_custom_count = 0;
        
        tt_State = 0;
        tt_count = 0;
        tt_reset = 0;
  } else if (soft_tap_count > 1000) {
        led_bypass_state = 3;
        control_shape_custom = 1;
        //table_custom_ready = 0;
  }

  

  //soft tap count
  if (PIN_soft_tap == 0) {
    soft_tap_count++;
  } else {
    soft_tap_count = 0;
    tt_reset = 1;
  }

if (table_custom_ready == 2) {
  if (control_shape_mode != control_shape_mode_compare) {
     if (PIN_soft_tap == 1) {
        control_shape_custom = 0;
     }
  }
}
  
  
  
  
  
  

  //led state management
  if (led_bypass_state == 0) {
     PIN_led_bypass = 0;
  } else if (led_bypass_state == 1) {
     PIN_led_bypass = 1;
  } else if (led_bypass_state == 2) {
     if (led_bypass_state_count > 50) {
        led_bypass_state_count = 0;
        PIN_led_bypass = ~PIN_led_bypass;
     } else {
        led_bypass_state_count++;
     }
  } else if (led_bypass_state == 3) {
     if (led_bypass_state_count > 150) {
        led_bypass_state_count = 0;
        PIN_led_bypass = ~PIN_led_bypass;
     } else {
        led_bypass_state_count++;
     }
  }


  
  EnableInterrupts();
}


//###############################################################################
//###############################################################################
//###############################################################################


void lfo_timer() iv IVT_TIMER_4 ilevel 7 ics ICS_SOFT {
  T4IF_bit         = 0;




   if (control_shape_mode == 1) {
      lfo_accumulator = table_sine[lfo_table_count] * (199 - control_shape);
      lfo_accumulator_blend = table_triangle[lfo_table_count] * control_shape;
   } else if (control_shape_mode == 2) {
      lfo_accumulator = table_triangle[lfo_table_count] * (199 - control_shape);
      lfo_accumulator_blend = table_square[lfo_table_count] * control_shape;
   } else if (control_shape_mode == 3) {
      lfo_accumulator = table_square[lfo_table_count] * (199 - control_shape);
      lfo_accumulator_blend = table_ramp_up[lfo_table_count] * control_shape;
   } else if (control_shape_mode == 4) {
      lfo_accumulator = table_ramp_up[lfo_table_count] * (199 - control_shape);
      lfo_accumulator_blend = table_ramp_down[lfo_table_count] * control_shape;
   } else if (control_shape_mode == 5) {
      lfo_accumulator = table_ramp_down[lfo_table_count] * (199 - control_shape);
      lfo_accumulator_blend = table_bumpy_pinch[lfo_table_count] * control_shape;
   }
   lfo_accumulator = lfo_accumulator / 199;
   lfo_accumulator_blend = lfo_accumulator_blend / 199;
   lfo_accumulator = lfo_accumulator + lfo_accumulator_blend;
   
   

      //random granularity
      if (control_random_sens == 0) {
         random_count_mode = 256;        //256
         random_rand_shift = 128;        //128
      } else {
        random_count_mode = 32;        //32
        random_rand_shift = 32;        //16
      }



    if (random_count < random_count_mode) {
       random_count++;
    } else {

       random_accumulator_step = rand();
       random_accumulator_step = random_accumulator_step / random_rand_shift;
       random_count = 0;
       if (random_accumulator_temp <= 127) {
          random_accumulator_sens = 0;
       } else {
          random_accumulator_sens = 1;
       }
    }



    if (random_accumulator_sens == 1) {
          random_accumulator = random_accumulator - random_accumulator_step;
          if (random_accumulator > 4000000000) {                                //crazy wrap-around 32bit variable fix
             random_accumulator = 0;
             random_count = random_count_mode;                                  //force reset if it shits the bed
          }
    } else {
          random_accumulator = random_accumulator + random_accumulator_step;
    }
   random_accumulator_temp = random_accumulator >> 9;          //9
   


    //custom wave injection
    if (control_shape_custom == 1) {
          
       if (table_custom_ready == 0) {                           //record
          lfo_accumulator = control_shape_sample;
          table_custom[table_custom_count] = control_shape_sample;
          table_custom_count++;

          if (table_custom_count > 3072) {
               table_custom_ready = 1;
               //table_custom_max = table_custom_count - 10;
               table_custom_max = 3062;
               table_custom_count = 0;
          }
          if (PIN_soft_tap == 1) {
               table_custom_ready = 1;
               table_custom_max = table_custom_count - 10;
               if (table_custom_max > 3062) {table_custom_max == 3062;}
               table_custom_count = 0;
          }


       }





       if (table_custom_ready == 1) {                    //set
       

          //table_custom_max = table_custom_count - 10;
          //table_custom_count = 0;
          table_custom_ready = 2;
          //control_shape_mode_compare = control_shape_mode;
          


          //interpolate loop points
          DisableInterrupts();

          table_custom_max_fixed = table_custom_max - 16;
          
          if (table_custom[table_custom_max_fixed] > table_custom[0]) {
             table_custom_point_diff = table_custom[table_custom_max_fixed] - table_custom[0];
             table_custom_plus = 0;
          } else {
             table_custom_point_diff = table_custom[0] - table_custom[table_custom_max_fixed];
             table_custom_plus = 1;
          }
          


          if (table_custom_point_diff == 0) {table_custom_point_diff = 1;}
          table_custom_point_diff = table_custom_point_diff / 16;
          table_custom_point_incr = table_custom_point_diff;
          
          
          table_custom_count = table_custom_max - 16;
          
          
          do {
             if (table_custom_plus == 1) {
                table_custom[table_custom_count] = table_custom[table_custom_max_fixed] + table_custom_point_diff;
             } else {
                table_custom[table_custom_count] = table_custom[table_custom_max_fixed] - table_custom_point_diff;
             }
             table_custom_count++;
             table_custom_point_diff = table_custom_point_diff + table_custom_point_incr;
             
             } while (table_custom_count < table_custom_max);
          
          //PIN_led_bypass = 1;
          
           table_custom_count = 0;
           table_custom_max = table_custom_max - 4;
          
           EnableInterrupts();

          
       }





       
       if (table_custom_ready == 2) {                    //loop
       
          if (PIN_soft_tap == 1) {
            if (soft_tap_count > 0) {                   //do once
               control_shape_mode_compare = control_shape_mode;
            }
          }
       
          if (table_custom_count > table_custom_max) {
             table_custom_count = 0;
          }  else {
             table_custom_count++;
          }
          lfo_accumulator = table_custom[table_custom_count];
       }

    }
    
    
    
    
   //blend clean wave with random wave
   random_accumulator_blend = random_accumulator_temp * control_random;
   random_accumulator_blend = random_accumulator_blend / 512;
   lfo_accumulator = lfo_accumulator * (512 - control_random);
   lfo_accumulator = lfo_accumulator / 512;
   lfo_accumulator = lfo_accumulator + random_accumulator_blend;
   

   



   //pinch shaping
   if (control_pinch_sens == 0) {
      pinch_result = table_bubble[lfo_table_count] * control_pinch;
   } else {
      pinch_result = table_bubble_inverted[lfo_table_count] * control_pinch;
   }
   pinch_result = pinch_result / 512;
   lfo_accumulator = lfo_accumulator * (512 - control_pinch);
   lfo_accumulator = lfo_accumulator / 512;
   lfo_accumulator = lfo_accumulator + pinch_result;





    
    
    
    


   //256->320 scaling
   lfo_accumulator = lfo_accumulator * 100;
   lfo_accumulator = lfo_accumulator / 80;

   //for led modulation
   lfo_accumulator_led = lfo_accumulator;

   //depth
   lfo_accumulator = lfo_accumulator * control_depth;
   lfo_accumulator = lfo_accumulator / 1024;



   
   
   //PWM output
   PWM_Set_Duty(lfo_accumulator, 1);   //0-320
   PWM_Set_Duty(lfo_accumulator_led, 2);   //0-320
   
   

   lfo_table_count++;



}






//###############################################################################
//###############################################################################
//###############################################################################




void main() {

//slow timer
  T1CON         = 0x8000;
  T1IE_bit         = 1;
  T1IF_bit         = 0;
  T1IP0_bit         = 1;
  T1IP1_bit         = 1;
  T1IP2_bit         = 1;
  PR1                 = 32000;        //1ms
  TMR1                 = 0;

  
//lfo timer

  T4CON         = 0x8030;
  T4IE_bit         = 1;
  T4IF_bit         = 0;
  T4IP0_bit         = 1;
  T4IP1_bit         = 1;
  T4IP2_bit         = 1;
  PR4                 = 128;
  TMR4                 = 0;

  //UART2_Init(76800);

 
  
AD1CHS = 0b1111110111101100;                          // configure AN pins as digital

//pin setup


//TRISB4_bit = 1;

TRISB6_bit = 0;     //bypass led

TRISB13_bit = 0;     //bypass relay 1
TRISB10_bit = 0;     //bypass relay 2


RPB3R = 0b0101;     //OC1 = B3
TRISB3_bit = 0;

RPB5R = 0b0101;     //OC2 = B5
TRISB5_bit = 0;

//RPB14R = 0b0010;     //U2TX = B14
//TRISB14_bit = 0;

control_random_canrefresh = 1;

bypass_relay_state = 1;
led_bypass_state = 1;
PIN_relay_1 = 1;
PIN_relay_2 = 0;
delay_ms(10);
PIN_relay_1 = 0;
PIN_relay_2 = 0;




//init PWM
pwm_period1 = PWM_Init(100000, 1, 1, 2);
pwm_period2 = PWM_Init(100000, 2, 1, 3);
PWM_Start(1);
PWM_Start(2);

EnableInterrupts();


PIN_led_bypass = 0;              //temp


table_custom_count = 0;
do {
   table_custom[table_custom_count] = 0;
   table_custom_count++;
} while (table_custom_count <= 3072);




/*
while (1) {
          LED = ~LED;
          delay_ms(500);
          }
*/
          
          
}