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 "LostFoundSound.h"
00016
00017 LostFoundSound::LostFoundSound() : lfsoundState(LFSS_IDLE),
00018 speakerID(oprimitiveID_UNDEF),
00019 foundSoundID(odesigndataID_UNDEF),
00020 foundWAV(),
00021 lostSoundID(odesigndataID_UNDEF),
00022 lostWAV(), playingWAV()
00023 {
00024 for (int i = 0; i < SOUND_NUM_BUFFER; i++) region[i] = 0;
00025 }
00026
00027 OStatus
00028 LostFoundSound::DoInit(const OSystemEvent& event)
00029 {
00030 OSYSDEBUG(("LostFoundSound::DoInit()\n"));
00031
00032 NEW_ALL_SUBJECT_AND_OBSERVER;
00033 REGISTER_ALL_ENTRY;
00034 SET_ALL_READY_AND_NOTIFY_ENTRY;
00035
00036 OpenSpeaker();
00037 NewSoundVectorData();
00038 LoadWAV();
00039 SetPowerAndVolume();
00040
00041 return oSUCCESS;
00042 }
00043
00044 OStatus
00045 LostFoundSound::DoStart(const OSystemEvent& event)
00046 {
00047 OSYSDEBUG(("LostFoundSound::DoStart()\n"));
00048
00049 lfsoundState = LFSS_START;
00050
00051 ENABLE_ALL_SUBJECT;
00052 ASSERT_READY_TO_ALL_OBSERVER;
00053
00054 return oSUCCESS;
00055 }
00056
00057 OStatus
00058 LostFoundSound::DoStop(const OSystemEvent& event)
00059 {
00060 OSYSDEBUG(("LostFoundSound::DoStop()\n"));
00061
00062 lfsoundState = LFSS_IDLE;
00063
00064 DISABLE_ALL_SUBJECT;
00065 DEASSERT_READY_TO_ALL_OBSERVER;
00066
00067 return oSUCCESS;
00068 }
00069
00070 OStatus
00071 LostFoundSound::DoDestroy(const OSystemEvent& event)
00072 {
00073 OPENR::DeleteDesignData(foundSoundID);
00074 OPENR::DeleteDesignData(lostSoundID);
00075 DELETE_ALL_SUBJECT_AND_OBSERVER;
00076 return oSUCCESS;
00077 }
00078
00079 void
00080 LostFoundSound::NotifyCommand(const ONotifyEvent& event)
00081 {
00082 OSYSDEBUG(("LostFoundSound::NotifyCommand()\n"));
00083
00084 if (lfsoundState != LFSS_START) {
00085 BallTrackingHeadResult result(BTH_BUSY);
00086 subject[sbjResult]->SetData(&result, sizeof(result));
00087 subject[sbjResult]->NotifyObservers();
00088 observer[event.ObsIndex()]->AssertReady();
00089 return;
00090 }
00091
00092 BallTrackingHeadCommand* cmd = (BallTrackingHeadCommand*)event.Data(0);
00093 if (cmd->type == BTHCMD_PLAY_FOUND_SOUND ||
00094 cmd->type == BTHCMD_PLAY_LOST_SOUND) {
00095
00096 Play(cmd);
00097 lfsoundState = LFSS_PLAYING;
00098
00099 } else {
00100
00101 BallTrackingHeadResult result(BTH_INVALID_ARG);
00102 subject[sbjResult]->SetData(&result, sizeof(result));
00103 subject[sbjResult]->NotifyObservers();
00104
00105 }
00106
00107 observer[event.ObsIndex()]->AssertReady();
00108 }
00109
00110 void
00111 LostFoundSound::ReadyPlay(const OReadyEvent& event)
00112 {
00113 OSYSDEBUG(("LostFoundSound::ReadyPlay()\n"));
00114
00115 if (lfsoundState == LFSS_PLAYING) {
00116
00117 RCRegion* rgn = FindFreeRegion();
00118 if (CopyWAVTo(rgn) == WAV_SUCCESS) {
00119 subject[sbjPlay]->SetData(rgn);
00120 subject[sbjPlay]->NotifyObservers();
00121 } else {
00122 if (IsAllRegionFree() == true) {
00123 BallTrackingHeadResult result(BTH_SUCCESS);
00124 subject[sbjResult]->SetData(&result, sizeof(result));
00125 subject[sbjResult]->NotifyObservers();
00126 lfsoundState = LFSS_START;
00127 }
00128 }
00129
00130 }
00131 }
00132
00133 void
00134 LostFoundSound::Play(BallTrackingHeadCommand* cmd)
00135 {
00136 OSYSDEBUG(("LostFoundSound::Play()\n"));
00137
00138 if (cmd->type == BTHCMD_PLAY_FOUND_SOUND) {
00139 playingWAV = &foundWAV;
00140 } else {
00141 playingWAV = &lostWAV;
00142 }
00143 playingWAV->Rewind();
00144
00145 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00146 if (CopyWAVTo(region[i]) == WAV_SUCCESS)
00147 subject[sbjPlay]->SetData(region[i]);
00148 }
00149 subject[sbjPlay]->NotifyObservers();
00150 }
00151
00152 void
00153 LostFoundSound::OpenSpeaker()
00154 {
00155 OStatus result = OPENR::OpenPrimitive(SPEAKER_LOCATOR, &speakerID);
00156 if (result != oSUCCESS) {
00157 OSYSLOG1((osyslogERROR, "%s : %s %d",
00158 "LostFoundSound::OpenSpeaker()",
00159 "OPENR::OpenPrimitive() FAILED", result));
00160 }
00161 }
00162
00163 void
00164 LostFoundSound::NewSoundVectorData()
00165 {
00166 OStatus result;
00167 MemoryRegionID soundVecDataID;
00168 OSoundVectorData* soundVecData;
00169
00170 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00171
00172 result = OPENR::NewSoundVectorData(1, SOUND_UNIT_SIZE,
00173 &soundVecDataID, &soundVecData);
00174 if (result != oSUCCESS) {
00175 OSYSLOG1((osyslogERROR, "%s : %s %d",
00176 "LostFoundSound::NewSoundVectorData()",
00177 "OPENR::NewSoundVectorData() FAILED", result));
00178 return;
00179 }
00180
00181 soundVecData->SetNumData(1);
00182 soundVecData->GetInfo(0)->Set(odataSOUND_VECTOR,
00183 speakerID, SOUND_UNIT_SIZE);
00184
00185 region[i] = new RCRegion(soundVecData->vectorInfo.memRegionID,
00186 soundVecData->vectorInfo.offset,
00187 (void*)soundVecData,
00188 soundVecData->vectorInfo.totalSize);
00189 }
00190 }
00191
00192 void
00193 LostFoundSound::LoadWAV()
00194 {
00195 OStatus result;
00196 size_t size;
00197 byte* addr;
00198
00199 result = OPENR::FindDesignData(FOUND_SOUND,
00200 &foundSoundID, &addr, &size);
00201 if (result != oSUCCESS) {
00202 OSYSLOG1((osyslogERROR, "%s : %s %d",
00203 "LostFoundSound::LoadWAV()",
00204 "OPENR::FindDesignData() FAILED", result));
00205 }
00206
00207 foundWAV.Set(addr);
00208
00209 result = OPENR::FindDesignData(LOST_SOUND,
00210 &lostSoundID, &addr, &size);
00211 if (result != oSUCCESS) {
00212 OSYSLOG1((osyslogERROR, "%s : %s %d",
00213 "LostFoundSound::LoadWAV()",
00214 "OPENR::FindDesignData() FAILED", result));
00215 }
00216
00217 lostWAV.Set(addr);
00218 }
00219
00220 void
00221 LostFoundSound::SetPowerAndVolume()
00222 {
00223 OStatus result;
00224
00225 result = OPENR::ControlPrimitive(speakerID,
00226 oprmreqSPEAKER_MUTE_ON, 0, 0, 0, 0);
00227 if (result != oSUCCESS) {
00228 OSYSLOG1((osyslogERROR, "%s : %s %d",
00229 "LostFoundSound::SetPowerAndVolume()",
00230 "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_ON) FAILED",
00231 result));
00232 }
00233
00234 result = OPENR::SetMotorPower(opowerON);
00235 if (result != oSUCCESS) {
00236 OSYSLOG1((osyslogERROR, "%s : %s %d",
00237 "LostFoundSound::SetPowerAndVolume()",
00238 "OPENR::SetMotorPower() FAILED", result));
00239 }
00240
00241 result = OPENR::ControlPrimitive(speakerID,
00242 oprmreqSPEAKER_MUTE_OFF, 0, 0, 0, 0);
00243 if (result != oSUCCESS) {
00244 OSYSLOG1((osyslogERROR, "%s : %s %d",
00245 "LostFoundSound::SetPowerAndVolume()",
00246 "OPENR::ControlPrimitive(oprmreqSPEAKER_MUTE_OFF) FAILED",
00247 result));
00248 }
00249
00250 OPrimitiveControl_SpeakerVolume volume(ospkvol10dB);
00251 result = OPENR::ControlPrimitive(speakerID,
00252 oprmreqSPEAKER_SET_VOLUME,
00253 &volume, sizeof(volume), 0, 0);
00254 if (result != oSUCCESS) {
00255 OSYSLOG1((osyslogERROR, "%s : %s %d",
00256 "LostFoundSound::SetPowerAndVolume()",
00257 "OPENR::ControlPrimitive(oprmreqSPEAKER_SET_VOLUME) FAILED",
00258 result));
00259 }
00260 }
00261
00262 WAVError
00263 LostFoundSound::CopyWAVTo(RCRegion* rgn)
00264 {
00265 OSoundVectorData* soundVecData = (OSoundVectorData*)rgn->Base();
00266 return playingWAV->CopyTo(soundVecData);
00267 }
00268
00269 RCRegion*
00270 LostFoundSound::FindFreeRegion()
00271 {
00272 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00273 if (region[i]->NumberOfReference() == 1) return region[i];
00274 }
00275
00276 return 0;
00277 }
00278
00279 bool
00280 LostFoundSound::IsAllRegionFree()
00281 {
00282 for (int i = 0; i < SOUND_NUM_BUFFER; i++) {
00283 if (region[i]->NumberOfReference() > 1) return false;
00284 }
00285
00286 return true;
00287 }