demos_source/code_TestDoorLight.c

Go to the documentation of this file.
00001 /*
00002  * Femto OS v 0.91 - Copyright (C) 2008-2009 Ruud Vlaming
00003  *
00004  * This file is part of the Femto OS distribution.
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, version 3 of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Please note that, due to the GPLv3 license, for application of this
00019  * work and/or combined work in embedded systems special obligations apply.
00020  * If these are not to you liking, please know the Femto OS is dual
00021  * licensed. A commercial license and support are available.
00022  * See http://www.femtoos.org/ for details.
00023  */
00024 
00025 /* This file is solely for demonstration purposes. */
00026 
00027 #include "femtoos_code.h"
00028 
00042 /* Connections
00043  * PA0 out : led beacon (making it easy to find the switch in the dark)
00044  * PA1 out : led signal (giving information about the internal state)
00045  * PA2 out : 230V light (via solid state relay)
00046  * PA3 out : unused
00047  * PA4 in  : capacitive switch
00048  * PA5 in  : unused
00049  * PA6 in  : unused
00050  * PA7 in  : unused
00051  */
00052 
00053 #define pinLEDbeacon PA0
00054 #define pinLEDsignal PA1
00055 #define pinLAMP      PA2
00056 #define pinSWITCH    PB0
00057 
00058 void appBoot(void)
00059 { DDRA  |= 0x07;
00060   PORTA &= 0xF7;
00061   DDRB  &= 0xFE;
00062   PORTB &= 0xFE; }
00063 
00064 /* here you don't need protection since the PORTA operations are sbi/cbi which are atomair operations */
00065 #define setChannel(channel,state)  if (!state) { PORTA |= (1 << (channel)); } else { PORTA &=  ~(1 << (channel)); }
00066 
00067 /* uiDelayMode
00068  * 0: LAMP off
00069  * 1: LAMP 2 min on
00070  * 2: LAMP 15 min on
00071  * 3: LAMP 60 min on
00072  * 4: LAMP 4.5 hours on (max delay)
00073  *
00074  * uiLedMode
00075  * 0: Led is off, switching at this moment switches off lamp
00076  * 1: Led is on beacon, switching at this moment increases DelayMode (Lamp is switched on)
00077  * 2: led if on, switching at this moment increases DelayMode
00078  * 3: led is in flash state, switching is ignored.
00079  */
00080 
00081 volatile Tuint08 uiLedMode;
00082 volatile Tuint08 uiDelayMode;
00083 volatile Tuint08 uiDelayTimer;
00084 
00085 
00086 void appTick16(void)
00087 { if (uiDelayTimer == 0) { uiDelayMode = 0; } else { uiDelayTimer--; }  }
00088 
00089 
00090 #if (preTaskDefined(ReadSwitch))
00091 
00092 void appLoop_ReadSwitch(void)
00093 { Tuint08 uiSwitchOldState = PINB & (1<<pinSWITCH);;
00094   Tuint08 uiSwitchNewState;
00095   while (true)
00096   { uiSwitchNewState = PINB & (1<<pinSWITCH);
00097     if (uiSwitchOldState !=  uiSwitchNewState)
00098     { uiSwitchOldState =  uiSwitchNewState;
00099       Tuint08 uiLM = uiLedMode;
00100       if (uiLM==0)
00101       { uiDelayMode = 0;
00102         uiDelayTimer = 0; }
00103       else if (uiLM < 3)
00104       { if (uiDelayMode++ > 4) uiDelayMode=4;
00105         switch (uiDelayMode)
00106         { case 1 : { uiDelayTimer = 2;   } break;
00107           case 2 : { uiDelayTimer = 15;  } break;
00108           case 3 : { uiDelayTimer = 60;  } break;
00109           default: { uiDelayTimer = 255; } break; } } }
00110     taskDelayFromNow(200); } }
00111 
00112 #endif
00113 
00114 
00115 #if (preTaskDefined(DriveLed))
00116 
00117 static void LedOff()
00118 { if (uiDelayMode > 0)
00119   { uiLedMode=0;
00120     setChannel(pinLEDbeacon,false);
00121     setChannel(pinLEDsignal,false);
00122     taskDelayFromNow(5000); } }
00123 
00124 static void LedFlash()
00125 { if (uiDelayMode > 0)
00126   { Tuint08 uiDM = uiDelayMode;
00127     uiLedMode=3;
00128     setChannel(pinLEDbeacon,false);
00129     while (uiDM-- > 0)
00130     { setChannel(pinLEDsignal,true);
00131       taskDelayFromNow(200);
00132       setChannel(pinLEDsignal,false);
00133       taskDelayFromNow(200); } } }
00134 
00135 static void LedOn()
00136 { if (uiDelayMode > 0)
00137   { uiLedMode=2;
00138     setChannel(pinLEDbeacon,false);
00139     setChannel(pinLEDsignal,true);
00140     taskDelayFromNow(5000); } }
00141 
00142 static void LedBeacon()
00143 { if (uiDelayMode == 0)
00144   { uiLedMode=1;
00145     setChannel(pinLEDbeacon,true);
00146     setChannel(pinLEDsignal,false);
00147     taskDelayFromNow(200); } }
00148 
00149 void appLoop_DriveLed(void)
00150 { while (true)
00151   { LedFlash();
00152     LedBeacon();
00153     LedOn();
00154     LedOff(); } }
00155 
00156 #endif
00157 
00158 
00159 #if (preTaskDefined(DriveLamp))
00160 
00161 void appLoop_DriveLamp(void)
00162 { while (true)
00163   { setChannel(pinLAMP,uiDelayMode>0)
00164     taskDelayFromNow(150); } }
00165 
00166 #endif
00167 

Generated on Fri Oct 16 00:05:21 2009 for FemtoOS by  doxygen 1.5.2