Template for the ELEC1620 End of year exam

Dependencies:   mbed

Committer:
el16ttb
Date:
Fri Mar 22 13:11:07 2019 +0000
Revision:
0:54721f063ac8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el16ttb 0:54721f063ac8 1 #include "StreamOut.h"
el16ttb 0:54721f063ac8 2 //PinName clk, PinName data, PinName latch
el16ttb 0:54721f063ac8 3
el16ttb 0:54721f063ac8 4 int StreamOut::_index = 0;
el16ttb 0:54721f063ac8 5 int StreamOut::_locked = 0;
el16ttb 0:54721f063ac8 6 Message StreamOut::_buffer[BUFFERSIZE];
el16ttb 0:54721f063ac8 7 DigitalOut* StreamOut::_clk = new DigitalOut(p6);
el16ttb 0:54721f063ac8 8 DigitalOut* StreamOut::_latch = new DigitalOut(p25);
el16ttb 0:54721f063ac8 9 DigitalOut* StreamOut::_data = new DigitalOut(p14);
el16ttb 0:54721f063ac8 10
el16ttb 0:54721f063ac8 11 StreamOut::StreamOut()
el16ttb 0:54721f063ac8 12 {
el16ttb 0:54721f063ac8 13
el16ttb 0:54721f063ac8 14 }
el16ttb 0:54721f063ac8 15
el16ttb 0:54721f063ac8 16 StreamOut::~StreamOut(){
el16ttb 0:54721f063ac8 17 delete _clk, _latch, _data;
el16ttb 0:54721f063ac8 18 }
el16ttb 0:54721f063ac8 19
el16ttb 0:54721f063ac8 20 void StreamOut::lock(){
el16ttb 0:54721f063ac8 21 _locked++;
el16ttb 0:54721f063ac8 22 }
el16ttb 0:54721f063ac8 23
el16ttb 0:54721f063ac8 24 void StreamOut::unlock(){
el16ttb 0:54721f063ac8 25 _locked--;
el16ttb 0:54721f063ac8 26 if(_locked < 0){
el16ttb 0:54721f063ac8 27 _locked = 0;
el16ttb 0:54721f063ac8 28 }
el16ttb 0:54721f063ac8 29 }
el16ttb 0:54721f063ac8 30
el16ttb 0:54721f063ac8 31 void StreamOut::init(){
el16ttb 0:54721f063ac8 32
el16ttb 0:54721f063ac8 33 }
el16ttb 0:54721f063ac8 34
el16ttb 0:54721f063ac8 35 void StreamOut::writeMessage(char code, unsigned int data){
el16ttb 0:54721f063ac8 36 if(_index < BUFFERSIZE && _locked == 0){
el16ttb 0:54721f063ac8 37 // printf("Write message: %x %x\n", code, data);
el16ttb 0:54721f063ac8 38 // delete _buffer[_index];
el16ttb 0:54721f063ac8 39 _buffer[_index].set(code, data);
el16ttb 0:54721f063ac8 40 _index++;
el16ttb 0:54721f063ac8 41 }
el16ttb 0:54721f063ac8 42 }
el16ttb 0:54721f063ac8 43
el16ttb 0:54721f063ac8 44 void StreamOut::sendMessages(){
el16ttb 0:54721f063ac8 45 // printf("Send message\n");
el16ttb 0:54721f063ac8 46 int remove = 0;
el16ttb 0:54721f063ac8 47 while(remove < _index){
el16ttb 0:54721f063ac8 48 sendMessage(_buffer[remove]);
el16ttb 0:54721f063ac8 49 remove++;
el16ttb 0:54721f063ac8 50 }
el16ttb 0:54721f063ac8 51 _index = 0;
el16ttb 0:54721f063ac8 52 //printf("Messages sent\n");
el16ttb 0:54721f063ac8 53 }
el16ttb 0:54721f063ac8 54
el16ttb 0:54721f063ac8 55 void StreamOut::sendMessage(Message msg){
el16ttb 0:54721f063ac8 56 // printf("Message sent: %x %x\n", msg.get_code(), msg.get_data());
el16ttb 0:54721f063ac8 57 *_latch = 1;
el16ttb 0:54721f063ac8 58 // wait_us(30);
el16ttb 0:54721f063ac8 59 for(int i = 3; i >= 0; i--){
el16ttb 0:54721f063ac8 60 *_clk = 0;
el16ttb 0:54721f063ac8 61 wait_us(5);
el16ttb 0:54721f063ac8 62 *_data = (msg.get_code() & (1 << i)) != 0;
el16ttb 0:54721f063ac8 63 *_clk = 1;
el16ttb 0:54721f063ac8 64 wait_us(5);
el16ttb 0:54721f063ac8 65 }
el16ttb 0:54721f063ac8 66 for (int i = 31; i >= 0; i--) {
el16ttb 0:54721f063ac8 67 *_clk = 0;
el16ttb 0:54721f063ac8 68 wait_us(5);
el16ttb 0:54721f063ac8 69 *_data = (msg.get_data() & (1 << i)) != 0;
el16ttb 0:54721f063ac8 70 *_clk = 1;
el16ttb 0:54721f063ac8 71 wait_us(5);
el16ttb 0:54721f063ac8 72 }
el16ttb 0:54721f063ac8 73 *_latch = 0;
el16ttb 0:54721f063ac8 74 wait_us(10);
el16ttb 0:54721f063ac8 75 }
el16ttb 0:54721f063ac8 76