регулировка амплитуды

gena321
Offline
Зарегистрирован: 19.01.2019
Здравствуйте
В этой программе регулировка амплитуды происходит с помощью  кнопок на PB6 и PB9  медленно , когда нажать добавительно кнопку на PB12  после 2 сек  регулировка происходит быстро.  Как сделать чтобы скорость менялась когда кнопка 6 или 9 нажата более 2 сек ?
 
#define SAMPLES 100
#define DEBOUNCE_DELAY 10
#define DEBOUNCE_IDLE 0
#define DEBOUNCE_ACTIVE 1

#include <libmaple/dma.h>

struct debounce_info
{
  int start, state;
  int16 shift;
} left, right, up, down;

dma_tube_config dma_cfg, dma_cfg2;

int flag1 = 0;
int flag2 = 0;
int out2 = PA8;
int val1[SAMPLES];
int val2[SAMPLES];
int ampp = PB9;
int ampn = PB6;
int16 shift = 0;
int amp = 35;
int cnt = 0;
int time_track = 0;
float stp = 6.2831 / SAMPLES;
int ret = 17;

timer_dev *dev2 = PIN_MAP[out2].timer_device;
uint8 cc_channel2 = PIN_MAP[out2].timer_channel;
void fun2()
{
  flag2++;
}

void timer_conf()
{

  timer_dma_set_base_addr(dev2, TIMER_DMA_BASE_CCR1);
  timer_dma_set_burst_len(dev2, 1);
  timer_dma_enable_req(dev2, cc_channel2);
  timer_set_reload(dev2, 102);
  timer_set_prescaler(dev2, 0);
}

void dma_conf()
{

  dma_init(DMA1);

  // ampl
  /* T1C1 DMA C2 */
  dma_cfg.tube_dst = &(dev2->regs.gen->DMAR);
  dma_cfg.tube_dst_size = DMA_SIZE_32BITS;
  dma_cfg.tube_src = val2;
  dma_cfg.tube_src_size = DMA_SIZE_32BITS;

  dma_cfg.tube_nr_xfers = SAMPLES;
  dma_cfg.tube_flags = DMA_CFG_SRC_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;

  dma_cfg.tube_req_src = DMA_REQ_SRC_TIM1_CH1;
  dma_cfg.target_data = 0;

  ret = dma_tube_cfg(DMA1, DMA_CH2, &dma_cfg);
}

void dma_start()
{
  dma_attach_interrupt(DMA1, DMA_CH2, fun2);
  dma_enable(DMA1, DMA_CH2);
  timer_resume(dev2);
}

void init_wave()
{
  int i;
  for (i = 0; i < SAMPLES; i++)
  {
    val1[i] = 50 + amp * sin(stp * i);
    val2[i] = val1[i];
  }
}

void get_wave(int16 shift)
{
  /////////////////////////////////////
  int i;
  // int i < 50;

  for (i = 0; i < SAMPLES; i++)

    ///////////////////////////////////////////
  {
    val2[i] = 50 + amp * sin( stp * i + shift * 0.1 * 6.2831 / 360);
  }
}

int LED1 = PB15;
int button = PB12;
//int button = PB12;
int LED2 = PC13;

boolean LED1State = false;
boolean LED2State = false;

long buttonTimer = 0;
//long longPressTime = 250;
long longPressTime = 1000;

boolean buttonActive = false;
boolean longPressActive = false;

void setup() {
  int i;
  pinMode(out2, PWM);
  pinMode(ampp, INPUT_PULLDOWN);
  pinMode(ampn, INPUT_PULLDOWN);

  memset(&up, 0, sizeof(debounce_info));
  memset(&down, 0, sizeof(debounce_info));

  up.shift = 1;
  down.shift = -1;

  Serial.begin(9600);

  timer_conf();
  dma_conf();
  dma_start();

  init_wave();

  get_wave(shift);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  //pinMode(button, INPUT);
  pinMode(button, INPUT_PULLDOWN);
}

void loop() {
  int val;
  //////////////////////////
  if (digitalRead(button) == HIGH)
    // if (val == HIGH)
    //////////////////////////
  {
    if (buttonActive == false)
    {
      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
    {
      longPressActive = true;
      LED1State = 1;
      digitalWrite(LED1, LED1State);
    }
  }
  else
  {
    LED1State = 0;
    digitalWrite(LED1, LED1State);
    if (buttonActive == true)
    {
      if (longPressActive == true)
      {
        longPressActive = false;
      }
      else
        buttonActive = false;
    }
  }

  if (LED1State == 1)

  {
    delay(1);
    Serial.println(1);

  }
  else
  {
    delay(1000);
    Serial.println(1000);

  }
  process_key_amp(digitalRead(ampp), &up);
  process_key_amp(digitalRead(ampn), &down);
}

void process_key_amp(int val, struct debounce_info *k)
{
  switch (k->state)
  {
    case DEBOUNCE_IDLE:
      if (val == HIGH)
      {
        k->state = DEBOUNCE_ACTIVE;
        k->start = millis();
      }
      break;

    case DEBOUNCE_ACTIVE:
      if (val == HIGH)
      {
        if (millis() - k->start > DEBOUNCE_DELAY)
        {
          amp += (int)k->shift;
          get_wave(shift);
        }
      }
      else
        k->state = DEBOUNCE_IDLE;

      break;
  }
  Serial.println(amp);
}

 

 

gena321
Offline
Зарегистрирован: 19.01.2019
 //////////////////////////

  if (digitalRead(button) == HIGH)

    // if (val == HIGH)

    //////////////////////////

 

nik182
Offline
Зарегистрирован: 04.05.2015

О! Blue pill! Das ist fantastish! : delay(1000);