fix models list reload after USB mass storage connection (#5963)
[opentx.git] / radio / src / main_avr.cpp
blob24aaa1e9f65ac98a6304e3d08b35dc981e856833
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 void checkBattery();
24 uint8_t checkTrim(event_t event);
26 void perMain()
28 #if defined(SIMU)
29 doMixerCalculations();
30 #endif
31 #if defined(LCD_ST7920)
32 uint8_t lcdstate=0;
33 #define IS_LCD_REFRESH_ALLOWED() (0==lcdstate)
34 #else
35 #define IS_LCD_REFRESH_ALLOWED() (1)
36 #endif
37 uint16_t t0 = getTmr16KHz();
38 int16_t delta = (nextMixerEndTime - lastMixerDuration) - t0;
39 if (delta > 0 && delta < MAX_MIXER_DELTA) {
40 #if defined(PCBSTD) && defined(ROTARY_ENCODER_NAVIGATION)
41 rotencPoll();
42 #endif
44 // @@@ open.20.fsguruh
45 // SLEEP(); // wouldn't that make sense? should save a lot of battery power!!!
46 /* for future use; currently very very beta... */
47 #if defined(POWER_SAVE)
48 ADCSRA&=0x7F; // disable ADC for power saving
49 ACSR&=0xF7; // disable ACIE Interrupts
50 ACSR|=0x80; // disable Analog Comparator
51 // maybe we disable here a lot more hardware components in future to save even more power
55 MCUCR|=0x20; // enable Sleep (bit5)
56 // MCUCR|=0x28; // enable Sleep (bit5) enable ADC Noise Reduction (bit3)
57 // first tests showed: simple sleep would reduce cpu current from 40.5mA to 32.0mA
58 // noise reduction sleep would reduce it down to 28.5mA; However this would break pulses in theory
59 // however with standard module, it will need about 95mA. Therefore the drop to 88mA is not much noticable
60 do {
61 asm volatile(" sleep \n\t"); // if _SLEEP() is not defined use this
62 t0=getTmr16KHz();
63 delta= (nextMixerEndTime - lastMixerDuration) - t0;
64 } while ((delta>0) && (delta<MAX_MIXER_DELTA));
66 // reenabling of the hardware components needed here
67 MCUCR&=0x00; // disable sleep
68 ADCSRA|=0x80; // enable ADC
69 #endif
70 return;
73 nextMixerEndTime = t0 + MAX_MIXER_DELTA;
74 // this is a very tricky implementation; lastMixerEndTime is just like a default value not to stop mixcalculations totally;
75 // the real value for lastMixerEndTime is calculated inside pulses_XXX.cpp which aligns the timestamp to the pulses generated
76 // nextMixerEndTime is actually defined inside pulses_XXX.h
78 doMixerCalculations();
80 t0 = getTmr16KHz() - t0;
81 lastMixerDuration = t0;
82 if (t0 > maxMixerDuration) maxMixerDuration = t0;
84 #if defined(MODULE_ALWAYS_SEND_PULSES)
85 if (startupWarningState < STARTUP_WARNING_DONE) {
86 // don't do menu's until throttle and switch warnings are handled
87 return;
89 #endif
91 if (eepromIsTransferComplete()) {
92 if (theFile.isWriting())
93 theFile.nextWriteStep();
94 else if (TIME_TO_WRITE())
95 storageCheck(false);
98 #if defined(SDCARD)
99 sdMountPoll();
100 logsWrite();
101 #endif
103 event_t evt = getEvent();
104 evt = checkTrim(evt);
106 if (evt && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) backlightOn(); // on keypress turn the light on
107 doLoopCommonActions();
109 #if defined(TELEMETRY_FRSKY) || defined(TELEMETRY_MAVLINK)
110 telemetryWakeup();
111 #endif
113 #if defined(NAVIGATION_STICKS)
114 uint8_t sticks_evt = getSticksNavigationEvent();
115 if (sticks_evt) evt = sticks_evt;
116 #endif
118 #if defined(GUI)
119 const char * warn = warningText;
120 bool popupMenuActive = (popupMenuNoItems > 0);
122 if (IS_LCD_REFRESH_ALLOWED()) { // No need to redraw until lcdRefresh_ST7920(0) below completely refreshes the display.
123 lcdClear();
124 if (menuEvent) {
125 menuVerticalPosition = menuEvent == EVT_ENTRY_UP ? menuVerticalPositions[menuLevel] : 0;
126 menuHorizontalPosition = 0;
127 evt = menuEvent;
128 menuEvent = 0;
130 menuHandlers[menuLevel]((warn || popupMenuActive) ? 0 : evt);
132 if (warn) DISPLAY_WARNING(evt);
133 #if defined(NAVIGATION_MENUS)
134 if (popupMenuActive) {
135 const char * result = runPopupMenu(evt);
136 if (result) {
137 popupMenuHandler(result);
140 #endif
141 drawStatusLine();
144 #if defined(LCD_KS108)
145 lcdRefreshSide();
146 #elif defined(LCD_ST7920)
147 lcdstate = lcdRefresh_ST7920(0);
148 #else
149 lcdRefresh();
150 #endif
152 #endif // defined(GUI)
154 if (SLAVE_MODE()) {
155 JACK_PPM_OUT();
157 else {
158 JACK_PPM_IN();
161 checkBattery();