First version
Dependencies: mbed EthernetInterface mbed-rto
LEDDriver.cpp@52:45207965e160, 2018-05-09 (annotated)
- Committer:
- KlaasGovaerts
- Date:
- Wed May 09 12:31:31 2018 +0000
- Revision:
- 52:45207965e160
- Parent:
- 51:b9da0ba69a29
- Child:
- 54:4274186bf584
Blinking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
albireo987 | 38:47c075a354b5 | 1 | #include "LEDDriver.h" |
KlaasGovaerts | 45:2d877f01ca7b | 2 | //Serial pc(USBTX,USBRX); |
albireo987 | 26:4c673da1a3ae | 3 | LEDDriver::LEDDriver( Queue<int,8>* queue) |
albireo987 | 6:36158325f7b0 | 4 | { |
KlaasGovaerts | 45:2d877f01ca7b | 5 | //this->messageList=messageList; |
KlaasGovaerts | 45:2d877f01ca7b | 6 | //this->leds=leds; |
albireo987 | 26:4c673da1a3ae | 7 | this->queue=queue;// prep for multithreaded |
KlaasGovaerts | 47:8fe84f455571 | 8 | printf("Queue created\r\n"); |
KlaasGovaerts | 45:2d877f01ca7b | 9 | initArrays(); |
KlaasGovaerts | 47:8fe84f455571 | 10 | printf("Arrays initialised.\r\n"); |
KlaasGovaerts | 45:2d877f01ca7b | 11 | currentLocation=0; |
albireo987 | 6:36158325f7b0 | 12 | } |
albireo987 | 6:36158325f7b0 | 13 | |
albireo987 | 6:36158325f7b0 | 14 | void LEDDriver::drive(int numberOfLeds) |
albireo987 | 6:36158325f7b0 | 15 | { |
KlaasGovaerts | 47:8fe84f455571 | 16 | if(currentLocation==0){ |
KlaasGovaerts | 47:8fe84f455571 | 17 | poll(numberOfLeds,1); |
KlaasGovaerts | 47:8fe84f455571 | 18 | poll(numberOfLeds,2); |
KlaasGovaerts | 47:8fe84f455571 | 19 | } |
KlaasGovaerts | 47:8fe84f455571 | 20 | |
KlaasGovaerts | 45:2d877f01ca7b | 21 | setLEDS(currentLocation); |
KlaasGovaerts | 45:2d877f01ca7b | 22 | currentLocation++; |
KlaasGovaerts | 45:2d877f01ca7b | 23 | if(currentLocation>=20){ |
KlaasGovaerts | 45:2d877f01ca7b | 24 | currentLocation=0; |
KlaasGovaerts | 45:2d877f01ca7b | 25 | } |
KlaasGovaerts | 44:ffac07bff0d8 | 26 | |
KlaasGovaerts | 44:ffac07bff0d8 | 27 | /* |
albireo987 | 38:47c075a354b5 | 28 | Ticker ticker; |
albireo987 | 32:537005b4a065 | 29 | while(true) |
albireo987 | 26:4c673da1a3ae | 30 | { |
albireo987 | 27:29948b116e82 | 31 | poll(numberOfLeds,1) ;//prep multi |
KlaasGovaerts | 42:bb1d9d8a9fe4 | 32 | //for(int i =0;i<numberOfLeds;i++) |
KlaasGovaerts | 42:bb1d9d8a9fe4 | 33 | //{ |
KlaasGovaerts | 44:ffac07bff0d8 | 34 | //ticker.attach(this,&LEDDriver::sendData,0.000625f); |
KlaasGovaerts | 42:bb1d9d8a9fe4 | 35 | //} |
albireo987 | 38:47c075a354b5 | 36 | poll(numberOfLeds,2) ; |
KlaasGovaerts | 44:ffac07bff0d8 | 37 | }*/ |
albireo987 | 6:36158325f7b0 | 38 | } |
albireo987 | 6:36158325f7b0 | 39 | |
KlaasGovaerts | 44:ffac07bff0d8 | 40 | void LEDDriver::run(Ticker* ticker) |
albireo987 | 6:36158325f7b0 | 41 | { |
KlaasGovaerts | 47:8fe84f455571 | 42 | printf("ticker attached.\r\n"); |
KlaasGovaerts | 48:c5cd73d93045 | 43 | ticker->attach(this,&LEDDriver::drive8leds,10.0);//TODO faster |
KlaasGovaerts | 44:ffac07bff0d8 | 44 | } |
KlaasGovaerts | 44:ffac07bff0d8 | 45 | |
KlaasGovaerts | 44:ffac07bff0d8 | 46 | void LEDDriver::drive8leds(){ |
KlaasGovaerts | 47:8fe84f455571 | 47 | printf("tick\r\n");//TODO |
KlaasGovaerts | 44:ffac07bff0d8 | 48 | drive(8); |
albireo987 | 6:36158325f7b0 | 49 | } |
albireo987 | 6:36158325f7b0 | 50 | |
albireo987 | 10:7871aeacea08 | 51 | void LEDDriver::ownWait(uint32_t us) |
albireo987 | 10:7871aeacea08 | 52 | { |
albireo987 | 10:7871aeacea08 | 53 | for(uint32_t i=0;i<us;i++) |
albireo987 | 10:7871aeacea08 | 54 | { |
albireo987 | 38:47c075a354b5 | 55 | for(volatile uint32_t j =0; j <3;j++) |
albireo987 | 10:7871aeacea08 | 56 | { |
albireo987 | 10:7871aeacea08 | 57 | |
albireo987 | 10:7871aeacea08 | 58 | } |
albireo987 | 10:7871aeacea08 | 59 | } |
albireo987 | 26:4c673da1a3ae | 60 | } |
albireo987 | 26:4c673da1a3ae | 61 | |
albireo987 | 27:29948b116e82 | 62 | void LEDDriver::poll(int numberOfLeds,int first) |
albireo987 | 26:4c673da1a3ae | 63 | { |
albireo987 | 27:29948b116e82 | 64 | if(first==1) |
albireo987 | 27:29948b116e82 | 65 | { |
albireo987 | 27:29948b116e82 | 66 | for(int i =0;i<(numberOfLeds/2);i++) |
albireo987 | 27:29948b116e82 | 67 | { |
KlaasGovaerts | 44:ffac07bff0d8 | 68 | osEvent event = queue->get(); |
KlaasGovaerts | 44:ffac07bff0d8 | 69 | int temp = *((int*)event.value.p); |
KlaasGovaerts | 52:45207965e160 | 70 | if(temp==0||temp==1||temp==2||temp==3||temp==4) |
albireo987 | 27:29948b116e82 | 71 | { |
KlaasGovaerts | 44:ffac07bff0d8 | 72 | messageList[i]=temp; |
KlaasGovaerts | 52:45207965e160 | 73 | } |
albireo987 | 27:29948b116e82 | 74 | } |
albireo987 | 27:29948b116e82 | 75 | } |
albireo987 | 27:29948b116e82 | 76 | else if(first==2) |
albireo987 | 38:47c075a354b5 | 77 | { |
albireo987 | 27:29948b116e82 | 78 | for(int i =(numberOfLeds/2);i<(numberOfLeds);i++) |
albireo987 | 27:29948b116e82 | 79 | { |
KlaasGovaerts | 44:ffac07bff0d8 | 80 | osEvent event = queue->get(); |
KlaasGovaerts | 44:ffac07bff0d8 | 81 | int temp = *((int*)event.value.p); |
KlaasGovaerts | 52:45207965e160 | 82 | if(temp==0||temp==1||temp==2||temp==3||temp==4) |
albireo987 | 27:29948b116e82 | 83 | { |
KlaasGovaerts | 44:ffac07bff0d8 | 84 | messageList[i]=temp; |
KlaasGovaerts | 52:45207965e160 | 85 | } |
albireo987 | 27:29948b116e82 | 86 | } |
albireo987 | 32:537005b4a065 | 87 | } |
albireo987 | 38:47c075a354b5 | 88 | } |
albireo987 | 38:47c075a354b5 | 89 | |
KlaasGovaerts | 45:2d877f01ca7b | 90 | void LEDDriver::initArrays() |
albireo987 | 38:47c075a354b5 | 91 | { |
KlaasGovaerts | 45:2d877f01ca7b | 92 | // 1 1 1 1 0 0 0 0 0 1 |
KlaasGovaerts | 45:2d877f01ca7b | 93 | int tempCodedMessage0[20]={1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0};//{1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,1,0}; currently frequency measure message(standard square wave) |
albireo987 | 38:47c075a354b5 | 94 | // 1 1 1 1 0 1 0 0 0 1 |
KlaasGovaerts | 45:2d877f01ca7b | 95 | int tempCodedMessage1[20]={1,0,1,0,1,0,1,0,0,1,1,0,0,1,0,1,0,1,1,0}; |
albireo987 | 38:47c075a354b5 | 96 | // 1 1 1 1 1 0 1 0 0 1 |
KlaasGovaerts | 45:2d877f01ca7b | 97 | int tempCodedMessage2[20]={1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,1,0}; |
albireo987 | 38:47c075a354b5 | 98 | // 1 1 1 1 1 1 0 0 0 1 |
KlaasGovaerts | 45:2d877f01ca7b | 99 | int tempCodedMessage3[20]={1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,1,1,0}; |
KlaasGovaerts | 45:2d877f01ca7b | 100 | int tempCodedMessage4[20]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//default message for multithreading |
KlaasGovaerts | 47:8fe84f455571 | 101 | int tempMessageList[8]={0,0,0,0,0,0,0,0};//TODO |
KlaasGovaerts | 48:c5cd73d93045 | 102 | DigitalOut *tempLeds[]={new DigitalOut(p23),new DigitalOut(p11),new DigitalOut(p12),new DigitalOut(p13),new DigitalOut(p14),new DigitalOut(p15),new DigitalOut(p16),new DigitalOut(p17)}; |
KlaasGovaerts | 47:8fe84f455571 | 103 | printf("Arrays created.\r\n"); |
KlaasGovaerts | 45:2d877f01ca7b | 104 | |
KlaasGovaerts | 45:2d877f01ca7b | 105 | memcpy(codedMessage0,tempCodedMessage0,sizeof(codedMessage0)); |
KlaasGovaerts | 45:2d877f01ca7b | 106 | memcpy(codedMessage1,tempCodedMessage1,sizeof(codedMessage1)); |
KlaasGovaerts | 45:2d877f01ca7b | 107 | memcpy(codedMessage2,tempCodedMessage2,sizeof(codedMessage2)); |
KlaasGovaerts | 45:2d877f01ca7b | 108 | memcpy(codedMessage3,tempCodedMessage3,sizeof(codedMessage3)); |
KlaasGovaerts | 45:2d877f01ca7b | 109 | memcpy(codedMessage4,tempCodedMessage4,sizeof(codedMessage4)); |
KlaasGovaerts | 47:8fe84f455571 | 110 | printf("First 5 arrays copied.\r\n"); |
KlaasGovaerts | 45:2d877f01ca7b | 111 | |
KlaasGovaerts | 47:8fe84f455571 | 112 | memcpy(messageList,tempMessageList,sizeof(messageList)); |
KlaasGovaerts | 47:8fe84f455571 | 113 | printf("messageList array copied.\r\n"); |
KlaasGovaerts | 47:8fe84f455571 | 114 | |
KlaasGovaerts | 45:2d877f01ca7b | 115 | memcpy(leds,tempLeds,sizeof(leds)); |
KlaasGovaerts | 47:8fe84f455571 | 116 | printf("LEDS array copied.\r\n"); |
KlaasGovaerts | 45:2d877f01ca7b | 117 | } |
KlaasGovaerts | 45:2d877f01ca7b | 118 | |
KlaasGovaerts | 45:2d877f01ca7b | 119 | void LEDDriver::setLEDS(int j){ |
albireo987 | 38:47c075a354b5 | 120 | for(int led =0 ; led<8;led++) |
albireo987 | 38:47c075a354b5 | 121 | { |
albireo987 | 38:47c075a354b5 | 122 | if(messageList[led]==0) |
albireo987 | 38:47c075a354b5 | 123 | { |
KlaasGovaerts | 48:c5cd73d93045 | 124 | leds[led]->write(codedMessage0[j]); |
KlaasGovaerts | 45:2d877f01ca7b | 125 | //ownWait(25);//100HZ |
albireo987 | 38:47c075a354b5 | 126 | } |
albireo987 | 38:47c075a354b5 | 127 | else if(messageList[led]==1) |
albireo987 | 38:47c075a354b5 | 128 | { |
KlaasGovaerts | 48:c5cd73d93045 | 129 | leds[led]->write(codedMessage1[j]); |
KlaasGovaerts | 45:2d877f01ca7b | 130 | //ownWait(25); |
albireo987 | 38:47c075a354b5 | 131 | } |
albireo987 | 38:47c075a354b5 | 132 | else if(messageList[led]==2) |
albireo987 | 38:47c075a354b5 | 133 | { |
KlaasGovaerts | 48:c5cd73d93045 | 134 | leds[led]->write(codedMessage2[j]); |
KlaasGovaerts | 45:2d877f01ca7b | 135 | //ownWait(25); |
albireo987 | 38:47c075a354b5 | 136 | } |
albireo987 | 38:47c075a354b5 | 137 | else if(messageList[led]==3) |
albireo987 | 38:47c075a354b5 | 138 | { |
KlaasGovaerts | 48:c5cd73d93045 | 139 | leds[led]->write(codedMessage3[j]); |
KlaasGovaerts | 45:2d877f01ca7b | 140 | //ownWait(25); |
albireo987 | 38:47c075a354b5 | 141 | } |
KlaasGovaerts | 47:8fe84f455571 | 142 | else if(messageList[led]==4||messageList[led]==-1) |
albireo987 | 38:47c075a354b5 | 143 | { |
KlaasGovaerts | 48:c5cd73d93045 | 144 | leds[led]->write(codedMessage4[j]); |
KlaasGovaerts | 45:2d877f01ca7b | 145 | //ownWait(25); |
albireo987 | 38:47c075a354b5 | 146 | } |
albireo987 | 38:47c075a354b5 | 147 | } |
KlaasGovaerts | 48:c5cd73d93045 | 148 | } |
KlaasGovaerts | 48:c5cd73d93045 | 149 | |
KlaasGovaerts | 48:c5cd73d93045 | 150 | //TODO destructor |