Committer:
yxyang
Date:
Tue May 30 06:54:21 2017 +0000
Revision:
0:4e108bee7994

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yxyang 0:4e108bee7994 1 #include "TSL1401CL.h"
yxyang 0:4e108bee7994 2
yxyang 0:4e108bee7994 3 TSL1401CL::TSL1401CL(PinName pin_clk, PinName pin_si, PinName pin_adc) : clk(pin_clk), si(pin_si), adc(pin_adc), integration_time(0)
yxyang 0:4e108bee7994 4 {
yxyang 0:4e108bee7994 5 clk.write(0);
yxyang 0:4e108bee7994 6 integration_timer.start();
yxyang 0:4e108bee7994 7 }
yxyang 0:4e108bee7994 8
yxyang 0:4e108bee7994 9 bool TSL1401CL::integrationReady()
yxyang 0:4e108bee7994 10 {
yxyang 0:4e108bee7994 11 return static_cast<uint32_t>(integration_timer.read_us()) >= integration_time;
yxyang 0:4e108bee7994 12 }
yxyang 0:4e108bee7994 13
yxyang 0:4e108bee7994 14 void TSL1401CL::read()
yxyang 0:4e108bee7994 15 {
yxyang 0:4e108bee7994 16 integration_timer.reset();
yxyang 0:4e108bee7994 17 integration_timer.start();
yxyang 0:4e108bee7994 18 clk.write(0);
yxyang 0:4e108bee7994 19 si.write(1);
yxyang 0:4e108bee7994 20 clk.write(1);
yxyang 0:4e108bee7994 21 si.write(0);
yxyang 0:4e108bee7994 22
yxyang 0:4e108bee7994 23 for (uint8_t i = 0; i < TSL1401CL_PIXEL_COUNT; i++) {
yxyang 0:4e108bee7994 24 clk.write(0);
yxyang 0:4e108bee7994 25 data[i] = adc.read_u16();
yxyang 0:4e108bee7994 26 clk.write(1);
yxyang 0:4e108bee7994 27 }
yxyang 0:4e108bee7994 28 clk.write(0);
yxyang 0:4e108bee7994 29 }
yxyang 0:4e108bee7994 30
yxyang 0:4e108bee7994 31 void TSL1401CL::setIntegrationTime(uint32_t int_time_desired)
yxyang 0:4e108bee7994 32 {
yxyang 0:4e108bee7994 33 integration_time = int_time_desired;
yxyang 0:4e108bee7994 34 }
yxyang 0:4e108bee7994 35
yxyang 0:4e108bee7994 36 int TSL1401CL::findLineEdge (uint16_t threshold, uint8_t precision, size_t crop_amount, bool invert)
yxyang 0:4e108bee7994 37 {
yxyang 0:4e108bee7994 38 if(precision == 0) precision = 1;
yxyang 0:4e108bee7994 39
yxyang 0:4e108bee7994 40 // Create an array with resolution determined by precision containing the change between points in data
yxyang 0:4e108bee7994 41 uint16_t change_len = (TSL1401CL_PIXEL_COUNT - 2*crop_amount)/precision - 1;
yxyang 0:4e108bee7994 42 int32_t change_in_data[change_len];
yxyang 0:4e108bee7994 43 for(uint16_t i = crop_amount, j = 0; i < TSL1401CL_PIXEL_COUNT - crop_amount - precision; i += precision, j++) {
yxyang 0:4e108bee7994 44 change_in_data[j] = (int)data[i+precision] - (int)data[i];
yxyang 0:4e108bee7994 45 }
yxyang 0:4e108bee7994 46
yxyang 0:4e108bee7994 47 // Find the index of the maximum value in change_in_data
yxyang 0:4e108bee7994 48 uint16_t maxchange = 0;
yxyang 0:4e108bee7994 49 for(uint16_t i = 1; i < change_len; i++) {
yxyang 0:4e108bee7994 50 if(invert) {
yxyang 0:4e108bee7994 51 if(change_in_data[i] < change_in_data[maxchange]) maxchange = i;
yxyang 0:4e108bee7994 52 } else {
yxyang 0:4e108bee7994 53 if(change_in_data[i] > change_in_data[maxchange]) maxchange = i;
yxyang 0:4e108bee7994 54 }
yxyang 0:4e108bee7994 55 }
yxyang 0:4e108bee7994 56
yxyang 0:4e108bee7994 57 // If there isn't a sharp enough change returns -1
yxyang 0:4e108bee7994 58 if((change_in_data[maxchange] >= threshold && !invert) || (change_in_data[maxchange]*-1 >= threshold && invert)) {
yxyang 0:4e108bee7994 59 return maxchange*precision + crop_amount;
yxyang 0:4e108bee7994 60 }
yxyang 0:4e108bee7994 61 else return -1;
yxyang 0:4e108bee7994 62 }
yxyang 0:4e108bee7994 63
yxyang 0:4e108bee7994 64 int TSL1401CL::findLineCenter (uint16_t threshold, uint8_t precision, size_t crop_amount)
yxyang 0:4e108bee7994 65 {
yxyang 0:4e108bee7994 66 if(precision == 0) precision = 1;
yxyang 0:4e108bee7994 67
yxyang 0:4e108bee7994 68 // Create an array with resolution determined by precision containing the change between points in data
yxyang 0:4e108bee7994 69 uint16_t change_len = (TSL1401CL_PIXEL_COUNT - 2*crop_amount)/precision - 1;
yxyang 0:4e108bee7994 70 int32_t change_in_data[change_len];
yxyang 0:4e108bee7994 71 for(uint16_t i = crop_amount, j = 0; i < TSL1401CL_PIXEL_COUNT - crop_amount - precision; i += precision, j++) {
yxyang 0:4e108bee7994 72 change_in_data[j] = (int)data[i+precision] - (int)data[i];
yxyang 0:4e108bee7994 73 }
yxyang 0:4e108bee7994 74
yxyang 0:4e108bee7994 75 // Find the index of the maximum and minimum value in change_in_data
yxyang 0:4e108bee7994 76 uint16_t maxchange = 0;
yxyang 0:4e108bee7994 77 uint16_t minchange = 0;
yxyang 0:4e108bee7994 78 for(uint16_t i = 1; i < change_len; i++) {
yxyang 0:4e108bee7994 79 if(change_in_data[i] > change_in_data[maxchange]) maxchange = i;
yxyang 0:4e108bee7994 80 if(change_in_data[i] < change_in_data[minchange]) minchange = i;
yxyang 0:4e108bee7994 81 }
yxyang 0:4e108bee7994 82
yxyang 0:4e108bee7994 83 // Return the average of the max and min value
yxyang 0:4e108bee7994 84 if(change_in_data[maxchange] >= threshold && change_in_data[minchange]*-1 >= threshold) {
yxyang 0:4e108bee7994 85 return (precision*maxchange+precision*minchange)/2 + crop_amount;
yxyang 0:4e108bee7994 86 }
yxyang 0:4e108bee7994 87 return -1;
yxyang 0:4e108bee7994 88 }
yxyang 0:4e108bee7994 89
yxyang 0:4e108bee7994 90 uint32_t TSL1401CL::getData(uint8_t index)
yxyang 0:4e108bee7994 91 {
yxyang 0:4e108bee7994 92 return data[index];
yxyang 0:4e108bee7994 93 }