00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <OPENR/OPENRAPI.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/core_macro.h>
00015 #include "BlinkingLED.h"
00016
00017 BlinkingLED::BlinkingLED() : blinkingLEDState(BLS_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 BlinkingLED::DoInit(const OSystemEvent& event)
00025 {
00026 OSYSDEBUG(("BlinkingLED::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 BlinkingLED::DoStart(const OSystemEvent& event)
00042 {
00043 OSYSDEBUG(("BlinkingLED::DoStart()\n"));
00044
00045 BlinkLED();
00046 blinkingLEDState = BLS_START;
00047
00048 ENABLE_ALL_SUBJECT;
00049 ASSERT_READY_TO_ALL_OBSERVER;
00050
00051 return oSUCCESS;
00052 }
00053
00054 OStatus
00055 BlinkingLED::DoStop(const OSystemEvent& event)
00056 {
00057 OSYSDEBUG(("BlinkingLED::DoStop()\n"));
00058
00059 blinkingLEDState = BLS_IDLE;
00060
00061 DISABLE_ALL_SUBJECT;
00062 DEASSERT_READY_TO_ALL_OBSERVER;
00063
00064 return oSUCCESS;
00065 }
00066
00067 OStatus
00068 BlinkingLED::DoDestroy(const OSystemEvent& event)
00069 {
00070 DELETE_ALL_SUBJECT_AND_OBSERVER;
00071 return oSUCCESS;
00072 }
00073
00074 void
00075 BlinkingLED::Ready(const OReadyEvent& event)
00076 {
00077 OSYSDEBUG(("BlinkingLED::Ready()\n"));
00078
00079 if (blinkingLEDState == BLS_START) {
00080 OSYSDEBUG(("BLS_START\n"));
00081 BlinkLED();
00082 }
00083 }
00084
00085 void
00086 BlinkingLED::OpenPrimitives()
00087 {
00088 for (int i = 0; i < NUM_LEDS; i++) {
00089 OStatus result = OPENR::OpenPrimitive(LED_LOCATOR[i], &ledID[i]);
00090 if (result != oSUCCESS) {
00091 OSYSLOG1((osyslogERROR, "%s : %s %d",
00092 "BlinkingLED::OpenPrimitives()",
00093 "OPENR::OpenPrimitive() FAILED", result));
00094 }
00095 }
00096 }
00097
00098 void
00099 BlinkingLED::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 "BlinkingLED::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 for (int j = 0; j < NUM_LEDS; j++) {
00123 OCommandInfo* info = cmdVecData->GetInfo(j);
00124 info->Set(odataLED_COMMAND2, ledID[j], 1);
00125
00126 OCommandData* data = cmdVecData->GetData(j);
00127 OLEDCommandValue2* val = (OLEDCommandValue2*)data->value;
00128 if (i % 2 == 0) {
00129 val[0].led = (j % 2 == 0) ? oledON : oledOFF;
00130 } else {
00131 val[0].led = (j % 2 == 0) ? oledOFF : oledON;
00132 }
00133 val[0].period = 64;
00134 OSYSDEBUG(("ledID[%d] %d\n", j, val[0].led));
00135 }
00136 }
00137 }
00138
00139 void
00140 BlinkingLED::BlinkLED()
00141 {
00142 static int index = -1;
00143
00144 if (index == -1) {
00145 index = 0;
00146 subject[sbjBlink]->SetData(region[index]);
00147 index++;
00148 }
00149
00150 subject[sbjBlink]->SetData(region[index]);
00151 subject[sbjBlink]->NotifyObservers();
00152
00153 index++;
00154 index = index % NUM_COMMAND_VECTOR;
00155 OSYSDEBUG(("index %d\n", index));
00156 }