Yoctopuce Yocto-I2C Mode D'emploi page 179

Table des Matières

Publicité

Un exemple réel
Lancez Xcode 4.2 et ouvrez le projet exemple correspondant, fourni dans le répertoire Examples/
Doc-GettingStarted-Yocto-I2C de la librairie Yoctopuce.
Vous reconnaîtrez dans cet exemple l'utilisation des fonctions expliquées ci-dessus, cette fois
utilisées avec le décorum nécessaire à en faire un petit programme d'exemple concret.
#import <Foundation/Foundation.h>
#import "yocto_api.h"
#import "yocto_i2cport.h"
static void
usage(void)
{
NSLog(@"usage: demo
NSLog(@"
demo
NSLog(@"
demo any
exit(1);
}
int main(int
argc,
{
NSError
*error;
if
(argc <
2) {
usage();
}
@autoreleasepool {
// Setup the API to use local USB devices
if([YAPI
RegisterHub:@"usb": &error] != YAPI_SUCCESS) {
NSLog(@"RegisterHub error:
return
1;
}
NSString
*target
if
([target
isEqualToString:@"any"]) {
YI2cPort
*i2cPort
if
(i2cPort
NSLog(@"No module connected (check USB
return
1;
}
target
=
[[i2cPort
}
YI2cPort
*i2cPort
if
([i2cPort
isOnline]) {
[i2cPort set_i2cMode:@"400kbps"];
[i2cPort set_i2cVoltageLevel:Y_I2CVOLTAGELEVEL_3V3];
[i2cPort reset];
NSLog(@"****************************");
NSLog(@"* make sure voltage levels
NSLog(@"* are properly configured
NSLog(@"****************************");
NSMutableArray
[toSend
addObject:[NSNumber
NSMutableArray
short int
tempReg
[[received
if
(tempReg
tempReg
|=
} else {
tempReg
&=
}
NSLog(@"Current temperature: %f
} else {
NSLog(@"Module not connected (check identification and USB
}
[YAPI
FreeAPI];
}
return
0;
www.yoctopuce.com
<serial_number>");
<logical_name>");
(use any discovered
const char
* argv[])
%@", [error localizedDescription]);
=
[NSString
stringWithUTF8String:argv[1]];
=
[YI2cPort
FirstI2cPort];
== NULL) {
module]
serialNumber];
=
[YI2cPort
FindI2cPort:
[target stringByAppendingString:@".i2cPort"]];
*toSend
=
[NSMutableArray
numberWithUnsignedChar:0x05]];
*received
=
[i2cPort i2cSendAndReceiveArray:0x1f :toSend :2];
=
([[received
objectAtIndex:0] unsignedCharValue]
objectAtIndex:1]
& 0x1000) {
0xf000;
// perform sign extension
0x0fff;
// clear status bits
'C\n", tempReg
21. Utilisation du Yocto-I2C en Objective-C
device)");
cable)");
*");
*");
arrayWithCapacity:1];
unsignedCharValue];
/
16.0);
<<
8) +
cable)\n");
171

Publicité

Table des Matières
loading

Table des Matières