|

楼主 |
发表于 2023-1-7 11:38:30
|
显示全部楼层
#include <Arduino.h>
#include "TouchKey.h"
#define MAXrepeat 99
#define ledInterval 4
#define blinkTime 8000
#define disabledTime 10000
#define shuttingTime 10000
uint8_t prevSensorValue = 0;
enum actChannel
{
ch0 = 0,
ch1 = 1,
none = 2
};
enum actChannel actCh = 2;
unsigned long actChStartTime = 0;
const uint8_t LED7Pin[] = {37, 14, 35, 33, 11, 10, 32}; // A B C D E F G
const uint8_t numCode[] = {
0B00111111, // 0
0B00000110, // 1
0B01011011, // 2
0B01001111, // 3
0B01100110, // 4
0B01101101, // 5
0B01111101, // 6
0B00000111, // 7
0B01111111, // 8
0B01101111, // 9
0B01000000, // - 10
0B00000000, // NULL 11
};
enum states
{
DISABLED = 0,
WORKING = 1,
SHUTTING = 2
};
struct channels
{
enum states state;
unsigned long startTime;
uint8_t times;
uint8_t ctlPin;
} channel[2] = {{DISABLED, 0, 0, 12},
{DISABLED, 0, 0, 13}};
struct led
{
uint8_t ledPin;
uint8_t digit;
} led[4] = {{34, 11},
{30, 11},
{31, 11},
{15, 11}};
void ledUpdate()
{
if (channel[0].state == DISABLED)
{
led[0].digit = 11;
led[1].digit = 11;
}
else
{
led[0].digit = channel[0].times / 10;
if (led[0].digit == 0)
led[0].digit = 11;
led[1].digit = channel[0].times % 10;
}
if (channel[1].state == DISABLED)
{
led[2].digit = 11;
led[3].digit = 11;
}
else
{
led[2].digit = channel[1].times / 10;
if (led[2].digit == 0)
led[2].digit = 11;
led[3].digit = channel[1].times % 10;
}
}
void timesM()
{
if (actCh == none)
{
actCh = ch0;
channel[actCh].state = WORKING;
channel[actCh].times = 10;
}
else
{
channel[actCh].times--;
if (channel[actCh].times == 0)
{
channel[actCh].state = SHUTTING;
digitalWrite(channel[actCh].ctlPin, LOW);
}
}
channel[actCh].startTime = millis();
actChStartTime = millis();
ledUpdate();
}
void timesP()
{
if (actCh == none)
{
actCh = ch1;
channel[actCh].state = WORKING;
channel[actCh].times = 10;
}
else
{
channel[actCh].times++;
if (channel[actCh].times > MAXrepeat)
channel[actCh].times = MAXrepeat;
}
channel[actCh].startTime = millis();
actChStartTime = millis();
ledUpdate();
}
struct OneButton_s
{
bool val;
unsigned long startTime;
bool keyDown;
} key[] = {{false, 0, false}, {false, 0, false}};
void key_tick()
{
for (int i = 0; i < 2; i++)
{
unsigned long now;
now = millis();
if (key[i].keyDown && (now - key[i].startTime) > 400)
{
if (key[i].val == true)
{
key[i].startTime = now;
if (i == 0)
timesM();
else
timesP();
}
else
key[i].keyDown = false;
}
else if (!key[i].keyDown && (now - key[i].startTime) > 400)
{
if (key[i].val == true)
{
key[i].keyDown = true;
key[i].startTime = now;
}
}
}
}
uint8_t channelNo = 0;
void ledInit()
{
for (int i = 0; i < 7; i++)
pinMode(LED7Pin[i], OUTPUT);
for (int i = 0; i < 4; i++)
pinMode(led[i].ledPin, OUTPUT);
}
void ledSegmentsPush(uint8_t digit)
{
for (int j = 0; j < 7; j++)
digitalWrite(LED7Pin[j], bitRead(numCode[digit], j));
}
void ledShow()
{
static uint8_t i;
i = millis() / ledInterval % 4;
digitalWrite(led[i].ledPin, HIGH);
i++;
if (i > 3)
i = 0;
if ((millis() - actChStartTime) > blinkTime)
actCh = none;
if (i / 2 == actCh && bitRead(millis(), 9))
return;
else
{
ledSegmentsPush(led[i].digit);
digitalWrite(led[i].ledPin, LOW);
}
}
void setup()
{
PIN_FUNC &= 0b01111111;
pinMode(channel[0].ctlPin, OUTPUT);
digitalWrite(channel[0].ctlPin, HIGH);
pinMode(channel[1].ctlPin, OUTPUT);
digitalWrite(channel[1].ctlPin, HIGH);
ledInit();
channel[0].startTime = millis();
channel[1].startTime = millis();
TouchKey_begin((1 << 4) | (1 << 5));
TouchKey_SetMaxHalfDelta(128); // increase if sensor value are more noisy
TouchKey_SetNoiseHalfDelta(8); // If baseline need to adjust at higher rate, increase this value
TouchKey_SetNoiseCountLimit(10); // If baseline need to adjust faster, increase this value
TouchKey_SetFilterDelayLimit(5); // make overall adjustment slopwer
TouchKey_SetTouchThreshold(220); // Bigger touch pad can use a bigger value
TouchKey_SetReleaseThreshold(150); // Smaller than touch threshold
}
void loop()
{
TouchKey_Process();
uint8_t touchResult = TouchKey_Get();
if (touchResult == 32)
key[0].val = true;
else if (touchResult == 16)
key[1].val = true;
else
{
key[0].val = false;
key[1].val = false;
}
key_tick();
channelNo = (channelNo + 1) % 2;
unsigned long delta = millis() - channel[channelNo].startTime;
switch (channel[channelNo].state)
{
case DISABLED:
if (delta > disabledTime &&
channel[(channelNo + 1) % 2].state == DISABLED &&
millis() - channel[(channelNo + 1) % 2].startTime > disabledTime)
{
SAFE_MOD = 0x55; // 进入安全模式
SAFE_MOD = 0xAA;
GLOBAL_CFG |= 0x02;
PCON |= 0x02;
}
break;
case WORKING:
if (delta > 60000)
{
if (channel[channelNo].times == 0)
{
channel[channelNo].state = SHUTTING;
channel[channelNo].startTime = millis();
digitalWrite(channel[channelNo].ctlPin, LOW);
actChStartTime = millis();
actCh = channelNo;
}
else
{
channel[channelNo].times -= delta / 60000;
channel[channelNo].startTime += 60000;
}
ledUpdate();
}
break;
case SHUTTING:
if (delta > shuttingTime)
{
channel[channelNo].state = DISABLED;
channel[channelNo].startTime = millis();
led[channelNo * 2].digit = 11;
led[channelNo * 2 + 1].digit = 11;
digitalWrite(channel[channelNo].ctlPin, HIGH);
}
}
ledShow();
} |
打赏
-
查看全部打赏
|