Mattéo
Dependencies: mbed Pixy2_Library2
Pixy2_Libray.cpp@0:fc353d12d2b1, 2020-05-15 (annotated)
- Committer:
- benbrou06
- Date:
- Fri May 15 16:50:51 2020 +0000
- Revision:
- 0:fc353d12d2b1
Matteo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benbrou06 | 0:fc353d12d2b1 | 1 | /* |
benbrou06 | 0:fc353d12d2b1 | 2 | * mbed library for Pixy2 Camera |
benbrou06 | 0:fc353d12d2b1 | 3 | * Copyright (c) 2019 Jordan DUCHÊNE |
benbrou06 | 0:fc353d12d2b1 | 4 | */ |
benbrou06 | 0:fc353d12d2b1 | 5 | |
benbrou06 | 0:fc353d12d2b1 | 6 | #include "mbed.h" |
benbrou06 | 0:fc353d12d2b1 | 7 | #include "Pixy2_Library.h" |
benbrou06 | 0:fc353d12d2b1 | 8 | |
benbrou06 | 0:fc353d12d2b1 | 9 | |
benbrou06 | 0:fc353d12d2b1 | 10 | |
benbrou06 | 0:fc353d12d2b1 | 11 | |
benbrou06 | 0:fc353d12d2b1 | 12 | Pixy2_Library::Pixy2_Library (PinName p_sda, PinName p_scl, int address) // Class Constructor |
benbrou06 | 0:fc353d12d2b1 | 13 | { |
benbrou06 | 0:fc353d12d2b1 | 14 | composant = new I2C(p_sda,p_scl); //sda, sc1 |
benbrou06 | 0:fc353d12d2b1 | 15 | composant ->frequency(100000); |
benbrou06 | 0:fc353d12d2b1 | 16 | I2Caddress = address; |
benbrou06 | 0:fc353d12d2b1 | 17 | } |
benbrou06 | 0:fc353d12d2b1 | 18 | |
benbrou06 | 0:fc353d12d2b1 | 19 | |
benbrou06 | 0:fc353d12d2b1 | 20 | void Pixy2_Library::Pixy2_WhiteLED(bool STATE) // White LEDs BuiltIN ON/OFF methode |
benbrou06 | 0:fc353d12d2b1 | 21 | { |
benbrou06 | 0:fc353d12d2b1 | 22 | |
benbrou06 | 0:fc353d12d2b1 | 23 | unsigned char readdata[21]; |
benbrou06 | 0:fc353d12d2b1 | 24 | unsigned char lampeON[6]= {0xae,0xc1,22,2,1,0}; |
benbrou06 | 0:fc353d12d2b1 | 25 | unsigned char lampeOFF[6]= {0xae,0xc1,22,2,0,0}; |
benbrou06 | 0:fc353d12d2b1 | 26 | if(STATE == true) { |
benbrou06 | 0:fc353d12d2b1 | 27 | do { |
benbrou06 | 0:fc353d12d2b1 | 28 | composant->write(I2Caddress,(char*)lampeON,6); |
benbrou06 | 0:fc353d12d2b1 | 29 | composant->read(I2Caddress,(char*)readdata,10); |
benbrou06 | 0:fc353d12d2b1 | 30 | wait(0.01); |
benbrou06 | 0:fc353d12d2b1 | 31 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 32 | } else { |
benbrou06 | 0:fc353d12d2b1 | 33 | do { |
benbrou06 | 0:fc353d12d2b1 | 34 | composant->write(I2Caddress,(char*)lampeOFF,6); |
benbrou06 | 0:fc353d12d2b1 | 35 | composant->read(I2Caddress,(char*)readdata,10); |
benbrou06 | 0:fc353d12d2b1 | 36 | wait(0.01); |
benbrou06 | 0:fc353d12d2b1 | 37 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 38 | } |
benbrou06 | 0:fc353d12d2b1 | 39 | |
benbrou06 | 0:fc353d12d2b1 | 40 | } |
benbrou06 | 0:fc353d12d2b1 | 41 | void Pixy2_Library::Pixy2_SetServo(int PAN, int TILT) // Head Servo Crontrol methode |
benbrou06 | 0:fc353d12d2b1 | 42 | { |
benbrou06 | 0:fc353d12d2b1 | 43 | int lowP, hightP, lowT, hightT; |
benbrou06 | 0:fc353d12d2b1 | 44 | hightP = PAN >> 8 ; |
benbrou06 | 0:fc353d12d2b1 | 45 | lowP = PAN & 0x0f; |
benbrou06 | 0:fc353d12d2b1 | 46 | hightT = TILT >> 8 ; |
benbrou06 | 0:fc353d12d2b1 | 47 | lowT = TILT & 0x0f; |
benbrou06 | 0:fc353d12d2b1 | 48 | unsigned char readdata[21]; |
benbrou06 | 0:fc353d12d2b1 | 49 | unsigned char SetServ[8]= {0xae,0xc1,18,4,lowP,hightP,lowT,hightP}; |
benbrou06 | 0:fc353d12d2b1 | 50 | do { |
benbrou06 | 0:fc353d12d2b1 | 51 | composant->write(I2Caddress,(char*)SetServ,6); |
benbrou06 | 0:fc353d12d2b1 | 52 | composant->read(I2Caddress,(char*)readdata,10); |
benbrou06 | 0:fc353d12d2b1 | 53 | wait(0.01); |
benbrou06 | 0:fc353d12d2b1 | 54 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 55 | } |
benbrou06 | 0:fc353d12d2b1 | 56 | |
benbrou06 | 0:fc353d12d2b1 | 57 | |
benbrou06 | 0:fc353d12d2b1 | 58 | void Pixy2_Library::Pixy2_RGBLED(bool STATE) |
benbrou06 | 0:fc353d12d2b1 | 59 | { |
benbrou06 | 0:fc353d12d2b1 | 60 | |
benbrou06 | 0:fc353d12d2b1 | 61 | unsigned char readdata[21]; |
benbrou06 | 0:fc353d12d2b1 | 62 | unsigned char lampeON[6]= {0xae,0xc1,22,2,0,1}; |
benbrou06 | 0:fc353d12d2b1 | 63 | unsigned char lampeOFF[6]= {0xae,0xc1,22,2,0,0}; |
benbrou06 | 0:fc353d12d2b1 | 64 | if(STATE == true) { |
benbrou06 | 0:fc353d12d2b1 | 65 | do { |
benbrou06 | 0:fc353d12d2b1 | 66 | composant->write(I2Caddress,(char*)lampeON,6); |
benbrou06 | 0:fc353d12d2b1 | 67 | composant->read(I2Caddress,(char*)readdata,10); |
benbrou06 | 0:fc353d12d2b1 | 68 | wait(0.01); |
benbrou06 | 0:fc353d12d2b1 | 69 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 70 | } else { |
benbrou06 | 0:fc353d12d2b1 | 71 | do { |
benbrou06 | 0:fc353d12d2b1 | 72 | composant->write(I2Caddress,(char*)lampeOFF,6); |
benbrou06 | 0:fc353d12d2b1 | 73 | composant->read(I2Caddress,(char*)readdata,10); |
benbrou06 | 0:fc353d12d2b1 | 74 | wait(0.01); |
benbrou06 | 0:fc353d12d2b1 | 75 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 76 | } |
benbrou06 | 0:fc353d12d2b1 | 77 | |
benbrou06 | 0:fc353d12d2b1 | 78 | } |
benbrou06 | 0:fc353d12d2b1 | 79 | |
benbrou06 | 0:fc353d12d2b1 | 80 | int Pixy2_Library::Pixy2_GetBlock(char DATA, int BLOCK_NUMBER) |
benbrou06 | 0:fc353d12d2b1 | 81 | { |
benbrou06 | 0:fc353d12d2b1 | 82 | int DataSize = BLOCK_NUMBER * 14 + 6; |
benbrou06 | 0:fc353d12d2b1 | 83 | unsigned char writedata[6]= {0xae,0xc1,32,2,255,20}; |
benbrou06 | 0:fc353d12d2b1 | 84 | unsigned char readdata[DataSize + 1]; |
benbrou06 | 0:fc353d12d2b1 | 85 | do { |
benbrou06 | 0:fc353d12d2b1 | 86 | composant->write(I2Caddress,(char*)writedata,6); |
benbrou06 | 0:fc353d12d2b1 | 87 | composant->read(I2Caddress,(char*)readdata,DataSize); |
benbrou06 | 0:fc353d12d2b1 | 88 | wait(0.1); |
benbrou06 | 0:fc353d12d2b1 | 89 | } while(readdata[0] != 175 && readdata[1] != 193); |
benbrou06 | 0:fc353d12d2b1 | 90 | switch(DATA) { |
benbrou06 | 0:fc353d12d2b1 | 91 | case 's': |
benbrou06 | 0:fc353d12d2b1 | 92 | if(readdata[DataSize - 13]<2) return readdata[7]*256+readdata[DataSize - 14]; |
benbrou06 | 0:fc353d12d2b1 | 93 | if(readdata[DataSize - 13]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 94 | break; |
benbrou06 | 0:fc353d12d2b1 | 95 | case 'x': |
benbrou06 | 0:fc353d12d2b1 | 96 | if(readdata[DataSize - 11]<2) return readdata[DataSize - 11]*256+readdata[DataSize - 12]; |
benbrou06 | 0:fc353d12d2b1 | 97 | if(readdata[DataSize - 11]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 98 | break; |
benbrou06 | 0:fc353d12d2b1 | 99 | case 'y': |
benbrou06 | 0:fc353d12d2b1 | 100 | if(readdata[DataSize - 9]<2) return readdata[DataSize - 9]*256+readdata[DataSize - 10]; |
benbrou06 | 0:fc353d12d2b1 | 101 | if(readdata[DataSize - 9]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 102 | break; |
benbrou06 | 0:fc353d12d2b1 | 103 | case 'w': |
benbrou06 | 0:fc353d12d2b1 | 104 | if(readdata[DataSize - 7]<2) return readdata[DataSize - 7]*256+readdata[DataSize - 8]; |
benbrou06 | 0:fc353d12d2b1 | 105 | if(readdata[DataSize - 7]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 106 | break; |
benbrou06 | 0:fc353d12d2b1 | 107 | case 'h': |
benbrou06 | 0:fc353d12d2b1 | 108 | if(readdata[DataSize - 5]<2) return readdata[DataSize - 5]*256+readdata[DataSize - 6]; |
benbrou06 | 0:fc353d12d2b1 | 109 | if(readdata[DataSize - 5]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 110 | break; |
benbrou06 | 0:fc353d12d2b1 | 111 | case 'a': |
benbrou06 | 0:fc353d12d2b1 | 112 | if(readdata[DataSize - 3]<2) return readdata[DataSize - 3]*256+readdata[DataSize - 4]; |
benbrou06 | 0:fc353d12d2b1 | 113 | if(readdata[DataSize - 3]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 114 | break; |
benbrou06 | 0:fc353d12d2b1 | 115 | case 'i': |
benbrou06 | 0:fc353d12d2b1 | 116 | if(readdata[DataSize - 1]<2) return readdata[DataSize - 1]*256+readdata[DataSize - 2]; //////////////////////// 1 |
benbrou06 | 0:fc353d12d2b1 | 117 | if(readdata[DataSize - 1]>=2) return -2; |
benbrou06 | 0:fc353d12d2b1 | 118 | break; |
benbrou06 | 0:fc353d12d2b1 | 119 | } |
benbrou06 | 0:fc353d12d2b1 | 120 | } |
benbrou06 | 0:fc353d12d2b1 | 121 | |
benbrou06 | 0:fc353d12d2b1 | 122 | |
benbrou06 | 0:fc353d12d2b1 | 123 |