Rev 1440 | Rev 1481 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1440 | Rev 1477 | ||
|---|---|---|---|
| Line 1... | Line -... | ||
| 1 | - | ||
| 2 | #include "sns_PIR.h" |
1 | #include "sns_PIR.h" |
| 3 | 2 | ||
| 4 | #ifdef sns_PIR_USEEEPROM |
- | |
| 5 | #include "sns_PIR_eeprom.h" |
- | |
| 6 |
|
3 | #define ABS(a) ((a) < 0 ? (-1*a) : (a)) |
| 7 | { |
4 | |
| 8 | { |
5 | |
| 9 |
|
6 | // a weighted moving avarage value is used to remove "DC offset" |
| 10 | 0xAB, // x |
- | |
| 11 |
|
7 | static uint32_t zeroOffset = 0; |
| 12 | }, |
8 | |
| 13 |
|
9 | // a second avarage value is used to detect changes in avarage "energy" |
| - | 10 | #define NR_AVG_VALUES 15 |
|
| - | 11 | static int8_t avgValues[NR_AVG_VALUES]; |
|
| - | 12 | static uint8_t avgIndex = 0; |
|
| 14 | }; |
13 | |
| 15 | #endif |
- | |
| 16 | 14 | ||
| 17 | void sns_PIR_Init(void) |
15 | void sns_PIR_Init(void) |
| 18 | { |
16 | { |
| 19 | #ifdef sns_PIR_USEEEPROM |
- | |
| 20 | if (EEDATA_OK) |
- | |
| 21 | { |
- | |
| 22 | ///TODO: Use stored data to set initial values for the module |
- | |
| 23 | blablaX = eeprom_read_byte(EEDATA.x); |
- | |
| 24 | blablaY = eeprom_read_word(EEDATA.y); |
- | |
| 25 | } else |
- | |
| 26 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
- | |
| 27 | eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC); |
- | |
| 28 | eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC); |
- | |
| 29 | EEDATA_UPDATE_CRC; |
- | |
| 30 | } |
- | |
| 31 | #endif |
- | |
| 32 | ///TODO: Initialize hardware etc here |
- | |
| 33 | - | ||
| 34 | // to use PCINt lib, call this function: (the callback function look as a timer callback function) |
- | |
| 35 | // Pcint_SetCallbackPin(sns_PIR_PCINT, EXP_C , &sns_PIR_pcint_callback); |
- | |
| 36 | - | ||
| 37 |
|
17 | ltc2450_init(); |
| 38 | Timer_SetTimeout(sns_PIR_BRIGHT_TIMER, |
18 | Timer_SetTimeout(sns_PIR_BRIGHT_TIMER, 40, TimerTypeFreeRunning, 0); |
| 39 | } |
19 | } |
| - | 20 | ||
| 40 | 21 | ||
| 41 | void sns_PIR_Process(void) |
22 | void sns_PIR_Process(void) |
| 42 | { |
23 | { |
| - | 24 | static uint8_t printCounter = 0; |
|
| 43 | if (Timer_Expired(sns_PIR_BRIGHT_TIMER)) { |
25 | if (Timer_Expired(sns_PIR_BRIGHT_TIMER)) { |
| - | 26 | uint16_t pirValue; |
|
| - | 27 | do { |
|
| 44 |
|
28 | // if ADC is busy, returned value is 0xFFFF |
| - | 29 | pirValue = ltc2450_read(); |
|
| - | 30 | } while (pirValue >= 0xFFFF); |
|
| - | 31 | ||
| - | 32 | // first time? |
|
| - | 33 | if (zeroOffset == 0) { |
|
| - | 34 | zeroOffset = pirValue; |
|
| - | 35 | } |
|
| - | 36 | else { |
|
| 45 |
|
37 | zeroOffset = (zeroOffset*4 + (uint32_t)pirValue)/5; |
| - | 38 | } |
|
| - | 39 | ||
| - | 40 | int8_t test = (int8_t)((int32_t)pirValue - (int32_t)zeroOffset); |
|
| - | 41 | avgValues[avgIndex++] = test; |
|
| - | 42 | if (avgIndex >= NR_AVG_VALUES) { |
|
| - | 43 | avgIndex = 0; |
|
| - | 44 | } |
|
| - | 45 | uint16_t avgValue = 0; |
|
| - | 46 | for (uint8_t i=0; i<NR_AVG_VALUES; i++) { |
|
| - | 47 | avgValue += ABS(avgValues[i]); |
|
| - | 48 | } |
|
| - | 49 | uint8_t motionDetect = (avgValue >= 80); |
|
| - | 50 | ||
| - | 51 | ||
| - | 52 | if (printCounter++ >= 200/40) { |
|
| - | 53 | printCounter = 0; |
|
| - | 54 | //printf("AD:%u\n", pirValue); |
|
| - | 55 | printf("Motion: %u\n", motionDetect); |
|
| 46 | 56 | ||
| 47 | StdCan_Msg_t txMsg |
57 | /*StdCan_Msg_t txMsg; |
| 48 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); |
58 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); |
| 49 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
59 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
| 50 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR; |
60 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR; |
| 51 | txMsg.Header.ModuleId = sns_PIR_ID; |
61 | txMsg.Header.ModuleId = sns_PIR_ID; |
| 52 | txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION; |
62 | txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION; |
| 53 | txMsg.Length = 3; |
63 | txMsg.Length = 3; |
| 54 | txMsg.Data[0] = 0; |
64 | txMsg.Data[0] = 0; |
| 55 | txMsg.Data[1] = (brightness>>2)&0xff; |
- | |
| 56 | txMsg.Data[2] = 0; |
- | |
| 57 |
|
65 | txMsg.Data[1] = (uint8_t)((pirValue & 0xFF00) >> 2); |
| 58 |
|
66 | txMsg.Data[2] = (uint8_t)((pirValue & 0x00FF) >> 0); |
| 59 | - | ||
| 60 | StdCan_Put |
67 | StdCan_Put(&txMsg);*/ |
| - | 68 | } |
|
| 61 | } |
69 | } |
| 62 | } |
70 | } |
| 63 | 71 | ||
| 64 | void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg) |
72 | void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg) |
| 65 | { |
73 | { |
| Line 96... | Line 104... | ||
| 96 | txMsg.Data[4] = NUMBER_OF_MODULES; |
104 | txMsg.Data[4] = NUMBER_OF_MODULES; |
| 97 | txMsg.Data[5] = ModuleSequenceNumber; |
105 | txMsg.Data[5] = ModuleSequenceNumber; |
| 98 | 106 | ||
| 99 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
107 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 100 | } |
108 | } |
| - | 109 | ||