BlinkingHeadLED.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/core_macro.h>
00015 #include "BlinkingHeadLED.h"
00016 
00017 BlinkingHeadLED::BlinkingHeadLED() : blinkingLEDState(BHLS_IDLE)
00018 {
00019     for (int i = 0; i < NUM_LEDS; i++) ledID[i] = oprimitiveID_UNDEF;
00020     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) region[i] = 0;
00021 }
00022 
00023 OStatus
00024 BlinkingHeadLED::DoInit(const OSystemEvent& event)
00025 {
00026     OSYSDEBUG(("BlinkingHeadLED::DoInit()\n"));
00027 
00028     NEW_ALL_SUBJECT_AND_OBSERVER;
00029     REGISTER_ALL_ENTRY;
00030     SET_ALL_READY_AND_NOTIFY_ENTRY;
00031 
00032     OpenPrimitives();
00033     NewCommandVectorData();
00034     OStatus st = OPENR::SetMotorPower(opowerON);
00035     OSYSDEBUG(("OPENR::SetMotorPower(opowerON) %d\n", st));
00036     
00037     return oSUCCESS;
00038 }
00039 
00040 OStatus
00041 BlinkingHeadLED::DoStart(const OSystemEvent& event)
00042 {
00043     OSYSDEBUG(("BlinkingHeadLED::DoStart()\n"));
00044 
00045     BlinkLED();
00046     blinkingLEDState = BHLS_START;
00047 
00048     ENABLE_ALL_SUBJECT;
00049     ASSERT_READY_TO_ALL_OBSERVER;
00050 
00051     return oSUCCESS;
00052 }
00053 
00054 OStatus
00055 BlinkingHeadLED::DoStop(const OSystemEvent& event)
00056 {
00057     OSYSDEBUG(("BlinkingHeadLED::DoStop()\n"));
00058 
00059     blinkingLEDState = BHLS_IDLE;
00060 
00061     DISABLE_ALL_SUBJECT;    
00062     DEASSERT_READY_TO_ALL_OBSERVER;
00063 
00064     return oSUCCESS;
00065 }
00066 
00067 OStatus
00068 BlinkingHeadLED::DoDestroy(const OSystemEvent& event)
00069 {
00070     DELETE_ALL_SUBJECT_AND_OBSERVER;
00071     return oSUCCESS;
00072 }
00073 
00074 void
00075 BlinkingHeadLED::Ready(const OReadyEvent& event)
00076 {
00077     OSYSDEBUG(("BlinkingHeadLED::Ready()\n"));
00078 
00079     if (blinkingLEDState == BHLS_START) {
00080         OSYSDEBUG(("BHLS_START\n"));
00081         BlinkLED();
00082     }
00083 }
00084 
00085 void
00086 BlinkingHeadLED::OpenPrimitives()
00087 {
00088     for (int i = 0; i < NUM_LEDS; i++) {
00089         OStatus result = OPENR::OpenPrimitive(LED2_LOCATOR[i], &ledID[i]);
00090         if (result != oSUCCESS) {
00091             OSYSLOG1((osyslogERROR, "%s : %s %d",
00092                       "BlinkingHeadLED::OpenPrimitives()",
00093                       "OPENR::OpenPrimitive() FAILED", result));
00094         }
00095     }
00096 }
00097 
00098 void
00099 BlinkingHeadLED::NewCommandVectorData()
00100 {
00101     OStatus result;
00102     MemoryRegionID      cmdVecDataID;
00103     OCommandVectorData* cmdVecData;
00104 
00105     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00106 
00107         result = OPENR::NewCommandVectorData(NUM_LEDS, 
00108                                              &cmdVecDataID, &cmdVecData);
00109         if (result != oSUCCESS) {
00110             OSYSLOG1((osyslogERROR, "%s : %s %d",
00111                       "BlinkingHeadLED::NewCommandVectorData()",
00112                       "OPENR::NewCommandVectorData() FAILED", result));
00113         }
00114 
00115         region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00116                                  cmdVecData->vectorInfo.offset,
00117                                  (void*)cmdVecData,
00118                                  cmdVecData->vectorInfo.totalSize);
00119 
00120         cmdVecData->SetNumData(NUM_LEDS);
00121 
00122 
00123         for (int j = 0; j < NUM_LEDS; j++) {
00124             OCommandInfo* info = cmdVecData->GetInfo(j);
00125             info->Set(odataLED_COMMAND2, ledID[j], NUM_FRAMES);
00126 
00127             OCommandData* data = cmdVecData->GetData(j);
00128             OLEDCommandValue2* val = (OLEDCommandValue2*)data->value;
00129             for (int k = 0; k < NUM_FRAMES; k++) {
00130                 val[k].led    = LED2_VALUE[j][k];
00131                 val[k].period = 64; // 8ms * 64 = 512ms
00132             }
00133         }
00134     }
00135 }
00136 
00137 void
00138 BlinkingHeadLED::BlinkLED()
00139 {
00140     static int index = -1;
00141 
00142     if (index == -1) { // BlinkLED() is called first time.
00143         index = 0;
00144         subject[sbjBlink]->SetData(region[index]);
00145         index++;
00146     } 
00147 
00148     subject[sbjBlink]->SetData(region[index]);
00149     subject[sbjBlink]->NotifyObservers();
00150 
00151     index++;
00152     index = index % NUM_COMMAND_VECTOR;
00153     OSYSDEBUG(("index %d\n", index));
00154 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1