commit ef074f78201e74f50e9769a97651b2d8e9b85b4f Author: armBookCodeExamples <armBookCodeExamples@gmail.com> Date: Thu, 17 Mar 2022 18:42:26 -0300 initial commit Diffstat:
| A | .gitignore | | | 10 | ++++++++++ |
| A | main.cpp | | | 14 | ++++++++++++++ |
| A | mbed-os.lib | | | 2 | ++ |
| A | mbed_app.json | | | 8 | ++++++++ |
| A | modules/arm_book_lib.h | | | 33 | +++++++++++++++++++++++++++++++++ |
| A | modules/code/code.cpp | | | 109 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/code/code.h | | | 25 | +++++++++++++++++++++++++ |
| A | modules/date_and_time/date_and_time.cpp | | | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/date_and_time/date_and_time.h | | | 20 | ++++++++++++++++++++ |
| A | modules/display/display.cpp | | | 289 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/display/display.h | | | 30 | ++++++++++++++++++++++++++++++ |
| A | modules/event_log/event_log.cpp | | | 117 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/event_log/event_log.h | | | 31 | +++++++++++++++++++++++++++++++ |
| A | modules/fire_alarm/fire_alarm.cpp | | | 149 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/fire_alarm/fire_alarm.h | | | 22 | ++++++++++++++++++++++ |
| A | modules/gas_sensor/gas_sensor.cpp | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
| A | modules/gas_sensor/gas_sensor.h | | | 18 | ++++++++++++++++++ |
| A | modules/matrix_keypad/matrix_keypad.cpp | | | 141 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/matrix_keypad/matrix_keypad.h | | | 18 | ++++++++++++++++++ |
| A | modules/pc_serial_com/pc_serial_com.cpp | | | 311 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/pc_serial_com/pc_serial_com.h | | | 22 | ++++++++++++++++++++++ |
| A | modules/siren/siren.cpp | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/siren/siren.h | | | 19 | +++++++++++++++++++ |
| A | modules/smart_home_system/smart_home_system.cpp | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/smart_home_system/smart_home_system.h | | | 20 | ++++++++++++++++++++ |
| A | modules/strobe_light/strobe_light.cpp | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/strobe_light/strobe_light.h | | | 20 | ++++++++++++++++++++ |
| A | modules/temperature_sensor/temperature_sensor.cpp | | | 86 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/temperature_sensor/temperature_sensor.h | | | 21 | +++++++++++++++++++++ |
| A | modules/user_interface/user_interface.cpp | | | 193 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | modules/user_interface/user_interface.h | | | 26 | ++++++++++++++++++++++++++ |
31 files changed, 2006 insertions(+), 0 deletions(-) diff --git a/.gitignore b/.gitignore @@ -0,0 +1,10 @@ +.build +.mbed +projectfiles +*.py* +mbed-os +BUILD + +compile_commands.json +.clangd +.cache diff --git a/main.cpp b/main.cpp @@ -0,0 +1,13 @@ +//=====[Libraries]============================================================= + +#include "smart_home_system.h" + +//=====[Main function, the program entry point after power on or reset]======== + +int main() +{ + smartHomeSystemInit(); + while (true) { + smartHomeSystemUpdate(); + } +} +\ No newline at end of file diff --git a/mbed-os.lib b/mbed-os.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/mbed-os.git#26606218ad9d1ee1c8781aa73774fd7ea3a7658e +\ No newline at end of file diff --git a/mbed_app.json b/mbed_app.json @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "*": { + "target.printf_lib": "std" + } + } +} +\ No newline at end of file diff --git a/modules/arm_book_lib.h b/modules/arm_book_lib.h @@ -0,0 +1,32 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _ARM_BOOK_LIB_H_ +#define _ARM_BOOK_LIB_H_ + +//=====[Libraries]============================================================= + +#include <mbed.h> + +//=====[Declaration of public defines]========================================= + +// Functional states +#ifndef OFF +#define OFF 0 +#endif +#ifndef ON +#define ON (!OFF) +#endif + +// Electrical states +#ifndef LOW +#define LOW 0 +#endif +#ifndef HIGH +#define HIGH (!LOW) +#endif + +#define delay(ms) thread_sleep_for( ms ) + +//=====[#include guards - end]================================================= + +#endif // _ARM_BOOK_LIB_H_ +\ No newline at end of file diff --git a/modules/code/code.cpp b/modules/code/code.cpp @@ -0,0 +1,108 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "code.h" + +#include "user_interface.h" +#include "pc_serial_com.h" +#include "date_and_time.h" +#include "temperature_sensor.h" +#include "gas_sensor.h" +#include "matrix_keypad.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +//=====[Declaration of external public global variables]======================= + +extern char codeSequenceFromUserInterface[CODE_NUMBER_OF_KEYS]; +extern char codeSequenceFromPcSerialCom[CODE_NUMBER_OF_KEYS]; + +//=====[Declaration and initialization of private global variables]============ + +static int numberOfIncorrectCodes = 0; +static char codeSequence[CODE_NUMBER_OF_KEYS] = { '1', '8', '0', '5' }; + +//=====[Declarations (prototypes) of private functions]======================== + +static bool codeMatch( char* codeToCompare ); +static void codeDeactivate(); + +//=====[Implementations of public functions]=================================== + +void codeWrite( char* newCodeSequence ) +{ + int i; + for (i = 0; i < CODE_NUMBER_OF_KEYS; i++) { + codeSequence[i] = newCodeSequence[i]; + } +} + +bool codeMatchFrom( codeOrigin_t codeOrigin ) +{ + bool codeIsCorrect = false; + switch (codeOrigin) { + case CODE_KEYPAD: + if( userInterfaceCodeCompleteRead() ) { + codeIsCorrect = codeMatch(codeSequenceFromUserInterface); + userInterfaceCodeCompleteWrite(false); + if ( codeIsCorrect ) { + codeDeactivate(); + } else { + incorrectCodeStateWrite(ON); + numberOfIncorrectCodes++; + } + } + + + break; + case CODE_PC_SERIAL: + if( pcSerialComCodeCompleteRead() ) { + codeIsCorrect = codeMatch(codeSequenceFromPcSerialCom); + pcSerialComCodeCompleteWrite(false); + if ( codeIsCorrect ) { + codeDeactivate(); + pcSerialComStringWrite( "\r\nThe code is correct\r\n\r\n" ); + } else { + incorrectCodeStateWrite(ON); + numberOfIncorrectCodes++; + pcSerialComStringWrite( "\r\nThe code is incorrect\r\n\r\n" ); + } + } + + break; + default: + break; + } + + if ( numberOfIncorrectCodes >= 5 ) { + systemBlockedStateWrite(ON); + } + + return codeIsCorrect; +} + +//=====[Implementations of private functions]================================== + +static bool codeMatch( char* codeToCompare ) +{ + int i; + for (i = 0; i < CODE_NUMBER_OF_KEYS; i++) { + if ( codeSequence[i] != codeToCompare[i] ) { + return false; + } + } + return true; +} + +static void codeDeactivate() +{ + systemBlockedStateWrite(OFF); + incorrectCodeStateWrite(OFF); + numberOfIncorrectCodes = 0; +} +\ No newline at end of file diff --git a/modules/code/code.h b/modules/code/code.h @@ -0,0 +1,24 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _CODE_H_ +#define _CODE_H_ + +//=====[Declaration of public defines]========================================= + +#define CODE_NUMBER_OF_KEYS 4 + +//=====[Declaration of public data types]====================================== + +typedef enum{ + CODE_KEYPAD, + CODE_PC_SERIAL, +} codeOrigin_t; + +//=====[Declarations (prototypes) of public functions]========================= + +void codeWrite( char* newCodeSequence ); +bool codeMatchFrom( codeOrigin_t codeOrigin ); + +//=====[#include guards - end]================================================= + +#endif // _CODE_H_ +\ No newline at end of file diff --git a/modules/date_and_time/date_and_time.cpp b/modules/date_and_time/date_and_time.cpp @@ -0,0 +1,48 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" + +#include "date_and_time.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +char* dateAndTimeRead() +{ + time_t epochSeconds; + epochSeconds = time(NULL); + return ctime(&epochSeconds); +} + +void dateAndTimeWrite( int year, int month, int day, + int hour, int minute, int second ) +{ + struct tm rtcTime; + + rtcTime.tm_year = year - 1900; + rtcTime.tm_mon = month - 1; + rtcTime.tm_mday = day; + rtcTime.tm_hour = hour; + rtcTime.tm_min = minute; + rtcTime.tm_sec = second; + + rtcTime.tm_isdst = -1; + + set_time( mktime( &rtcTime ) ); +} + +//=====[Implementations of private functions]================================== + diff --git a/modules/date_and_time/date_and_time.h b/modules/date_and_time/date_and_time.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _DATE_AND_TIME_H_ +#define _DATE_AND_TIME_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +char* dateAndTimeRead(); + +void dateAndTimeWrite( int year, int month, int day, + int hour, int minute, int second ); + +//=====[#include guards - end]================================================= + +#endif // _DATE_AND_TIME_H_ +\ No newline at end of file diff --git a/modules/display/display.cpp b/modules/display/display.cpp @@ -0,0 +1,288 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" +#include "display.h" + +//=====[Declaration of private defines]======================================== + +#define DISPLAY_IR_CLEAR_DISPLAY 0b00000001 +#define DISPLAY_IR_ENTRY_MODE_SET 0b00000100 +#define DISPLAY_IR_DISPLAY_CONTROL 0b00001000 +#define DISPLAY_IR_FUNCTION_SET 0b00100000 +#define DISPLAY_IR_SET_DDRAM_ADDR 0b10000000 + +#define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010 +#define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000 +#define DISPLAY_IR_ENTRY_MODE_SET_SHIFT 0b00000001 +#define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT 0b00000000 + +#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON 0b00000100 +#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000 +#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON 0b00000010 +#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF 0b00000000 +#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON 0b00000001 +#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF 0b00000000 + +#define DISPLAY_IR_FUNCTION_SET_8BITS 0b00010000 +#define DISPLAY_IR_FUNCTION_SET_4BITS 0b00000000 +#define DISPLAY_IR_FUNCTION_SET_2LINES 0b00001000 +#define DISPLAY_IR_FUNCTION_SET_1LINE 0b00000000 +#define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100 +#define DISPLAY_IR_FUNCTION_SET_5x8DOTS 0b00000000 + +#define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0 +#define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64 +#define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20 +#define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84 + +#define DISPLAY_RS_INSTRUCTION 0 +#define DISPLAY_RS_DATA 1 + +#define DISPLAY_RW_WRITE 0 +#define DISPLAY_RW_READ 1 + +#define DISPLAY_PIN_RS 4 +#define DISPLAY_PIN_RW 5 +#define DISPLAY_PIN_EN 6 +#define DISPLAY_PIN_D0 7 +#define DISPLAY_PIN_D1 8 +#define DISPLAY_PIN_D2 9 +#define DISPLAY_PIN_D3 10 +#define DISPLAY_PIN_D4 11 +#define DISPLAY_PIN_D5 12 +#define DISPLAY_PIN_D6 13 +#define DISPLAY_PIN_D7 14 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut displayD0( D0 ); +DigitalOut displayD1( D1 ); +DigitalOut displayD2( D2 ); +DigitalOut displayD3( D3 ); +DigitalOut displayD4( D4 ); +DigitalOut displayD5( D5 ); +DigitalOut displayD6( D6 ); +DigitalOut displayD7( D7 ); +DigitalOut displayRs( D8 ); +DigitalOut displayEn( D9 ); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static display_t display; +static bool initial8BitCommunicationIsCompleted; + +//=====[Declarations (prototypes) of private functions]======================== + +static void displayPinWrite( uint8_t pinName, int value ); +static void displayDataBusWrite( uint8_t dataByte ); +static void displayCodeWrite( bool type, uint8_t dataBus ); + +//=====[Implementations of public functions]=================================== + +void displayInit( displayConnection_t connection ) +{ + display.connection = connection; + + initial8BitCommunicationIsCompleted = false; + + delay( 50 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 5 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 1 ); + + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS | + DISPLAY_IR_FUNCTION_SET_2LINES | + DISPLAY_IR_FUNCTION_SET_5x8DOTS ); + delay( 1 ); + break; + + case DISPLAY_CONNECTION_GPIO_4BITS: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_4BITS ); + delay( 1 ); + + initial8BitCommunicationIsCompleted = true; + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_4BITS | + DISPLAY_IR_FUNCTION_SET_2LINES | + DISPLAY_IR_FUNCTION_SET_5x8DOTS ); + delay( 1 ); + break; + } + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_DISPLAY_CONTROL | + DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF | + DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | + DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_CLEAR_DISPLAY ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_ENTRY_MODE_SET | + DISPLAY_IR_ENTRY_MODE_SET_INCREMENT | + DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_DISPLAY_CONTROL | + DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | + DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | + DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); + delay( 1 ); +} + +void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY ) +{ + switch( charPositionY ) { + case 0: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 1: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 2: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 3: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + } +} + + +void displayStringWrite( const char * str ) +{ + while (*str) { + displayCodeWrite(DISPLAY_RS_DATA, *str++); + } +} + +//=====[Implementations of private functions]================================== + +static void displayCodeWrite( bool type, uint8_t dataBus ) +{ + if ( type == DISPLAY_RS_INSTRUCTION ) + displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION); + else + displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_DATA); + displayPinWrite( DISPLAY_PIN_RW, DISPLAY_RW_WRITE ); + displayDataBusWrite( dataBus ); +} + +static void displayPinWrite( uint8_t pinName, int value ) +{ + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + switch( pinName ) { + case DISPLAY_PIN_D0: displayD0 = value; break; + case DISPLAY_PIN_D1: displayD1 = value; break; + case DISPLAY_PIN_D2: displayD2 = value; break; + case DISPLAY_PIN_D3: displayD3 = value; break; + case DISPLAY_PIN_D4: displayD4 = value; break; + case DISPLAY_PIN_D5: displayD5 = value; break; + case DISPLAY_PIN_D6: displayD6 = value; break; + case DISPLAY_PIN_D7: displayD7 = value; break; + case DISPLAY_PIN_RS: displayRs = value; break; + case DISPLAY_PIN_EN: displayEn = value; break; + case DISPLAY_PIN_RW: break; + default: break; + } + break; + case DISPLAY_CONNECTION_GPIO_4BITS: + switch( pinName ) { + case DISPLAY_PIN_D4: displayD4 = value; break; + case DISPLAY_PIN_D5: displayD5 = value; break; + case DISPLAY_PIN_D6: displayD6 = value; break; + case DISPLAY_PIN_D7: displayD7 = value; break; + case DISPLAY_PIN_RS: displayRs = value; break; + case DISPLAY_PIN_EN: displayEn = value; break; + case DISPLAY_PIN_RW: break; + default: break; + } + break; + } +} + +static void displayDataBusWrite( uint8_t dataBus ) +{ + displayPinWrite( DISPLAY_PIN_EN, OFF ); + displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b10000000 ); + displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b01000000 ); + displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00100000 ); + displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00010000 ); + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + displayPinWrite( DISPLAY_PIN_D3, dataBus & 0b00001000 ); + displayPinWrite( DISPLAY_PIN_D2, dataBus & 0b00000100 ); + displayPinWrite( DISPLAY_PIN_D1, dataBus & 0b00000010 ); + displayPinWrite( DISPLAY_PIN_D0, dataBus & 0b00000001 ); + break; + + case DISPLAY_CONNECTION_GPIO_4BITS: + if ( initial8BitCommunicationIsCompleted == true) { + displayPinWrite( DISPLAY_PIN_EN, ON ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_EN, OFF ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b00001000 ); + displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b00000100 ); + displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00000010 ); + displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00000001 ); + } + break; + + } + displayPinWrite( DISPLAY_PIN_EN, ON ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_EN, OFF ); + delay( 1 ); +} +\ No newline at end of file diff --git a/modules/display/display.h b/modules/display/display.h @@ -0,0 +1,29 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _DISPLAY_H_ +#define _DISPLAY_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +typedef enum { + DISPLAY_CONNECTION_GPIO_4BITS, + DISPLAY_CONNECTION_GPIO_8BITS, +} displayConnection_t; + +typedef struct { + displayConnection_t connection; +} display_t; + +//=====[Declarations (prototypes) of public functions]========================= + +void displayInit( displayConnection_t connection ); + +void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY ); + +void displayStringWrite( const char * str ); + +//=====[#include guards - end]================================================= + +#endif // _DISPLAY_H_ +\ No newline at end of file diff --git a/modules/event_log/event_log.cpp b/modules/event_log/event_log.cpp @@ -0,0 +1,117 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "event_log.h" + +#include "siren.h" +#include "fire_alarm.h" +#include "user_interface.h" +#include "date_and_time.h" +#include "pc_serial_com.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +typedef struct systemEvent { + time_t seconds; + char typeOfEvent[EVENT_LOG_NAME_MAX_LENGTH]; +} systemEvent_t; + +//=====[Declaration and initialization of public global objects]=============== + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static bool sirenLastState = OFF; +static bool gasLastState = OFF; +static bool tempLastState = OFF; +static bool ICLastState = OFF; +static bool SBLastState = OFF; +static int eventsIndex = 0; +static systemEvent_t arrayOfStoredEvents[EVENT_LOG_MAX_STORAGE]; + +//=====[Declarations (prototypes) of private functions]======================== + +static void eventLogElementStateUpdate( bool lastState, + bool currentState, + const char* elementName ); + +//=====[Implementations of public functions]=================================== + +void eventLogUpdate() +{ + bool currentState = sirenStateRead(); + eventLogElementStateUpdate( sirenLastState, currentState, "ALARM" ); + sirenLastState = currentState; + + currentState = gasDetectorStateRead(); + eventLogElementStateUpdate( gasLastState, currentState, "GAS_DET" ); + gasLastState = currentState; + + currentState = overTemperatureDetectorStateRead(); + eventLogElementStateUpdate( tempLastState, currentState, "OVER_TEMP" ); + tempLastState = currentState; + + currentState = incorrectCodeStateRead(); + eventLogElementStateUpdate( ICLastState, currentState, "LED_IC" ); + ICLastState = currentState; + + currentState = systemBlockedStateRead(); + eventLogElementStateUpdate( SBLastState ,currentState, "LED_SB" ); + SBLastState = currentState; +} + +int eventLogNumberOfStoredEvents() +{ + return eventsIndex; +} + +void eventLogRead( int index, char* str ) +{ + str[0] = '\0'; + strcat( str, "Event = " ); + strcat( str, arrayOfStoredEvents[index].typeOfEvent ); + strcat( str, "\r\nDate and Time = " ); + strcat( str, ctime(&arrayOfStoredEvents[index].seconds) ); + strcat( str, "\r\n" ); +} + +void eventLogWrite( bool currentState, const char* elementName ) +{ + char eventAndStateStr[EVENT_LOG_NAME_MAX_LENGTH] = ""; + + strcat( eventAndStateStr, elementName ); + if ( currentState ) { + strcat( eventAndStateStr, "_ON" ); + } else { + strcat( eventAndStateStr, "_OFF" ); + } + + arrayOfStoredEvents[eventsIndex].seconds = time(NULL); + strcpy( arrayOfStoredEvents[eventsIndex].typeOfEvent, eventAndStateStr ); + if ( eventsIndex < EVENT_LOG_MAX_STORAGE -1 ) { + eventsIndex++; + } else { + eventsIndex = 0; + } + + pcSerialComStringWrite(eventAndStateStr); + pcSerialComStringWrite("\r\n"); +} + +//=====[Implementations of private functions]================================== + +static void eventLogElementStateUpdate( bool lastState, + bool currentState, + const char* elementName ) +{ + if ( lastState != currentState ) { + eventLogWrite( currentState, elementName ); + } +} diff --git a/modules/event_log/event_log.h b/modules/event_log/event_log.h @@ -0,0 +1,31 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _EVENT_LOG_H_ +#define _EVENT_LOG_H_ + +//=====[Declaration of public defines]========================================= + +#define EVENT_LOG_MAX_STORAGE 20 +#define EVENT_HEAD_STR_LENGTH 8 +#define EVENT_LOG_NAME_MAX_LENGTH 13 +#define DATE_AND_TIME_STR_LENGTH 18 +#define CTIME_STR_LENGTH 25 +#define NEW_LINE_STR_LENGTH 3 +#define EVENT_STR_LENGTH (EVENT_HEAD_STR_LENGTH + \ + EVENT_LOG_NAME_MAX_LENGTH + \ + DATE_AND_TIME_STR_LENGTH + \ + CTIME_STR_LENGTH + \ + NEW_LINE_STR_LENGTH) + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void eventLogUpdate(); +int eventLogNumberOfStoredEvents(); +void eventLogRead( int index, char* str ); +void eventLogWrite( bool currentState, const char* elementName ); + +//=====[#include guards - end]================================================= + +#endif // _EVENT_LOG_H_ diff --git a/modules/fire_alarm/fire_alarm.cpp b/modules/fire_alarm/fire_alarm.cpp @@ -0,0 +1,149 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "fire_alarm.h" + +#include "siren.h" +#include "strobe_light.h" +#include "user_interface.h" +#include "code.h" +#include "date_and_time.h" +#include "temperature_sensor.h" +#include "gas_sensor.h" +#include "matrix_keypad.h" + +//=====[Declaration of private defines]======================================== + +#define TEMPERATURE_C_LIMIT_ALARM 50.0 +#define STROBE_TIME_GAS 1000 +#define STROBE_TIME_OVER_TEMP 500 +#define STROBE_TIME_GAS_AND_OVER_TEMP 100 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalIn alarmTestButton(BUTTON1); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static bool gasDetected = OFF; +static bool overTemperatureDetected = OFF; +static bool gasDetectorState = OFF; +static bool overTemperatureDetectorState = OFF; + +//=====[Declarations (prototypes) of private functions]======================== + +static void fireAlarmActivationUpdate(); +static void fireAlarmDeactivationUpdate(); +static void fireAlarmDeactivate(); +static int fireAlarmStrobeTime(); + +//=====[Implementations of public functions]=================================== + +void fireAlarmInit() +{ + temperatureSensorInit(); + gasSensorInit(); + sirenInit(); + strobeLightInit(); + + alarmTestButton.mode(PullDown); +} + +void fireAlarmUpdate() +{ + fireAlarmActivationUpdate(); + fireAlarmDeactivationUpdate(); + sirenUpdate( fireAlarmStrobeTime() ); + strobeLightUpdate( fireAlarmStrobeTime() ); +} + +bool gasDetectorStateRead() +{ + return gasDetectorState; +} + +bool overTemperatureDetectorStateRead() +{ + return overTemperatureDetectorState; +} + +bool gasDetectedRead() +{ + return gasDetected; +} + +bool overTemperatureDetectedRead() +{ + return overTemperatureDetected; +} + +//=====[Implementations of private functions]================================== + +static void fireAlarmActivationUpdate() +{ + temperatureSensorUpdate(); + gasSensorUpdate(); + + overTemperatureDetectorState = temperatureSensorReadCelsius() > + TEMPERATURE_C_LIMIT_ALARM; + + if ( overTemperatureDetectorState ) { + overTemperatureDetected = ON; + sirenStateWrite(ON); + strobeLightStateWrite(ON); + } + + gasDetectorState = !gasSensorRead(); + + if ( gasDetectorState ) { + gasDetected = ON; + sirenStateWrite(ON); + strobeLightStateWrite(ON); + } + + if( alarmTestButton ) { + overTemperatureDetected = ON; + gasDetected = ON; + sirenStateWrite(ON); + strobeLightStateWrite(ON); + } +} + +static void fireAlarmDeactivationUpdate() +{ + if ( sirenStateRead() ) { + if ( codeMatchFrom(CODE_KEYPAD) || + codeMatchFrom(CODE_PC_SERIAL) ) { + fireAlarmDeactivate(); + } + } +} + +static void fireAlarmDeactivate() +{ + sirenStateWrite(OFF); + strobeLightStateWrite(OFF); + overTemperatureDetected = OFF; + gasDetected = OFF; +} + +static int fireAlarmStrobeTime() +{ + if( gasDetectedRead() && overTemperatureDetectedRead() ) { + return STROBE_TIME_GAS_AND_OVER_TEMP; + } else if ( gasDetectedRead() ) { + return STROBE_TIME_GAS; + } else if ( overTemperatureDetectedRead() ) { + return STROBE_TIME_OVER_TEMP; + } else { + return 0; + } +} diff --git a/modules/fire_alarm/fire_alarm.h b/modules/fire_alarm/fire_alarm.h @@ -0,0 +1,21 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _FIRE_ALARM_H_ +#define _FIRE_ALARM_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void fireAlarmInit(); +void fireAlarmUpdate(); +bool gasDetectorStateRead(); +bool overTemperatureDetectorStateRead(); +bool gasDetectedRead(); +bool overTemperatureDetectedRead(); + +//=====[#include guards - end]================================================= + +#endif // _FIRE_ALARM_H_ +\ No newline at end of file diff --git a/modules/gas_sensor/gas_sensor.cpp b/modules/gas_sensor/gas_sensor.cpp @@ -0,0 +1,38 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" + +#include "gas_sensor.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalIn mq2(PE_12); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +void gasSensorInit() +{ +} + +void gasSensorUpdate() +{ +} + +bool gasSensorRead() +{ + return mq2; +} + +//=====[Implementations of private functions]================================== diff --git a/modules/gas_sensor/gas_sensor.h b/modules/gas_sensor/gas_sensor.h @@ -0,0 +1,18 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _GAS_SENSOR_H_ +#define _GAS_SENSOR_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void gasSensorInit(); +void gasSensorUpdate(); +bool gasSensorRead(); + +//=====[#include guards - end]================================================= + +#endif // _GAS_SENSOR_H_ diff --git a/modules/matrix_keypad/matrix_keypad.cpp b/modules/matrix_keypad/matrix_keypad.cpp @@ -0,0 +1,141 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "matrix_keypad.h" + +#include "date_and_time.h" + +//=====[Declaration of private defines]======================================== + +#define MATRIX_KEYPAD_NUMBER_OF_ROWS 4 +#define MATRIX_KEYPAD_NUMBER_OF_COLS 4 +#define DEBOUNCE_KEY_TIME_MS 40 + +//=====[Declaration of private data types]===================================== + +typedef enum { + MATRIX_KEYPAD_SCANNING, + MATRIX_KEYPAD_DEBOUNCE, + MATRIX_KEYPAD_KEY_HOLD_PRESSED +} matrixKeypadState_t; + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut keypadRowPins[MATRIX_KEYPAD_NUMBER_OF_ROWS] = {PB_3, PB_5, PC_7, PA_15}; +DigitalIn keypadColPins[MATRIX_KEYPAD_NUMBER_OF_COLS] = {PB_12, PB_13, PB_15, PC_6}; + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static matrixKeypadState_t matrixKeypadState; +static int timeIncrement_ms = 0; + +//=====[Declarations (prototypes) of private functions]======================== + +static char matrixKeypadScan(); +static void matrixKeypadReset(); + +//=====[Implementations of public functions]=================================== + +void matrixKeypadInit( int updateTime_ms ) +{ + timeIncrement_ms = updateTime_ms; + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + int pinIndex = 0; + for( pinIndex=0; pinIndex<MATRIX_KEYPAD_NUMBER_OF_COLS; pinIndex++ ) { + (keypadColPins[pinIndex]).mode(PullUp); + } +} + +char matrixKeypadUpdate() +{ + static int accumulatedDebounceMatrixKeypadTime = 0; + static char matrixKeypadLastKeyPressed = '\0'; + + char keyDetected = '\0'; + char keyReleased = '\0'; + + switch( matrixKeypadState ) { + + case MATRIX_KEYPAD_SCANNING: + keyDetected = matrixKeypadScan(); + if( keyDetected != '\0' ) { + matrixKeypadLastKeyPressed = keyDetected; + accumulatedDebounceMatrixKeypadTime = 0; + matrixKeypadState = MATRIX_KEYPAD_DEBOUNCE; + } + break; + + case MATRIX_KEYPAD_DEBOUNCE: + if( accumulatedDebounceMatrixKeypadTime >= + DEBOUNCE_KEY_TIME_MS ) { + keyDetected = matrixKeypadScan(); + if( keyDetected == matrixKeypadLastKeyPressed ) { + matrixKeypadState = MATRIX_KEYPAD_KEY_HOLD_PRESSED; + } else { + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + } + } + accumulatedDebounceMatrixKeypadTime = + accumulatedDebounceMatrixKeypadTime + timeIncrement_ms; + break; + + case MATRIX_KEYPAD_KEY_HOLD_PRESSED: + keyDetected = matrixKeypadScan(); + if( keyDetected != matrixKeypadLastKeyPressed ) { + if( keyDetected == '\0' ) { + keyReleased = matrixKeypadLastKeyPressed; + } + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + } + break; + + default: + matrixKeypadReset(); + break; + } + return keyReleased; +} + +//=====[Implementations of private functions]================================== + +static char matrixKeypadScan() +{ + int row = 0; + int col = 0; + int i = 0; + + char matrixKeypadIndexToCharArray[] = { + '1', '2', '3', 'A', + '4', '5', '6', 'B', + '7', '8', '9', 'C', + '*', '0', '#', 'D', + }; + + for( row=0; row<MATRIX_KEYPAD_NUMBER_OF_ROWS; row++ ) { + + for( i=0; i<MATRIX_KEYPAD_NUMBER_OF_ROWS; i++ ) { + keypadRowPins[i] = ON; + } + + keypadRowPins[row] = OFF; + + for( col=0; col<MATRIX_KEYPAD_NUMBER_OF_COLS; col++ ) { + if( keypadColPins[col] == OFF ) { + return matrixKeypadIndexToCharArray[ + row*MATRIX_KEYPAD_NUMBER_OF_ROWS + col]; + } + } + } + return '\0'; +} + +static void matrixKeypadReset() +{ + matrixKeypadState = MATRIX_KEYPAD_SCANNING; +} diff --git a/modules/matrix_keypad/matrix_keypad.h b/modules/matrix_keypad/matrix_keypad.h @@ -0,0 +1,17 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _MATRIX_KEYPAD_H_ +#define _MATRIX_KEYPAD_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void matrixKeypadInit( int updateTime_ms ); +char matrixKeypadUpdate(); + +//=====[#include guards - end]================================================= + +#endif // _MATRIX_KEYPAD_H_ +\ No newline at end of file diff --git a/modules/pc_serial_com/pc_serial_com.cpp b/modules/pc_serial_com/pc_serial_com.cpp @@ -0,0 +1,310 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "pc_serial_com.h" + +#include "siren.h" +#include "fire_alarm.h" +#include "code.h" +#include "date_and_time.h" +#include "temperature_sensor.h" +#include "gas_sensor.h" +#include "event_log.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +typedef enum{ + PC_SERIAL_COMMANDS, + PC_SERIAL_GET_CODE, + PC_SERIAL_SAVE_NEW_CODE, +} pcSerialComMode_t; + +//=====[Declaration and initialization of public global objects]=============== + +UnbufferedSerial uartUsb(USBTX, USBRX, 115200); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +char codeSequenceFromPcSerialCom[CODE_NUMBER_OF_KEYS]; + +//=====[Declaration and initialization of private global variables]============ + +static pcSerialComMode_t pcSerialComMode = PC_SERIAL_COMMANDS; +static bool codeComplete = false; +static int numberOfCodeChars = 0; + +//=====[Declarations (prototypes) of private functions]======================== + +static void pcSerialComStringRead( char* str, int strLength ); + +static void pcSerialComGetCodeUpdate( char receivedChar ); +static void pcSerialComSaveNewCodeUpdate( char receivedChar ); + +static void pcSerialComCommandUpdate( char receivedChar ); + +static void availableCommands(); +static void commandShowCurrentAlarmState(); +static void commandShowCurrentGasDetectorState(); +static void commandShowCurrentOverTemperatureDetectorState(); +static void commandEnterCodeSequence(); +static void commandEnterNewCode(); +static void commandShowCurrentTemperatureInCelsius(); +static void commandShowCurrentTemperatureInFahrenheit(); +static void commandSetDateAndTime(); +static void commandShowDateAndTime(); +static void commandShowStoredEvents(); + +//=====[Implementations of public functions]=================================== + +void pcSerialComInit() +{ + availableCommands(); +} + +char pcSerialComCharRead() +{ + char receivedChar = '\0'; + if( uartUsb.readable() ) { + uartUsb.read( &receivedChar, 1 ); + } + return receivedChar; +} + +void pcSerialComStringWrite( const char* str ) +{ + uartUsb.write( str, strlen(str) ); +} + +void pcSerialComUpdate() +{ + char receivedChar = pcSerialComCharRead(); + if( receivedChar != '\0' ) { + switch ( pcSerialComMode ) { + case PC_SERIAL_COMMANDS: + pcSerialComCommandUpdate( receivedChar ); + break; + + case PC_SERIAL_GET_CODE: + pcSerialComGetCodeUpdate( receivedChar ); + break; + + case PC_SERIAL_SAVE_NEW_CODE: + pcSerialComSaveNewCodeUpdate( receivedChar ); + break; + default: + pcSerialComMode = PC_SERIAL_COMMANDS; + break; + } + } +} + +bool pcSerialComCodeCompleteRead() +{ + return codeComplete; +} + +void pcSerialComCodeCompleteWrite( bool state ) +{ + codeComplete = state; +} + +//=====[Implementations of private functions]================================== + +static void pcSerialComStringRead( char* str, int strLength ) +{ + int strIndex; + for ( strIndex = 0; strIndex < strLength; strIndex++) { + uartUsb.read( &str[strIndex] , 1 ); + uartUsb.write( &str[strIndex] ,1 ); + } + str[strLength]='\0'; +} + +static void pcSerialComGetCodeUpdate( char receivedChar ) +{ + codeSequenceFromPcSerialCom[numberOfCodeChars] = receivedChar; + pcSerialComStringWrite( "*" ); + numberOfCodeChars++; + if ( numberOfCodeChars >= CODE_NUMBER_OF_KEYS ) { + pcSerialComMode = PC_SERIAL_COMMANDS; + codeComplete = true; + numberOfCodeChars = 0; + } +} + +static void pcSerialComSaveNewCodeUpdate( char receivedChar ) +{ + static char newCodeSequence[CODE_NUMBER_OF_KEYS]; + + newCodeSequence[numberOfCodeChars] = receivedChar; + pcSerialComStringWrite( "*" ); + numberOfCodeChars++; + if ( numberOfCodeChars >= CODE_NUMBER_OF_KEYS ) { + pcSerialComMode = PC_SERIAL_COMMANDS; + numberOfCodeChars = 0; + codeWrite( newCodeSequence ); + pcSerialComStringWrite( "\r\nNew code configured\r\n\r\n" ); + } +} + +static void pcSerialComCommandUpdate( char receivedChar ) +{ + switch (receivedChar) { + case '1': commandShowCurrentAlarmState(); break; + case '2': commandShowCurrentGasDetectorState(); break; + case '3': commandShowCurrentOverTemperatureDetectorState(); break; + case '4': commandEnterCodeSequence(); break; + case '5': commandEnterNewCode(); break; + case 'c': case 'C': commandShowCurrentTemperatureInCelsius(); break; + case 'f': case 'F': commandShowCurrentTemperatureInFahrenheit(); break; + case 's': case 'S': commandSetDateAndTime(); break; + case 't': case 'T': commandShowDateAndTime(); break; + case 'e': case 'E': commandShowStoredEvents(); break; + default: availableCommands(); break; + } +} + +static void availableCommands() +{ + pcSerialComStringWrite( "Available commands:\r\n" ); + pcSerialComStringWrite( "Press '1' to get the alarm state\r\n" ); + pcSerialComStringWrite( "Press '2' to get the gas detector state\r\n" ); + pcSerialComStringWrite( "Press '3' to get the over temperature detector state\r\n" ); + pcSerialComStringWrite( "Press '4' to enter the code to deactivate the alarm\r\n" ); + pcSerialComStringWrite( "Press '5' to enter a new code to deactivate the alarm\r\n" ); + pcSerialComStringWrite( "Press 'f' or 'F' to get lm35 reading in Fahrenheit\r\n" ); + pcSerialComStringWrite( "Press 'c' or 'C' to get lm35 reading in Celsius\r\n" ); + pcSerialComStringWrite( "Press 's' or 'S' to set the date and time\r\n" ); + pcSerialComStringWrite( "Press 't' or 'T' to get the date and time\r\n" ); + pcSerialComStringWrite( "Press 'e' or 'E' to get the stored events\r\n" ); + pcSerialComStringWrite( "\r\n" ); +} + +static void commandShowCurrentAlarmState() +{ + if ( sirenStateRead() ) { + pcSerialComStringWrite( "The alarm is activated\r\n"); + } else { + pcSerialComStringWrite( "The alarm is not activated\r\n"); + } +} + +static void commandShowCurrentGasDetectorState() +{ + if ( gasDetectorStateRead() ) { + pcSerialComStringWrite( "Gas is being detected\r\n"); + } else { + pcSerialComStringWrite( "Gas is not being detected\r\n"); + } +} + +static void commandShowCurrentOverTemperatureDetectorState() +{ + if ( overTemperatureDetectorStateRead() ) { + pcSerialComStringWrite( "Temperature is above the maximum level\r\n"); + } else { + pcSerialComStringWrite( "Temperature is below the maximum level\r\n"); + } +} + +static void commandEnterCodeSequence() +{ + if( sirenStateRead() ) { + pcSerialComStringWrite( "Please enter the four digits numeric code " ); + pcSerialComStringWrite( "to deactivate the alarm: " ); + pcSerialComMode = PC_SERIAL_GET_CODE; + codeComplete = false; + numberOfCodeChars = 0; + } else { + pcSerialComStringWrite( "Alarm is not activated.\r\n" ); + } +} + +static void commandEnterNewCode() +{ + pcSerialComStringWrite( "Please enter the new four digits numeric code " ); + pcSerialComStringWrite( "to deactivate the alarm: " ); + numberOfCodeChars = 0; + pcSerialComMode = PC_SERIAL_SAVE_NEW_CODE; + +} + +static void commandShowCurrentTemperatureInCelsius() +{ + char str[100] = ""; + sprintf ( str, "Temperature: %.2f \xB0 C\r\n", + temperatureSensorReadCelsius() ); + pcSerialComStringWrite( str ); +} + +static void commandShowCurrentTemperatureInFahrenheit() +{ + char str[100] = ""; + sprintf ( str, "Temperature: %.2f \xB0 C\r\n", + temperatureSensorReadFahrenheit() ); + pcSerialComStringWrite( str ); +} + +static void commandSetDateAndTime() +{ + char year[5] = ""; + char month[3] = ""; + char day[3] = ""; + char hour[3] = ""; + char minute[3] = ""; + char second[3] = ""; + + pcSerialComStringWrite("\r\nType four digits for the current year (YYYY): "); + pcSerialComStringRead( year, 4); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Type two digits for the current month (01-12): "); + pcSerialComStringRead( month, 2); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Type two digits for the current day (01-31): "); + pcSerialComStringRead( day, 2); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Type two digits for the current hour (00-23): "); + pcSerialComStringRead( hour, 2); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Type two digits for the current minutes (00-59): "); + pcSerialComStringRead( minute, 2); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Type two digits for the current seconds (00-59): "); + pcSerialComStringRead( second, 2); + pcSerialComStringWrite("\r\n"); + + pcSerialComStringWrite("Date and time has been set\r\n"); + + dateAndTimeWrite( atoi(year), atoi(month), atoi(day), + atoi(hour), atoi(minute), atoi(second) ); +} + +static void commandShowDateAndTime() +{ + char str[100] = ""; + sprintf ( str, "Date and Time = %s", dateAndTimeRead() ); + pcSerialComStringWrite( str ); + pcSerialComStringWrite("\r\n"); +} + +static void commandShowStoredEvents() +{ + char str[EVENT_STR_LENGTH] = ""; + int i; + for (i = 0; i < eventLogNumberOfStoredEvents(); i++) { + eventLogRead( i, str ); + pcSerialComStringWrite( str ); + pcSerialComStringWrite( "\r\n" ); + } +} +\ No newline at end of file diff --git a/modules/pc_serial_com/pc_serial_com.h b/modules/pc_serial_com/pc_serial_com.h @@ -0,0 +1,21 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _PC_SERIAL_COM_H_ +#define _PC_SERIAL_COM_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void pcSerialComInit(); +char pcSerialComCharRead(); +void pcSerialComStringWrite( const char* str ); +void pcSerialComUpdate(); +bool pcSerialComCodeCompleteRead(); +void pcSerialComCodeCompleteWrite( bool state ); + +//=====[#include guards - end]================================================= + +#endif // _PC_SERIAL_COM_H_ +\ No newline at end of file diff --git a/modules/siren/siren.cpp b/modules/siren/siren.cpp @@ -0,0 +1,61 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "siren.h" + +#include "smart_home_system.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut sirenPin(PE_10); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static bool sirenState = OFF; + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +void sirenInit() +{ + sirenPin = ON; +} + +bool sirenStateRead() +{ + return sirenState; +} + +void sirenStateWrite( bool state ) +{ + sirenState = state; +} + +void sirenUpdate( int strobeTime ) +{ + static int accumulatedTimeAlarm = 0; + accumulatedTimeAlarm = accumulatedTimeAlarm + SYSTEM_TIME_INCREMENT_MS; + + if( sirenState ) { + if( accumulatedTimeAlarm >= strobeTime ) { + accumulatedTimeAlarm = 0; + sirenPin= !sirenPin; + } + } else { + sirenPin = ON; + } +} + +//=====[Implementations of private functions]================================== + diff --git a/modules/siren/siren.h b/modules/siren/siren.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _SIREN_H_ +#define _SIREN_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void sirenInit(); +bool sirenStateRead(); +void sirenStateWrite( bool state ); +void sirenUpdate( int strobeTime ); + +//=====[#include guards - end]================================================= + +#endif // _SIREN_H_ diff --git a/modules/smart_home_system/smart_home_system.cpp b/modules/smart_home_system/smart_home_system.cpp @@ -0,0 +1,45 @@ +//=====[Libraries]============================================================= + +#include "arm_book_lib.h" + +#include "smart_home_system.h" + +#include "siren.h" +#include "user_interface.h" +#include "fire_alarm.h" +#include "pc_serial_com.h" +#include "event_log.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +void smartHomeSystemInit() +{ + userInterfaceInit(); + fireAlarmInit(); + pcSerialComInit(); +} + +void smartHomeSystemUpdate() +{ + userInterfaceUpdate(); + fireAlarmUpdate(); + pcSerialComUpdate(); + eventLogUpdate(); + delay(SYSTEM_TIME_INCREMENT_MS); +} + +//=====[Implementations of private functions]================================== diff --git a/modules/smart_home_system/smart_home_system.h b/modules/smart_home_system/smart_home_system.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _SMART_HOME_SYSTEM_H_ +#define _SMART_HOME_SYSTEM_H_ + +//=====[Declaration of public defines]========================================= + +#define SYSTEM_TIME_INCREMENT_MS 10 + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void smartHomeSystemInit(); +void smartHomeSystemUpdate(); + +//=====[#include guards - end]================================================= + +#endif // _SMART_HOME_SYSTEM_H_ +\ No newline at end of file diff --git a/modules/strobe_light/strobe_light.cpp b/modules/strobe_light/strobe_light.cpp @@ -0,0 +1,60 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "strobe_light.h" +#include "smart_home_system.h" + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut strobeLight(LED1); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static bool strobeLightState = OFF; + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +void strobeLightInit() +{ + strobeLight = OFF; +} + +bool strobeLightStateRead() +{ + return strobeLightState; +} + +void strobeLightStateWrite( bool state ) +{ + strobeLightState = state; +} + +void strobeLightUpdate( int strobeTime ) +{ + static int accumulatedTimeAlarm = 0; + accumulatedTimeAlarm = accumulatedTimeAlarm + SYSTEM_TIME_INCREMENT_MS; + + if( strobeLightState ) { + if( accumulatedTimeAlarm >= strobeTime ) { + accumulatedTimeAlarm = 0; + strobeLight= !strobeLight; + } + } else { + strobeLight = OFF; + } +} + +//=====[Implementations of private functions]================================== + diff --git a/modules/strobe_light/strobe_light.h b/modules/strobe_light/strobe_light.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _STROBE_LIGHT_H_ +#define _STROBE_LIGHT_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void strobeLightInit(); +bool strobeLightStateRead(); +void strobeLightStateWrite( bool state ); +void strobeLightUpdate( int strobeTime ); + +//=====[#include guards - end]================================================= + +#endif // _STROBE_LIGHT_H_ +\ No newline at end of file diff --git a/modules/temperature_sensor/temperature_sensor.cpp b/modules/temperature_sensor/temperature_sensor.cpp @@ -0,0 +1,86 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" + +#include "temperature_sensor.h" + +#include "smart_home_system.h" + +//=====[Declaration of private defines]======================================== + +#define LM35_NUMBER_OF_AVG_SAMPLES 10 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +AnalogIn lm35(A1); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +float lm35TemperatureC = 0.0; +float lm35ReadingsArray[LM35_NUMBER_OF_AVG_SAMPLES]; + +//=====[Declarations (prototypes) of private functions]======================== + +static float analogReadingScaledWithTheLM35Formula( float analogReading ); + +//=====[Implementations of public functions]=================================== + +void temperatureSensorInit() +{ + int i; + + for( i=0; i<LM35_NUMBER_OF_AVG_SAMPLES ; i++ ) { + lm35ReadingsArray[i] = 0; + } +} + +void temperatureSensorUpdate() +{ + static int lm35SampleIndex = 0; + float lm35ReadingsSum = 0.0; + float lm35ReadingsAverage = 0.0; + + int i = 0; + + lm35ReadingsArray[lm35SampleIndex] = lm35.read(); + lm35SampleIndex++; + if ( lm35SampleIndex >= LM35_NUMBER_OF_AVG_SAMPLES) { + lm35SampleIndex = 0; + } + + lm35ReadingsSum = 0.0; + for (i = 0; i < LM35_NUMBER_OF_AVG_SAMPLES; i++) { + lm35ReadingsSum = lm35ReadingsSum + lm35ReadingsArray[i]; + } + lm35ReadingsAverage = lm35ReadingsSum / LM35_NUMBER_OF_AVG_SAMPLES; + lm35TemperatureC = analogReadingScaledWithTheLM35Formula ( lm35ReadingsAverage ); +} + + +float temperatureSensorReadCelsius() +{ + return lm35TemperatureC; +} + +float temperatureSensorReadFahrenheit() +{ + return celsiusToFahrenheit( lm35TemperatureC ); +} + +float celsiusToFahrenheit( float tempInCelsiusDegrees ) +{ + return ( tempInCelsiusDegrees * 9.0 / 5.0 + 32.0 ); +} + +//=====[Implementations of private functions]================================== + +static float analogReadingScaledWithTheLM35Formula( float analogReading ) +{ + return ( analogReading * 3.3 / 0.01 ); +} diff --git a/modules/temperature_sensor/temperature_sensor.h b/modules/temperature_sensor/temperature_sensor.h @@ -0,0 +1,20 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _TEMPERATURE_SENSOR_H_ +#define _TEMPERATURE_SENSOR_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void temperatureSensorInit(); +void temperatureSensorUpdate(); +float temperatureSensorReadCelsius(); +float temperatureSensorReadFahrenheit(); +float celsiusToFahrenheit( float tempInCelsiusDegrees ); + +//=====[#include guards - end]================================================= + +#endif // _TEMPERATURE_SENSOR_H_ +\ No newline at end of file diff --git a/modules/user_interface/user_interface.cpp b/modules/user_interface/user_interface.cpp @@ -0,0 +1,192 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "user_interface.h" + +#include "code.h" +#include "siren.h" +#include "smart_home_system.h" +#include "fire_alarm.h" +#include "date_and_time.h" +#include "temperature_sensor.h" +#include "gas_sensor.h" +#include "matrix_keypad.h" +#include "display.h" + +//=====[Declaration of private defines]======================================== + +#define DISPLAY_REFRESH_TIME_MS 1000 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut incorrectCodeLed(LED3); +DigitalOut systemBlockedLed(LED2); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +char codeSequenceFromUserInterface[CODE_NUMBER_OF_KEYS]; + +//=====[Declaration and initialization of private global variables]============ + +static bool incorrectCodeState = OFF; +static bool systemBlockedState = OFF; + +static bool codeComplete = false; +static int numberOfCodeChars = 0; + +//=====[Declarations (prototypes) of private functions]======================== + +static void userInterfaceMatrixKeypadUpdate(); +static void incorrectCodeIndicatorUpdate(); +static void systemBlockedIndicatorUpdate(); + +static void userInterfaceDisplayInit(); +static void userInterfaceDisplayUpdate(); + +//=====[Implementations of public functions]=================================== + +void userInterfaceInit() +{ + incorrectCodeLed = OFF; + systemBlockedLed = OFF; + matrixKeypadInit( SYSTEM_TIME_INCREMENT_MS ); + userInterfaceDisplayInit(); +} + +void userInterfaceUpdate() +{ + userInterfaceMatrixKeypadUpdate(); + incorrectCodeIndicatorUpdate(); + systemBlockedIndicatorUpdate(); + userInterfaceDisplayUpdate(); +} + +bool incorrectCodeStateRead() +{ + return incorrectCodeState; +} + +void incorrectCodeStateWrite( bool state ) +{ + incorrectCodeState = state; +} + +bool systemBlockedStateRead() +{ + return systemBlockedState; +} + +void systemBlockedStateWrite( bool state ) +{ + systemBlockedState = state; +} + +bool userInterfaceCodeCompleteRead() +{ + return codeComplete; +} + +void userInterfaceCodeCompleteWrite( bool state ) +{ + codeComplete = state; +} + +//=====[Implementations of private functions]================================== + +static void userInterfaceMatrixKeypadUpdate() +{ + static int numberOfHashKeyReleased = 0; + char keyReleased = matrixKeypadUpdate(); + + if( keyReleased != '\0' ) { + + if( sirenStateRead() && !systemBlockedStateRead() ) { + if( !incorrectCodeStateRead() ) { + codeSequenceFromUserInterface[numberOfCodeChars] = keyReleased; + numberOfCodeChars++; + if ( numberOfCodeChars >= CODE_NUMBER_OF_KEYS ) { + codeComplete = true; + numberOfCodeChars = 0; + } + } else { + if( keyReleased == '#' ) { + numberOfHashKeyReleased++; + if( numberOfHashKeyReleased >= 2 ) { + numberOfHashKeyReleased = 0; + numberOfCodeChars = 0; + codeComplete = false; + incorrectCodeState = OFF; + } + } + } + } + } +} + +static void userInterfaceDisplayInit() +{ + displayInit( DISPLAY_CONNECTION_GPIO_4BITS ); + + displayCharPositionWrite ( 0,0 ); + displayStringWrite( "Temperature:" ); + + displayCharPositionWrite ( 0,1 ); + displayStringWrite( "Gas:" ); + + displayCharPositionWrite ( 0,2 ); + displayStringWrite( "Alarm:" ); +} + +static void userInterfaceDisplayUpdate() +{ + static int accumulatedDisplayTime = 0; + char temperatureString[3] = ""; + + if( accumulatedDisplayTime >= + DISPLAY_REFRESH_TIME_MS ) { + + accumulatedDisplayTime = 0; + + sprintf(temperatureString, "%.0f", temperatureSensorReadCelsius()); + displayCharPositionWrite ( 12,0 ); + displayStringWrite( temperatureString ); + displayCharPositionWrite ( 14,0 ); + displayStringWrite( "'C" ); + + displayCharPositionWrite ( 4,1 ); + + if ( gasDetectorStateRead() ) { + displayStringWrite( "Detected " ); + } else { + displayStringWrite( "Not Detected" ); + } + + displayCharPositionWrite ( 6,2 ); + + if ( sirenStateRead() ) { + displayStringWrite( "ON " ); + } else { + displayStringWrite( "OFF" ); + } + + } else { + accumulatedDisplayTime = + accumulatedDisplayTime + SYSTEM_TIME_INCREMENT_MS; + } +} + +static void incorrectCodeIndicatorUpdate() +{ + incorrectCodeLed = incorrectCodeStateRead(); +} + +static void systemBlockedIndicatorUpdate() +{ + systemBlockedLed = systemBlockedState; +} +\ No newline at end of file diff --git a/modules/user_interface/user_interface.h b/modules/user_interface/user_interface.h @@ -0,0 +1,25 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _USER_INTERFACE_H_ +#define _USER_INTERFACE_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void userInterfaceInit(); +void userInterfaceUpdate(); +bool userInterfaceCodeCompleteRead(); +void userInterfaceCodeCompleteWrite( bool state ); + +bool incorrectCodeStateRead(); +void incorrectCodeStateWrite( bool state ); + +bool systemBlockedStateRead(); +void systemBlockedStateWrite( bool state ); + +//=====[#include guards - end]================================================= + +#endif // _USER_INTERFACE_H_ +\ No newline at end of file
