Five Guys – Security System – Code


#include <asf.h>

#include "keyboard.h"

#include "touch_api.h"

#include <stdio.h>

#include <conf_example.h>

#include <gfx_mono.h>

&nbsp;

//define our own codes for easier use with buttons

#define BOTTOM_RIGHT  40

#define TOP_RIGHT    38

#define TOP_LEFT  8

#define BOTTOM_LEFT 13

&nbsp;

//global

struct pwm_config mypwm[2]; //For your PWM configuration –CKH

struct keyboard_event input; // probes for button push

&nbsp;

//function declarations

bool CheckKey();

bool KeyToCheck(<em>uint8_t</em>);

&nbsp;

static void adc_handler(ADC_t *adc, <em>uint8_t</em> ch_mask, adc_result_t result)

{

#ifdef CONF_BOARD_OLED_UG_2832HSWEG04

gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);

#endif

<em>int32_t</em> value;

char out_str[OUTPUT_STR_SIZE];

char *outputString = <em>NULL</em>;

bool valid;

bool goingUp = true;

int alarmSound = 10;

&nbsp;

value = (<em>int8_t</em>)(result);

&nbsp;

if(value > 70){//"low level" alert - LED flashes

pwm_start(&mypwm[0], 50);

}

if(value > 100){//"high level alert" - LED lit and buzzer armed

//alarm is going off and we need to check the password

gfx_mono_init();

gfx_mono_draw_string("Enter Password", 5, 5, &sysfont);

pwm_start(&mypwm[0], 100); // LED fully lit

valid = CheckKey();

if(!valid){

gfx_mono_init();

gfx_mono_draw_string("Calling Police",  5, 5, &sysfont);

while(true){

keyboard_get_key_state(&input);

if (input.type == KEYBOARD_RELEASE)

{

if(input.keycode == TOP_RIGHT){//end logic loop if password is correct

break;

}

}

delay_ms(250);

pwm_start(&mypwm[1], alarmSound);

if(alarmSound == 50){//restart "breathing" alarm loop

goingUp = false;

}

if(alarmSound == 10){ // "breathing" alarm loop

goingUp = true;

}

if(goingUp){

alarmSound += 10;

}

else{

alarmSound -= 10;

}

}

gfx_mono_init();

gfx_mono_draw_string("CORRECT!", 5, 5, &sysfont);

}

}

else{

pwm_start(&mypwm[0], 0); // restart LED if password is correct

pwm_start(&mypwm[1], 0);// restart buzzer if password is correct

}

//

//snprintf(out_str, OUTPUT_STR_SIZE, "%4d", value);

//gfx_mono_draw_string(out_str, 0, 0, &sysfont);

// Start next conversion.

adc_start_conversion(adc, ch_mask);

}

&nbsp;

int main(void)

{

struct adc_config         adc_conf;

struct adc_channel_config adcch_conf;

&nbsp;

board_init();

sysclk_init();

sleepmgr_init();

irq_initialize_vectors();

cpu_irq_enable();

gfx_mono_init();

&nbsp;

// Enable backlight if display type is not OLED

#ifndef CONF_BOARD_OLED_UG_2832HSWEG04

ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN);

#endif

&nbsp;

&nbsp;

&nbsp;

/* Set up a PWM channel with 500 Hz frequency. Here we will output 4 different pulse trains on 4 pins of the A3BU Xplained.*/

pwm_init(&mypwm[0], PWM_TCC0, PWM_CH_A, 1000);//this is SDA on J1 on the A3BU Xplained - LED

pwm_init(&mypwm[1], PWM_TCC0, PWM_CH_B, 1000);// this is SCL on J1 - buzzer

// both at 1 kHz PWM

pwm_start(&mypwm[0], 100);

pwm_start(&mypwm[1], 100);

// Initialize configuration structures.

adc_read_configuration(&ADCB, &adc_conf);

adcch_read_configuration(&ADCB, ADC_CH0, &adcch_conf);

&nbsp;

adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_8,

ADC_REF_VCC);

adc_set_clock_rate(&adc_conf, 200000UL);

adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);

adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);

&nbsp;

adc_write_configuration(&ADCB, &adc_conf);

adc_set_callback(&ADCB, &adc_handler);

&nbsp;

/* Configure ADC channel 0:

* - single-ended measurement from temperature sensor

* - interrupt flag set on completed conversion

* - interrupts disabled

*/

adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE,

1);

adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);

adcch_enable_interrupt(&adcch_conf);

&nbsp;

adcch_write_configuration(&ADCB, ADC_CH0, &adcch_conf);

&nbsp;

// Enable the ADC and start the first conversion.

adc_enable(&ADCB);

adc_start_conversion(&ADCB, ADC_CH0);

&nbsp;

do {

// Sleep until ADC interrupt triggers.

sleepmgr_enter_sleep();

} while (1);

}

&nbsp;

bool CheckKey() //password check in high alert

{

bool isValid = false;

&nbsp;

isValid = KeyToCheck(TOP_RIGHT);

if(!isValid) return false;

isValid = KeyToCheck(BOTTOM_RIGHT);

if(!isValid) return false;

&nbsp;

return true;

}

&nbsp;

bool KeyToCheck(<em>uint8_t</em> nextCorrectKey)//receives button input and checks against pre-programmed password

{

// any key will exit the loop

while (true)

{

keyboard_get_key_state(&input);

if (input.type == KEYBOARD_RELEASE)

{

if(input.keycode == nextCorrectKey){

return true;

}

else{

return false;

}

}

}

}