Skip to content

Commit

Permalink
Update Timer.ino
Browse files Browse the repository at this point in the history
Полностью изменён алгоритм работы
  • Loading branch information
Timur-Khabibulin authored Mar 25, 2022
1 parent b0933d7 commit 5840939
Showing 1 changed file with 84 additions and 46 deletions.
130 changes: 84 additions & 46 deletions IntervalometerForCamera/Timer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,95 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

void Timer() {
static uint32_t timer, timer2, waitTime = 0;
if (modes.pos == modes.MANUAL_SHUTTER_SPEED) {
waitTime = camSettings.interval * 1000;
if (millis() - timer > waitTime) {
timer = millis();
take_photo(false);
if (camSettings.frame_rate > 0) {
camSettings.frame_rate--;
}
}
} else {
sleepCheck();
if (camSettings.longExpNR == false) waitTime = camSettings.interval * 1000 + camSettings.shutter_speed;
else waitTime = camSettings.interval * 1000 + camSettings.shutter_speed * 2;
if (millis() - timer > waitTime) {
timer = millis();
draw();
take_photo(true);
timer2 = millis();
while (camSettings.longExpNR == true && millis() - timer2 < camSettings.shutter_speed) {
}
canSleepFunc();
if (camSettings.frame_rate > 0) {
camSettings.frame_rate--;
}
}
}
void Start() {
if (modes.pos == modes.AUTO_SHUTTER_SPEED) camSettings.shutter_speed = 500;
if (modes.pos == modes.MANUAL_SHUTTER_SPEED) camSettings.shutter_speed = shutterSpeedValues[SSCounter];
if (modes.pos == modes.BULB_SHUTTER_SPEED) camSettings.shutter_speed = camSettings.BulbShutterSpeed * 1000; // to ms
if (modes.pos != modes.BULB_SHUTTER_SPEED) camSettings.longExpNR = false;

if (camSettings.AFControl) digitalWrite(FOCUS_PIN, HIGH);
digitalWrite(SHUTTER_PIN, HIGH);
hw_Timer_2_Start();
startFlag = true;
}

void take_photo(bool SS) {
if (camSettings.frame_rate > 0) {
if (SS == true) photoPinsControl(camSettings.shutter_speed);
else photoPinsControl(500);
}
void Stop() {
startFlag = false;
hw_Timer_1_Stop();
hw_Timer_2_Stop();
}

void hw_Timer_1_Stop() {
hwTimer1.count = 0;
cli();
TCCR1A = 0;
TCCR1B = 0;
sei();
}

void hw_Timer_2_Stop() {
hwTimer2.count = 0;
cli();
TCCR2A = 0;
TCCR2B = 0;
sei();
}

void photoPinsControl(uint32_t shutSpeed) {
static uint32_t timer, timer2, waitTime = 0;
if (camSettings.AFControl == true) waitTime = shutSpeed + FOCUSE_TIME;
else waitTime = shutSpeed;
timer = millis();
while (millis() - timer < waitTime) {
if (camSettings.AFControl == true) {
if ( millis() - timer2 > FOCUSE_TIME) {
timer2 = millis();
digitalWrite(FOCUS_PIN, HIGH);
void hw_Timer_1_Start() {
// ==== Timer 1 ====
hwTimer1.count = 0;
cli();

hwTimer1.timerVal = hwTimer1.PERIOD_SEC * hwTimer1.CPU_FREQ_HZ / hwTimer1.PRESCALER - 1;

hw_Timer_1_Stop();

OCR1A = hwTimer1.timerVal; // установка регистров совпадения

TCCR1B |= (1 << WGM12); // включить CTC режим
TCCR1B |= (1 << CS10); // Установить биты на коэффициент деления 1024
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A); // включить прерывание по совпадению таймера
// ================
sei();
}

void hw_Timer_2_Start() {
hwTimer2.count = 0;
// ==== Timer 2 =====
cli();
hwTimer2.timerVal = hwTimer2.PERIOD_SEC * hwTimer2.CPU_FREQ_HZ / hwTimer2.PRESCALER - 1;

hw_Timer_2_Stop();

OCR2A = hwTimer2.timerVal;

TCCR2A |= (1 << WGM21); // включить CTC режим
TCCR2B = (1 << CS22) | (1 << CS21) | (0 << CS20); // Установить биты на коэффициент деления 256
TIMSK2 |= (1 << OCIE2A); // включить прерывание по совпадению таймера
hwTimer2.state = true;
sei();
}

void hibernation(uint32_t intervalTime, bool sleepState) {
if (!sleepState) {
if (camSettings.longExpNR) {
if ((hwTimer1.count >= (camSettings.shutter_speed / 1000 + 2)) && (hwTimer1.count <= (camSettings.shutter_speed / 1000 + 4)))
digitalWrite(POWER_MODE_PIN, HIGH);
else {
digitalWrite(POWER_MODE_PIN, LOW);
sleepState = true;
}
} else if ((hwTimer1.count > 2) && (hwTimer1.count < 4)) digitalWrite(POWER_MODE_PIN, HIGH);
else {
digitalWrite(POWER_MODE_PIN, LOW);
sleepState = true;
}
} else {
if ((hwTimer1.count > (intervalTime - TIME_TO_WAKEUP_SEC)) && (hwTimer1.count < intervalTime)) digitalWrite(POWER_MODE_PIN, HIGH);
else {
digitalWrite(POWER_MODE_PIN, LOW);
sleepState = false;
}
digitalWrite(SHUTTER_PIN, HIGH);
}
if (camSettings.AFControl == true) digitalWrite(FOCUS_PIN, LOW);
digitalWrite(SHUTTER_PIN, LOW);
}

0 comments on commit 5840939

Please sign in to comment.