Permalink
Browse files

genesis for remote control firmware using USART/RS485

  • Loading branch information...
alxlo committed Jul 26, 2012
1 parent b8b563b commit bdac51aac14c19ee9375190f1d5d959836b5e9fd
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,84 @@
+/*
+ * PentaFnord Firmware
+ *
+ * by Alexander Lorz <bigalex@gmx.de>
+ *
+ *
+ * Parts of this software are derived work, originating from the fine
+ * fnordlicht firmware (see http://lochraster.org/fnordlichtmini)
+ * by Alexander Neumann <alexander@bumpern.de> and Lars Noschinski
+ * <lars@public.noschinski.de>, originally published and of course
+ * republished under the terms of the GNU General Public License version 3
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GLOBALS_H
+#define GLOBALS_H
+
+//#include <stdbool.h>
+#include <avr/version.h>
+#include "lib/common.h"
+
+/* check for avr-libc version */
+#if __AVR_LIBC_VERSION__ < 10600UL
+#error "newer libc version (>= 1.6.0) needed!"
+#endif
+
+/* check if cpu speed is defined */
+#ifndef F_CPU
+#error "please define F_CPU! (see Makefile)"
+#endif
+
+/* check if this cpu is supported */
+#if !(defined(__AVR_ATmega48__) || defined(__AVR_ATmega168__))
+#error "this cpu is not supported yet!"
+#endif
+
+/* Port D is used for PWM with:
+ * PD3: red
+ * PD4: green
+ * PD5: blue
+ * PD6: white
+ * PD7: ultra violet
+ */
+#define PWM_PORT D
+#define PWM_CHANNELS 5
+#define PWM_SHIFT 3
+#define PWM_CHANNEL_MASK (_BV(PD3) | _BV(PD4) | _BV(PD5) | _BV(PD6)| _BV(PD7)) //This is Red Green Blue White UV
+
+/* min brightness level */
+#define PWM_MIN_BRIGHTNESS 0
+
+#ifndef EXTERN
+#define EXTERN extern
+#endif
+
+//EXTERN uint8_t G_mode; //Current Operating mode
+//EXTERN uint8_t G_trigger;
+//EXTERN bool G_greenUp;
+//EXTERN bool G_greenDown;
+//EXTERN bool G_redUp;
+//EXTERN bool G_redDown;
+
+//#define LONGPRESS 30 //300ms //grenze zwischen Klicken und lange drücken
+//#define STARTMODE 0 //operating mode to start with
+//#define NUMMODES 5 //number of OP Modes
+//#define VALUEHOLD 50 //How long to hold maximum Values in Diags mode
+//#define INITIAL_TRIGGER 5 //anf�ngliche Empfindlichkeitssschwelle
+//#define HITEXPIRETIME 500 //maximal Dauer einer Trefferanzeige
+//#define PWRFADE_CONST 64
+//#define PWRFADE_DELAY 80
+//#define PWR_HITUP 5
+
+#endif /* GLOBALS_H */
View
@@ -0,0 +1,165 @@
+/*
+ * PentaFnord Firmware
+ *
+ * by Alexander Lorz <bigalex@gmx.de>
+ *
+ *
+ * Parts of this software are derived work, originating from the fine
+ * fnordlicht firmware (see http://lochraster.org/fnordlichtmini)
+ * by Alexander Neumann <alexander@bumpern.de> and Lars Noschinski
+ * <lars@public.noschinski.de>, originally published and of course
+ * republished under the terms of the GNU General Public License version 3
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <avr/io.h>
+#include "timer.h"
+#include "../globals.h"
+#include "button.h"
+
+/*
+ * RIGHT ADC2 PC2 PC0
+ * LEFT SPARE_C3 PC3
+ * DOWN ADC1 PC1 PC3 PB1 PC2
+ * UP ADC0 PC0
+ * PRESS SPARE_B1 PB1 PC1
+ */
+
+
+static uint8_t oldinput; // button readings from last poll cycle
+static uint8_t curinput; // button readings from current poll cycle
+static uint8_t btnstates[BTN_BUTTONS];
+static uint8_t btncounters[BTN_BUTTONS];
+static timer_t btntimers[BTN_BUTTONS];
+
+
+void button_init(){
+ //PB1 as Input
+ DDRB &= 0b11111101;
+ //enable PULLUP - do we have to?
+ PORTB |= 0b00000010;
+ //PC0-3 as input
+ DDRC &= 0b11110000;
+ PORTC |= 0b00001111;
+ // set predefined buttonstates
+ oldinput = 0;
+ curinput = 0;
+ for(uint8_t i=0; i<BTN_BUTTONS; i++){
+ btnstates[i] = BTNST_NTRL;
+ btncounters[i] = 0;
+ timer_set(&btntimers[i], 0x05); //50ms
+ }
+ //init button counter
+
+}
+
+
+// reset button to neutral state
+void button_clear(uint8_t button){
+ btnstates[button] = BTNST_NTRL;
+}
+
+
+void stateswitch(uint8_t i ){
+ switch(btnstates[i])
+ {
+ case BTNST_NTRL: //NEUTRAL
+ if (curinput & (1<<i)){ //button down
+ btncounters[i] = 0;
+ btnstates[i] = BTNST_DBNC;
+ }
+ break;
+ //intermediate state, check if button is still pressed to debounce
+ case BTNST_DBNC:
+ btnstates[i] = (curinput & (1<<i))? BTNST_SDN: BTNST_NTRL;
+ (btncounters[i])++;
+ break;
+
+ case BTNST_SDN: //is shortpressed and still held down
+ if (curinput & (1<<i)){
+ btncounters[i]++;
+ if (btncounters[i] > BTN_T_LONGFACT){ //500ms held
+ btnstates[i] = BTNST_LDN;
+ }
+ } else { //button was released
+ btnstates[i] = BTNST_SUP;
+ //signal shortclick
+ }
+ break;
+ case BTNST_LDN: //is longpressed and still held down
+ if (!(curinput & (1<<i))){
+ //button was released
+ btnstates[i] = BTNST_LUP; //signal longpress
+ }
+ break;
+ case BTNST_SUP: //Button came up after being pressed shortly
+ if ((curinput & (1<<i))){
+ //button was pressed again or is bouncing after release
+ btnstates[i] = BTNST_SUPDBNC; //going in special debounce
+ btncounters[i] = 0;
+ }
+ break;
+ case BTNST_LUP: //Button came up after being pressed for a long time
+ if ((curinput & (1<<i))){
+ //button was pressed again or is bouncing after release
+ btnstates[i] = BTNST_LUPDBNC; //going in special debounce
+ btncounters[i] = 0;
+ }
+ break;
+ case BTNST_SUPDBNC: //Button was pressed again after beeing short pressed(or is bouncing)
+ if ((curinput & (1<<i))){
+ //button is still pressed --> going to shortpress
+ btncounters[i]++;
+ btnstates[i] = BTNST_SDN; //starting over from short pressed
+ } else {
+ btnstates[i] = BTNST_SUP; //nope, it was bouncing, back to old state
+ }
+ break;
+ case BTNST_LUPDBNC: //Button was pressed again after beeing short pressed(or is bouncing)
+ if ((curinput & (1<<i))){
+ //button is still pressed --> going to shortpress
+ btncounters[i]++;
+ btnstates[i] = BTNST_SDN; //starting over from short pressed
+ } else {
+ btnstates[i] = BTNST_LUP; //nope, it was bouncing, back to old state
+ }
+ break;
+ default: //curently catches nothing
+ // do nothing yet
+ ;
+ } //end switch
+ timer_set(&btntimers[i], BTN_T_DEBOUNCE);
+}
+
+
+
+void button_poll(){
+ curinput = ~((PINC & 0b00001111) | ((PINB << 3) & 0b00010000)) & 0b00011111;
+ for(uint8_t i=0; i<BTN_BUTTONS; i++){
+ if (timer_expired(&btntimers[i])){
+ stateswitch(i);
+ } //end if timer expired
+ } //end for
+ oldinput = curinput;
+ return;
+}
+
+bool btn_state(uint8_t btnstate, uint8_t btn){
+ return (btnstates[btn]==btnstate);
+}
+
+
+
+
View
@@ -0,0 +1,73 @@
+/*
+ * PentaFnord Firmware
+ *
+ * by Alexander Lorz <bigalex@gmx.de>
+ *
+ *
+ * Parts of this software are derived work, originating from the fine
+ * fnordlicht firmware (see http://lochraster.org/fnordlichtmini)
+ * by Alexander Neumann <alexander@bumpern.de> and Lars Noschinski
+ * <lars@public.noschinski.de>, originally published and of course
+ * republished under the terms of the GNU General Public License version 3
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BUTTON_H
+#define BUTTON_H
+
+#include <stdbool.h>
+
+#define BTN_UP 0
+#define BTN_DOWN 1
+#define BTN_RIGHT 2
+#define BTN_LEFT 3
+#define BTN_CENTER 4
+
+#define BTN_BUTTONS 5
+
+#define BTN_T_DEBOUNCE 5 // 50ms debounce time = minimum short press time
+#define BTN_T_LONGFACT 10 // after 10 * T_DEBOUNCE = 500ms button reads as long pressed
+
+
+#define BTNST_NTRL 0 // neutral - initial state nothing interesting, please go along
+
+#define BTNST_DBNC 1 // debounce - pin went up, but we wait for things to stabilize and stop oscillating
+
+#define BTNST_SDN 2 // affirmative, button is pressed,
+ // and it's pressed no longer than
+ // BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
+
+#define BTNST_SUP 3 // and button went up after beeing pressed for a _short_ time
+
+#define BTNST_LDN 4 // button is still down for more than
+ //BTN_T_LONGFACT * BTN_T_DEBOUNCE * 10ms
+
+#define BTNST_LUP 5 // button came up after being pressed for a long time
+
+#define BTNST_SUPDBNC 6 // debounce after short up
+#define BTNST_LUPDBNC 7 // debounce after long up
+
+
+void button_init(void);
+
+void button_poll(void);
+
+// reset button to neutral state
+void button_clear(uint8_t button);
+
+//test if buttonstate von btn eqals btnstate, returns true if yes
+bool btn_state(uint8_t btnstate, uint8_t btn);
+
+
+#endif /* BUTTON_H */
View
@@ -0,0 +1,68 @@
+/*
+ * PentaFnord Firmware
+ *
+ * by Alexander Lorz <bigalex@gmx.de>
+ *
+ *
+ * Parts of this software are derived work, originating from the fine
+ * fnordlicht firmware (see http://lochraster.org/fnordlichtmini)
+ * by Alexander Neumann <alexander@bumpern.de> and Lars Noschinski
+ * <lars@public.noschinski.de>, originally published and of course
+ * republished under the terms of the GNU General Public License version 3
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef COLOR_H
+#define COLOR_H
+
+#include <stdint.h>
+#include "../globals.h"
+
+
+
+#if PWM_CHANNELS != 5
+#error "PWM_CHANNELS is not 5, this is unsupported!"
+#endif
+
+struct rgb_color_t
+{
+ union {
+ struct {
+ uint8_t red;
+ uint8_t green;
+ uint8_t blue;
+ uint8_t white;
+ uint8_t uv;
+
+ };
+ uint8_t rgbwu[5];
+ };
+};
+
+struct rgb_color_offset_t {
+ union {
+ struct {
+ int8_t red;
+ int8_t green;
+ int8_t blue;
+ uint8_t white;
+ uint8_t uv; };
+ int8_t rgbwu[5];
+ };
+};
+
+
+
+
+#endif /* COLOR_H*/
Oops, something went wrong.

0 comments on commit bdac51a

Please sign in to comment.