Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

alarm_flipflop.c

Example of polling hardware alarm.
/*
 * Copyright (C) 2003 BLADOX, s.r.o.  All rights reserved.
 * 
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <config.h>
#include <turbo/turbo.h>

#include <stdlib.h>
#include <string.h>

u8 PROGMEM t_Foo_eno[] = "SAFE OPENED!";        // alarm text
u8 PROGMEM t_Foo_enc[] = "SAFE CLOSED!";        // alarm text
u8 PROGMEM t_ms[] = "+420123456789";    // whom to send alarm

u8 alarm_state;                 //=1 if intruder detected, otherwise = 0
u8 pin_state;                   //=1 if intruder detected, otherwise = 0

//------------------------------------------------------------------------
//  INPUT pin of ALARM application (A15 = PC7)
//------------------------------------------------------------------------
#define ALARM_DDR DDRC
#define ALARM_DDR_PIN DD7
#define ALARM_PORT PORTC
#define ALARM_PORT_PIN PORT7
#define ALARM_PIN PINC
#define ALARM_PIN_PIN PIN7

void reset_flipflop ()
{
  sbi (ALARM_PORT, ALARM_PORT_PIN);     //set ALARM pin output register bit to log.1
  asm __volatile__ ("nop");     //wait

  sbi (ALARM_DDR, ALARM_DDR_PIN);       //ALARM pin is OUTPUT
  asm __volatile__ ("nop");     //wait

  cbi (ALARM_PORT, ALARM_PORT_PIN);     //set ALARM pin output register bit to log.0
  asm __volatile__ ("nop");     //wait

  sbi (ALARM_PORT, ALARM_PORT_PIN);     //set ALARM pin output register bit to log.1
  asm __volatile__ ("nop");     //wait

  cbi (ALARM_DDR, ALARM_DDR_PIN);       //ALARM pin is INPUT
}

void action_sms_opened (void *data)
{
  u8 *tmp = malloc (100);
  u8 *ms = str2msisdn (t_ms, MSISDN_ADN, MEM_R);

  memcpy (tmp, t_Foo_eno, sizeof (t_Foo_eno));  // answer

// sms on display
  send_sms (tmp, sizeof (t_Foo_eno), ms, MSISDN_ADN, 0xF4, 0, NULL, NULL);
// sms to ME
  send_sms (tmp, sizeof (t_Foo_eno), ms, MSISDN_ADN, 0xF5, 0, NULL, NULL);

  free (tmp);
  free (ms);
}

void action_sms_closed (void *data)
{
  u8 *tmp = malloc (100);
  u8 *ms = str2msisdn (t_ms, MSISDN_ADN, MEM_R);

  memcpy (tmp, t_Foo_enc, sizeof (t_Foo_enc));  // answer
// do not send info about change to normal state to the display
// to keep info about disturbance on the display
// sms on display
// send_sms (tmp, sizeof (t_Foo_enc), ms, MSISDN_ADN, 0xF4, 0, NULL, NULL);
// sms to ME
  send_sms (tmp, sizeof (t_Foo_enc), ms, MSISDN_ADN, 0xF5, 0, NULL, NULL);

  free (tmp);
  free (ms);
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_APP_INIT:
      reset_flipflop ();        //initialize sensor logic
      alarm_state = 0;          //no intruder detected
      reg_action (ACTION_STATUS);
      break;
    case ACTION_STATUS:
      if (alarm_state != 0)     // if alarm_state is 1, we have to reset FlipFlop to get actual state from sensor
        reset_flipflop ();
      pin_state = (inb (ALARM_PIN) & (1 << ALARM_PIN_PIN));     // read ALARM pin
      if (pin_state != alarm_state)     // test if input ALARM pin value has changed
      {
        alarm_state = pin_state;        // update alarm_state
        if (pin_state != 0)
        {                       // sensor has detected disturbance
          stk_thread (action_sms_opened, NULL);
        }
        else
        {                       // sensor has not detected disturbance
          stk_thread (action_sms_closed, NULL);
        }
      }
      break;
    default:
      break;
  }
}


Copyright © 2004-2006 BLADOX
Turbo version 1.2