summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Wegener <sven.wegener@stealer.net>2015-12-21 09:38:43 +0100
committerSven Wegener <sven.wegener@stealer.net>2016-01-21 22:46:24 +0100
commit1939f3608c3fc329b396dfb9e00a7b9311b92f60 (patch)
tree207ce29537bf76fe01f3b7a6ebfe9e0c789f5b70
parentff7a42a2700115a61eeabf8eba4341bb500cfeca (diff)
Implement buttonsbuttons
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
-rw-r--r--stm8/config.h1
-rw-r--r--stm8/main.c143
2 files changed, 131 insertions, 13 deletions
diff --git a/stm8/config.h b/stm8/config.h
index 50279ac..d955f81 100644
--- a/stm8/config.h
+++ b/stm8/config.h
@@ -61,6 +61,7 @@ typedef struct {
uint16_t vout; // mV
uint16_t cout; // mA
uint8_t constant_current; // If false, we are in constant voltage
+ uint8_t buttons[4];
#if DEBUG
uint8_t pc3;
#endif
diff --git a/stm8/main.c b/stm8/main.c
index 94e6faa..b7b72ea 100644
--- a/stm8/main.c
+++ b/stm8/main.c
@@ -474,9 +474,24 @@ void config_load(void)
#endif
}
+static void dec_value(uint16_t *val, uint16_t min, uint16_t step) {
+ if (*val >= min + step) {
+ *val -= step;
+ }
+}
+
+static void inc_value(uint16_t *val, uint16_t max, uint16_t step) {
+ if (*val <= max - step) {
+ *val += step;
+ }
+}
+
void read_state(void)
{
uint8_t tmp;
+ static uint8_t display = 4;
+ static uint16_t timer, starttimer;
+ timer--;
#if DEBUG
tmp = (PC_IDR & (1<<3)) ? 1 : 0;
@@ -494,6 +509,86 @@ void read_state(void)
output_check_state(&cfg_system, state.constant_current);
}
+ state.buttons[0] = (state.buttons[0] << 1) | ((PC_IDR >> 7) & 1);
+ if (state.buttons[0] & 1) {
+ PD_DDR |= 1 << 1;
+ state.buttons[3] = (state.buttons[3] << 1) | ((PC_IDR >> 7) & 1);
+ PD_DDR &= ~(1 << 1);
+ }
+
+ state.buttons[1] = (state.buttons[1] << 1) | ((PD_IDR >> 1) & 1);
+ if (state.buttons[1] & 1) {
+ PC_DDR |= 1 << 7;
+ state.buttons[2] = (state.buttons[2] << 1) | ((PD_IDR >> 1) & 1);
+ PC_DDR &= ~(1 << 7);
+ }
+
+ if (state.buttons[0] == (1 << 7)) {
+ if (cfg_system.output) {
+ cfg_system.output = 0;
+ display = 4;
+ commit_output();
+ } else {
+ display++;
+ if (display > 6) {
+ display = 5;
+ }
+ }
+ }
+
+ if (state.buttons[1] == (1 << 7) || (state.buttons[1] == 0x00 && timer == 0)) {
+ if (display == 5 || display == 3) {
+ display = 5;
+ dec_value(&cfg_output.vset, CAP_VMIN, CAP_VSTEP);
+ } else if (display == 6 || display == 2) {
+ display = 6;
+ dec_value(&cfg_output.cset, CAP_CMIN, CAP_CSTEP);
+ }
+ autocommit();
+ starttimer /= 2;
+ if (state.buttons[1] != 0x00) {
+ starttimer = 2000;
+ }
+ timer = 100 + starttimer;
+ }
+
+ if (state.buttons[2] == (1 << 7) || (state.buttons[2] == 0x00 && timer == 0)) {
+ if (display == 5 || display == 3) {
+ display = 5;
+ inc_value(&cfg_output.vset, CAP_VMAX, CAP_VSTEP);
+ } else if (display == 6 || display == 2) {
+ display = 6;
+ inc_value(&cfg_output.cset, CAP_CMAX, CAP_CSTEP);
+ }
+ autocommit();
+ starttimer /= 2;
+ if (state.buttons[2] != 0x00) {
+ starttimer = 2000;
+ }
+ timer = 100 + starttimer;
+ }
+
+ if (state.buttons[3] == (1 << 7)) {
+ if (!cfg_system.output) {
+ cfg_system.output = 1;
+ display = 3;
+ commit_output();
+ } else {
+ if (display == 5) {
+ commit_output();
+ display = 3;
+ } else if (display == 6) {
+ commit_output();
+ display = 2;
+ } else {
+ display++;
+ if (display > 3) {
+ display = 2;
+ }
+ }
+ }
+ }
+
if (adc_ready()) {
uint16_t val = adc_read();
uint8_t ch = adc_channel();
@@ -516,22 +611,44 @@ void read_state(void)
// Calculation: val * cal_vin * 3.3 / 1024
state.vin = adc_to_volt(val, &cfg_system.vin_adc);
ch = 2;
- {
- uint8_t ch1;
- uint8_t ch2;
- uint8_t ch3;
- uint8_t ch4;
-
- ch1 = '0' + (state.vin / 10000) % 10;
- ch2 = '0' + (state.vin / 1000) % 10;
- ch3 = '0' + (state.vin / 100) % 10;
- ch4 = '0' + (state.vin / 10 ) % 10;
-
- display_show(ch1, 0, ch2, 1, ch3, 0, ch4, 0);
- }
break;
}
+ if (display == 2) {
+ val = state.cout;
+ } else if (display == 3) {
+ val = state.vout;
+ } else if (display == 4) {
+ val = state.vin;
+ } else if (display == 5) {
+ val = cfg_output.vset;
+ } else if (display == 6) {
+ val = cfg_output.cset;
+ }
+
+ if (display == ch || (display > 4 && ch == 2)) {
+ uint8_t ch1;
+ uint8_t ch2;
+ uint8_t ch3;
+ uint8_t ch4;
+
+ if (display == 2 || display == 6) {
+ ch1 = '0' + (val / 1000) % 10;
+ ch2 = '0' + (val / 100) % 10;
+ ch3 = '0' + (val / 10) % 10;
+ ch4 = '0' + val % 10;
+
+ display_show(ch1, 1, ch2, 0, ch3, 0, ch4, 0);
+ } else {
+ ch1 = '0' + (val / 10000) % 10;
+ ch2 = '0' + (val / 1000) % 10;
+ ch3 = '0' + (val / 100) % 10;
+ ch4 = '0' + (val / 10 ) % 10;
+
+ display_show(ch1, 0, ch2, 1, ch3, 0, ch4, 0);
+ }
+ }
+
adc_start(ch);
}
}