![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SEDO subject project
Dependencies: ds3231 mbed-rtos mbed DHT
light_sensor.cpp@12:1d544cdab2cf, 2017-05-15 (annotated)
- Committer:
- ValenSalLop
- Date:
- Mon May 15 18:30:20 2017 +0000
- Revision:
- 12:1d544cdab2cf
final;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ValenSalLop | 12:1d544cdab2cf | 1 | /* |
ValenSalLop | 12:1d544cdab2cf | 2 | * Light sensor operations |
ValenSalLop | 12:1d544cdab2cf | 3 | */ |
ValenSalLop | 12:1d544cdab2cf | 4 | #include "mbed.h" |
ValenSalLop | 12:1d544cdab2cf | 5 | #include "rtos.h" |
ValenSalLop | 12:1d544cdab2cf | 6 | #include "main.h" |
ValenSalLop | 12:1d544cdab2cf | 7 | #include "light_sensor.h" |
ValenSalLop | 12:1d544cdab2cf | 8 | |
ValenSalLop | 12:1d544cdab2cf | 9 | AnalogIn LDR(A5); |
ValenSalLop | 12:1d544cdab2cf | 10 | |
ValenSalLop | 12:1d544cdab2cf | 11 | uint32_t light_init(void const *args) |
ValenSalLop | 12:1d544cdab2cf | 12 | { |
ValenSalLop | 12:1d544cdab2cf | 13 | return 1; |
ValenSalLop | 12:1d544cdab2cf | 14 | } |
ValenSalLop | 12:1d544cdab2cf | 15 | void light_thread(void const *args) |
ValenSalLop | 12:1d544cdab2cf | 16 | { |
ValenSalLop | 12:1d544cdab2cf | 17 | //DEBUG |
ValenSalLop | 12:1d544cdab2cf | 18 | mutexPCComm.lock(); |
ValenSalLop | 12:1d544cdab2cf | 19 | pc.printf("LDR: thread init\n"); |
ValenSalLop | 12:1d544cdab2cf | 20 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 21 | float lightCurr; |
ValenSalLop | 12:1d544cdab2cf | 22 | int8_t error = 0; |
ValenSalLop | 12:1d544cdab2cf | 23 | int8_t attempts=0; |
ValenSalLop | 12:1d544cdab2cf | 24 | while(true) { |
ValenSalLop | 12:1d544cdab2cf | 25 | Thread::signal_wait(0x1); |
ValenSalLop | 12:1d544cdab2cf | 26 | mutexPCComm.lock(); |
ValenSalLop | 12:1d544cdab2cf | 27 | pc.printf("LDR: loop\n"); |
ValenSalLop | 12:1d544cdab2cf | 28 | mutexPCComm.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 29 | error = -1; |
ValenSalLop | 12:1d544cdab2cf | 30 | attempts=0; |
ValenSalLop | 12:1d544cdab2cf | 31 | while(error!=0 && attempts<MAX_READ_ATTEMPTS_LIGHT && sensors_running == true) { |
ValenSalLop | 12:1d544cdab2cf | 32 | lightCurr=LDR.read(); |
ValenSalLop | 12:1d544cdab2cf | 33 | error = 0; |
ValenSalLop | 12:1d544cdab2cf | 34 | if(error==0) { |
ValenSalLop | 12:1d544cdab2cf | 35 | mutexData.lock(); |
ValenSalLop | 12:1d544cdab2cf | 36 | data.light = lightCurr; |
ValenSalLop | 12:1d544cdab2cf | 37 | data.lightError = 0; |
ValenSalLop | 12:1d544cdab2cf | 38 | mutexData.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 39 | } else { |
ValenSalLop | 12:1d544cdab2cf | 40 | mutexData.lock(); |
ValenSalLop | 12:1d544cdab2cf | 41 | data.lightError = 0; |
ValenSalLop | 12:1d544cdab2cf | 42 | mutexData.unlock(); |
ValenSalLop | 12:1d544cdab2cf | 43 | attempts++; |
ValenSalLop | 12:1d544cdab2cf | 44 | Thread::wait(3000); |
ValenSalLop | 12:1d544cdab2cf | 45 | } |
ValenSalLop | 12:1d544cdab2cf | 46 | } // while read attempts |
ValenSalLop | 12:1d544cdab2cf | 47 | } // main thread while |
ValenSalLop | 12:1d544cdab2cf | 48 | } // thread function |