witekio
Fork of TCS3200 by
TCS3200.cpp@1:e1c8062f2970, 2018-01-11 (annotated)
- Committer:
- pierre11
- Date:
- Thu Jan 11 09:28:04 2018 +0000
- Revision:
- 1:e1c8062f2970
- Parent:
- 0:b98e768bc655
witekio
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
grantphillips | 0:b98e768bc655 | 1 | #include "TCS3200.h" |
grantphillips | 0:b98e768bc655 | 2 | #include "mbed.h" |
grantphillips | 0:b98e768bc655 | 3 | |
grantphillips | 0:b98e768bc655 | 4 | |
grantphillips | 0:b98e768bc655 | 5 | TCS3200::TCS3200(PinName S0, PinName S1, PinName S2, PinName S3, PinName OUT) : |
grantphillips | 0:b98e768bc655 | 6 | mS0(S0), mS1(S1), mS2(S2), mS3(S3), signal(OUT) |
grantphillips | 0:b98e768bc655 | 7 | { |
grantphillips | 0:b98e768bc655 | 8 | SetMode(SCALE_100); |
grantphillips | 0:b98e768bc655 | 9 | signal.rise(this,&TCS3200::HighTrigger); |
grantphillips | 0:b98e768bc655 | 10 | signal.fall(this,&TCS3200::LowTrigger); |
grantphillips | 0:b98e768bc655 | 11 | } |
grantphillips | 0:b98e768bc655 | 12 | |
pierre11 | 1:e1c8062f2970 | 13 | long TCS3200::ReadRed() |
pierre11 | 1:e1c8062f2970 | 14 | { |
grantphillips | 0:b98e768bc655 | 15 | mS2=0; |
grantphillips | 0:b98e768bc655 | 16 | mS3=0; |
grantphillips | 0:b98e768bc655 | 17 | wait(0.1); //Delay to allow frequency to change for the set color |
grantphillips | 0:b98e768bc655 | 18 | return(pulsewidth); |
grantphillips | 0:b98e768bc655 | 19 | } |
grantphillips | 0:b98e768bc655 | 20 | |
pierre11 | 1:e1c8062f2970 | 21 | long TCS3200::ReadGreen() |
pierre11 | 1:e1c8062f2970 | 22 | { |
grantphillips | 0:b98e768bc655 | 23 | mS2=1; |
grantphillips | 0:b98e768bc655 | 24 | mS3=1; |
grantphillips | 0:b98e768bc655 | 25 | wait(0.1); //Delay to allow frequency to change for the set color |
grantphillips | 0:b98e768bc655 | 26 | return(pulsewidth); |
grantphillips | 0:b98e768bc655 | 27 | } |
grantphillips | 0:b98e768bc655 | 28 | |
pierre11 | 1:e1c8062f2970 | 29 | long TCS3200::ReadBlue() |
pierre11 | 1:e1c8062f2970 | 30 | { |
grantphillips | 0:b98e768bc655 | 31 | mS2=0; |
grantphillips | 0:b98e768bc655 | 32 | mS3=1; |
grantphillips | 0:b98e768bc655 | 33 | wait(0.1); //Delay to allow frequency to change for the set color |
grantphillips | 0:b98e768bc655 | 34 | return(pulsewidth); |
grantphillips | 0:b98e768bc655 | 35 | } |
grantphillips | 0:b98e768bc655 | 36 | |
pierre11 | 1:e1c8062f2970 | 37 | long TCS3200::ReadClear() |
pierre11 | 1:e1c8062f2970 | 38 | { |
grantphillips | 0:b98e768bc655 | 39 | mS2=1; |
grantphillips | 0:b98e768bc655 | 40 | mS3=0; |
grantphillips | 0:b98e768bc655 | 41 | wait(0.1); //Delay to allow frequency to change for the set color |
grantphillips | 0:b98e768bc655 | 42 | return(pulsewidth); |
grantphillips | 0:b98e768bc655 | 43 | } |
grantphillips | 0:b98e768bc655 | 44 | |
pierre11 | 1:e1c8062f2970 | 45 | void TCS3200::SetMode(TCS3200Mode mode) |
pierre11 | 1:e1c8062f2970 | 46 | { |
pierre11 | 1:e1c8062f2970 | 47 | if(mode == POWERDOWN) |
pierre11 | 1:e1c8062f2970 | 48 | { //TCS3200 in power down |
grantphillips | 0:b98e768bc655 | 49 | mS0 = 0; |
grantphillips | 0:b98e768bc655 | 50 | mS1 = 0; |
grantphillips | 0:b98e768bc655 | 51 | } |
pierre11 | 1:e1c8062f2970 | 52 | else if(mode == SCALE_2) |
pierre11 | 1:e1c8062f2970 | 53 | { //Output frequency at 2% scaling |
grantphillips | 0:b98e768bc655 | 54 | mS0 = 0; |
grantphillips | 0:b98e768bc655 | 55 | mS1 = 1; |
grantphillips | 0:b98e768bc655 | 56 | } |
pierre11 | 1:e1c8062f2970 | 57 | else if(mode == SCALE_20) |
pierre11 | 1:e1c8062f2970 | 58 | { //Output frequency at 20% scaling |
grantphillips | 0:b98e768bc655 | 59 | mS0 = 1; |
grantphillips | 0:b98e768bc655 | 60 | mS1 = 0; |
grantphillips | 0:b98e768bc655 | 61 | } |
pierre11 | 1:e1c8062f2970 | 62 | else if(mode == SCALE_100) |
pierre11 | 1:e1c8062f2970 | 63 | { //Output frequency at 100% scaling |
grantphillips | 0:b98e768bc655 | 64 | mS0 = 1; |
grantphillips | 0:b98e768bc655 | 65 | mS1 = 1; |
grantphillips | 0:b98e768bc655 | 66 | } |
grantphillips | 0:b98e768bc655 | 67 | else { //default is POWERDOWN |
grantphillips | 0:b98e768bc655 | 68 | mS0 = 0; |
grantphillips | 0:b98e768bc655 | 69 | mS1 = 0; |
grantphillips | 0:b98e768bc655 | 70 | } |
grantphillips | 0:b98e768bc655 | 71 | } |
grantphillips | 0:b98e768bc655 | 72 | |
pierre11 | 1:e1c8062f2970 | 73 | void TCS3200::HighTrigger() |
pierre11 | 1:e1c8062f2970 | 74 | { |
grantphillips | 0:b98e768bc655 | 75 | timer.start(); |
grantphillips | 0:b98e768bc655 | 76 | } |
grantphillips | 0:b98e768bc655 | 77 | |
pierre11 | 1:e1c8062f2970 | 78 | void TCS3200::LowTrigger() |
pierre11 | 1:e1c8062f2970 | 79 | { |
grantphillips | 0:b98e768bc655 | 80 | timer.stop(); |
grantphillips | 0:b98e768bc655 | 81 | pulsewidth = timer.read_us(); |
grantphillips | 0:b98e768bc655 | 82 | timer.reset(); |
grantphillips | 0:b98e768bc655 | 83 | } |