BlinkingBackLED.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 "BlinkingBackLED.h"
00016 
00017 BlinkingBackLED::BlinkingBackLED() : blinkingLEDState(BBLS_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 BlinkingBackLED::DoInit(const OSystemEvent& event)
00025 {
00026     OSYSDEBUG(("BlinkingBackLED::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 BlinkingBackLED::DoStart(const OSystemEvent& event)
00042 {
00043     OSYSDEBUG(("BlinkingBackLED::DoStart()\n"));
00044 
00045     if (subject[sbjBlink]->IsReady() == true) {
00046         blinkingLEDState = BBLS_WHITE0to255_COLOR0;
00047         White0to255_Color0();
00048     } else {
00049         blinkingLEDState = BBLS_START;
00050     }
00051 
00052     ENABLE_ALL_SUBJECT;
00053     ASSERT_READY_TO_ALL_OBSERVER;
00054 
00055     return oSUCCESS;
00056 }
00057 
00058 OStatus
00059 BlinkingBackLED::DoStop(const OSystemEvent& event)
00060 {
00061     OSYSDEBUG(("BlinkingBackLED::DoStop()\n"));
00062 
00063     blinkingLEDState = BBLS_IDLE;
00064 
00065     DISABLE_ALL_SUBJECT;    
00066     DEASSERT_READY_TO_ALL_OBSERVER;
00067 
00068     return oSUCCESS;
00069 }
00070 
00071 OStatus
00072 BlinkingBackLED::DoDestroy(const OSystemEvent& event)
00073 {
00074     DELETE_ALL_SUBJECT_AND_OBSERVER;
00075     return oSUCCESS;
00076 }
00077 
00078 void
00079 BlinkingBackLED::Ready(const OReadyEvent& event)
00080 {
00081     OSYSDEBUG(("BlinkingBackLED::Ready()\n"));
00082 
00083     if (blinkingLEDState == BBLS_IDLE) {
00084 
00085         OSYSDEBUG(("BBLS_IDLE\n"));
00086         ; // do nothing
00087 
00088     } else if (blinkingLEDState == BBLS_START) {
00089 
00090         OSYSDEBUG(("BBLS_START\n"));
00091         blinkingLEDState = BBLS_WHITE0to255_COLOR0;
00092         White0to255_Color0();
00093 
00094     } else if (blinkingLEDState == BBLS_WHITE0to255_COLOR0) {
00095 
00096         OSYSDEBUG(("BBLS_WHITE0to255_COLOR0\n"));
00097         LED3Result r = White0to255_Color0();
00098         if (r == LED3_FINISH) {
00099             blinkingLEDState = BBLS_WHITE255_COLOR0to255;
00100         }
00101 
00102     } else if (blinkingLEDState == BBLS_WHITE255_COLOR0to255) {
00103 
00104         OSYSDEBUG(("BBLS_WHITE255_COLOR0to255\n"));
00105         LED3Result r = White255_Color0to255();
00106         if (r == LED3_FINISH) {
00107             blinkingLEDState = BBLS_WHITE255to0_COLOR255;
00108         }
00109 
00110     } else if (blinkingLEDState == BBLS_WHITE255to0_COLOR255) {
00111 
00112         OSYSDEBUG(("BBLS_WHITE255to0_COLOR255\n"));
00113         LED3Result r = White255to0_Color255();
00114         if (r == LED3_FINISH) {
00115             blinkingLEDState = BBLS_WHITE0_COLOR255to0;
00116         }
00117 
00118     } else if (blinkingLEDState == BBLS_WHITE0_COLOR255to0) {
00119 
00120         OSYSDEBUG(("BBLS_WHITE0_COLOR255to0\n"));
00121         LED3Result r = White0_Color255to0();
00122         if (r == LED3_FINISH) {
00123             blinkingLEDState = BBLS_WHITE0_COLOR0;
00124         }
00125 
00126     } else { // blinkingLEDState == BBLS_WHITE0_COLOR0
00127 
00128         OSYSDEBUG(("BBLS_WHITE0_COLOR0\n"));
00129         LED3Result r = White0_Color0();
00130         if (r == LED3_FINISH) {
00131             blinkingLEDState = BBLS_WHITE0to255_COLOR0;
00132         }
00133 
00134     }
00135 }
00136 
00137 void
00138 BlinkingBackLED::OpenPrimitives()
00139 {
00140     for (int i = 0; i < NUM_LEDS; i++) {
00141         OStatus result = OPENR::OpenPrimitive(LED3_LOCATOR[i], &ledID[i]);
00142         if (result != oSUCCESS) {
00143             OSYSLOG1((osyslogERROR, "%s : %s %d",
00144                       "BlinkingBackLED::OpenPrimitives()",
00145                       "OPENR::OpenPrimitive() FAILED", result));
00146         }
00147     }
00148 }
00149 
00150 void
00151 BlinkingBackLED::NewCommandVectorData()
00152 {
00153     OStatus result;
00154     MemoryRegionID      cmdVecDataID;
00155     OCommandVectorData* cmdVecData;
00156 
00157     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00158 
00159         result = OPENR::NewCommandVectorData(NUM_LEDS, 
00160                                              &cmdVecDataID, &cmdVecData);
00161         if (result != oSUCCESS) {
00162             OSYSLOG1((osyslogERROR, "%s : %s %d",
00163                       "BlinkingBackLED::NewCommandVectorData()",
00164                       "OPENR::NewCommandVectorData() FAILED", result));
00165         }
00166 
00167         region[i] = new RCRegion(cmdVecData->vectorInfo.memRegionID,
00168                                  cmdVecData->vectorInfo.offset,
00169                                  (void*)cmdVecData,
00170                                  cmdVecData->vectorInfo.totalSize);
00171 
00172         cmdVecData->SetNumData(NUM_LEDS);
00173 
00174         for (int j = 0; j < NUM_LEDS; j++) {
00175             OCommandInfo* info = cmdVecData->GetInfo(j);
00176             info->Set(odataLED_COMMAND3, ledID[j], ocommandMAX_FRAMES);
00177         }
00178     }
00179 }
00180 
00181 LED3Result
00182 BlinkingBackLED::White0to255_Color0()
00183 {
00184     static int level = -1;
00185 
00186     if (level == -1) {
00187         level = 0;
00188         RCRegion* rgn = FindFreeRegion();
00189         SetWhiteLED3Value(rgn, level, POSITIVE_SLOPE);
00190         SetColorLED3Value(rgn, 0);
00191         subject[sbjBlink]->SetData(rgn);
00192         level++;
00193     } 
00194 
00195     RCRegion* rgn = FindFreeRegion();
00196     SetWhiteLED3Value(rgn, level, POSITIVE_SLOPE);
00197     SetColorLED3Value(rgn, 0);
00198     subject[sbjBlink]->SetData(rgn);
00199     subject[sbjBlink]->NotifyObservers();
00200     level++;
00201 
00202     if (level > MAX_LEVEL) {
00203         level = 0;
00204         return LED3_FINISH;
00205     } else {
00206         return LED3_CONT;
00207     }
00208 }
00209 
00210 LED3Result
00211 BlinkingBackLED::White255_Color0to255()
00212 {
00213     static int level = 0;
00214 
00215     RCRegion* rgn = FindFreeRegion();
00216     SetWhiteLED3Value(rgn, 255);
00217     SetColorLED3Value(rgn, level, POSITIVE_SLOPE);
00218     subject[sbjBlink]->SetData(rgn);
00219     subject[sbjBlink]->NotifyObservers();
00220     level++;
00221 
00222     if (level > MAX_LEVEL) {
00223         level = 0;
00224         return LED3_FINISH;
00225     } else {
00226         return LED3_CONT;
00227     }
00228 }
00229 
00230 LED3Result
00231 BlinkingBackLED::White255to0_Color255()
00232 {
00233     static int level = MAX_LEVEL;
00234 
00235     RCRegion* rgn = FindFreeRegion();
00236     SetWhiteLED3Value(rgn, level, NEGATIVE_SLOPE);
00237     SetColorLED3Value(rgn, 255);
00238     subject[sbjBlink]->SetData(rgn);
00239     subject[sbjBlink]->NotifyObservers();
00240     level--;
00241 
00242     if (level < 0) {
00243         level = MAX_LEVEL;
00244         return LED3_FINISH;
00245     } else {
00246         return LED3_CONT;
00247     }
00248 }
00249 
00250 LED3Result
00251 BlinkingBackLED::White0_Color255to0()
00252 {
00253     static int level = MAX_LEVEL;
00254 
00255     RCRegion* rgn = FindFreeRegion();
00256     SetWhiteLED3Value(rgn, 0);
00257     SetColorLED3Value(rgn, level, NEGATIVE_SLOPE);
00258     subject[sbjBlink]->SetData(rgn);
00259     subject[sbjBlink]->NotifyObservers();
00260     level--;
00261 
00262     if (level < 0) {
00263         level = MAX_LEVEL;
00264         return LED3_FINISH;
00265     } else {
00266         return LED3_CONT;
00267     }
00268 }
00269 
00270 LED3Result
00271 BlinkingBackLED::White0_Color0()
00272 {
00273     static int level = 0;
00274 
00275     RCRegion* rgn = FindFreeRegion();
00276     SetWhiteLED3Value(rgn, 0);
00277     SetColorLED3Value(rgn, 0);
00278     subject[sbjBlink]->SetData(rgn);
00279     subject[sbjBlink]->NotifyObservers();
00280     level++;
00281 
00282     if (level > MAX_LEVEL) {
00283         level = 0;
00284         return LED3_FINISH;
00285     } else {
00286         return LED3_CONT;
00287     }
00288 }
00289 
00290 void
00291 BlinkingBackLED::SetWhiteLED3Value(RCRegion* rgn, int level, int slope)
00292 {
00293     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00294 
00295     for (int i = 0; i < NUM_LEDS/2; i++) {
00296         OCommandData* wdata = cmdVecData->GetData(2*i+1);
00297         OLEDCommandValue3* wval3 = (OLEDCommandValue3*)wdata->value;
00298         for (int j = 0; j < ocommandMAX_FRAMES; j++) {
00299             if (slope == POSITIVE_SLOPE) {
00300                 wval3[j].intensity = 16 * level + j;
00301             } else {
00302                 wval3[j].intensity = 16 * level + 15 - j;
00303             }
00304             wval3[j].mode      = oled3_MODE_UNDEF;
00305             wval3[j].period    = 1;
00306         }
00307     }
00308 }
00309 
00310 void
00311 BlinkingBackLED::SetColorLED3Value(RCRegion* rgn, int level, int slope)
00312 {
00313     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00314 
00315     for (int i = 0; i < NUM_LEDS/2; i++) {
00316         OCommandData* cdata = cmdVecData->GetData(2*i);
00317         OLEDCommandValue3* cval3 = (OLEDCommandValue3*)cdata->value;
00318         for (int j = 0; j < ocommandMAX_FRAMES; j++) {
00319             if (slope == POSITIVE_SLOPE) {
00320                 cval3[j].intensity = 16 * level + j;                
00321             } else {
00322                 cval3[j].intensity = 16 * level + 15 - j;
00323             }
00324             cval3[j].mode      = oled3_MODE_UNDEF;
00325             cval3[j].period    = 1;
00326         }
00327     }
00328 }
00329 
00330 void
00331 BlinkingBackLED::SetWhiteLED3Value(RCRegion* rgn, sword intensity)
00332 {
00333     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00334 
00335     for (int i = 0; i < NUM_LEDS/2; i++) {
00336         OCommandData* wdata = cmdVecData->GetData(2*i+1);
00337         OLEDCommandValue3* wval3 = (OLEDCommandValue3*)wdata->value;
00338         for (int j = 0; j < ocommandMAX_FRAMES; j++) {
00339             wval3[j].intensity = intensity;
00340             wval3[j].mode      = oled3_MODE_UNDEF;
00341             wval3[j].period    = 1;
00342         }
00343     }
00344 }
00345 
00346 void
00347 BlinkingBackLED::SetColorLED3Value(RCRegion* rgn, sword intensity)
00348 {
00349     OCommandVectorData* cmdVecData = (OCommandVectorData*)rgn->Base();
00350 
00351     for (int i = 0; i < NUM_LEDS/2; i++) {
00352         OCommandData* cdata = cmdVecData->GetData(2*i);
00353         OLEDCommandValue3* cval3 = (OLEDCommandValue3*)cdata->value;
00354         for (int j = 0; j < ocommandMAX_FRAMES; j++) {
00355             cval3[j].intensity = intensity;
00356             cval3[j].mode      = oled3_MODE_UNDEF;
00357             cval3[j].period    = 1;
00358         }
00359     }
00360 }
00361 
00362 RCRegion*
00363 BlinkingBackLED::FindFreeRegion()
00364 {
00365     for (int i = 0; i < NUM_COMMAND_VECTOR; i++) {
00366         if (region[i]->NumberOfReference() == 1) return region[i];
00367     }
00368 
00369     return 0;
00370 }

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