/*********************************************************************
 *
 *  $Id: main.m 72776 2026-04-17 08:24:26Z seb $
 *
 *  An example that shows how to use a  Yocto-PowerColor
 *
 *  You can find more information on our web site:
 *   Yocto-PowerColor documentation:
 *      https://www.yoctopuce.com/EN/products/yocto-powercolor/doc.html
 *   Objective-C API Reference:
 *      https://www.yoctopuce.com/EN/doc/reference/yoctolib-objc-EN.html
 *
 *********************************************************************/

#import <Foundation/Foundation.h>
#import "yocto_api.h"
#import "yocto_colorled.h"

static void usage(void)
{
  NSLog(@"usage: demo <serial_number>  [ color | rgb ]");
  NSLog(@"       demo <logical_name> [ color | rgb ]");
  NSLog(@"       demo any  [ color | rgb ]                (use any discovered device)");
  NSLog(@"Eg.");
  NSLog(@"   demo any FF1493 ");
  NSLog(@"   demo YRGBHI01-123456 red");
  exit(1);
}

int main(int argc, const char * argv[])
{
  NSError * error;

  if(argc < 3) {
    usage();
  }

  @autoreleasepool {
    // Setup the API to use local USB devices
    if([YAPI RegisterHub:@"usb": &error] != YAPI_SUCCESS) {
      NSLog(@"RegisterHub error: %@", [error localizedDescription]);
      return 1;
    }
    NSString *target    = [NSString stringWithUTF8String:argv[1]];
    NSString *color_str = [NSString stringWithUTF8String:argv[2]];
    if ([target isEqualToString:@"any"]) {
      YColorLed *colorLed = [YColorLed FirstColorLed];
      if (colorLed == NULL) {
        NSLog(@"No Yocto-Color connected (check USB cable)");
        return 1;
      }
      target = [[colorLed module] serialNumber];
    }

    YColorLed *led1 =  [YColorLed FindColorLed:[target stringByAppendingString:
                        @".colorLed1"]];

    unsigned color;
    if ([color_str isEqualToString:@"red"])
      color = 0xFF0000;
    else if ([color_str isEqualToString:@"green"])
      color = 0x00FF00;
    else if ([color_str isEqualToString:@"blue"])
      color = 0x0000FF;
    else
      color = (int)strtoul([color_str UTF8String], NULL, 16);

    if ([led1 isOnline]) {
      [led1 rgbMove:color:1000];  // smooth transition
    } else {
      NSLog(@"Module not connected (check identification and USB cable)\n");
    }
    [YAPI FreeAPI];
  }

  return 0;
}
