libGizmod/GizmoPowermate.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "GizmoPowermate.hpp"
00030 #include "GizmoEventPowermate.hpp"
00031 #include "GizmoKeyDefs.hpp"
00032 #include "../libH/Debug.hpp"
00033 #include "../libH/Exception.hpp"
00034 #include "../libH/UtilTime.hpp"
00035 #include <boost/shared_ptr.hpp>
00036 #include <errno.h>
00037 
00038 using namespace std;
00039 using namespace boost;
00040 using namespace H;
00041 using namespace Gizmod;
00042 
00044 // Type Defs
00046 
00051 #define GIZMO_POWERMATE_TYPE    "Powermate"
00052 
00054 // Construction
00056 
00060 GizmoPowermate::GizmoPowermate(const H::DeviceInfo & deviceInfo, int DeviceID, int DeviceClassID) : Gizmo(GIZMO_CLASS_POWERMATE, deviceInfo, DeviceID, DeviceClassID), GizmoLinuxInputDevice(deviceInfo) {
00061         mPulseAsleep = true;
00062         mRotateCurDir = -1;
00063         mRotateCurTick = 0;
00064         mRotated = false;
00065         mRotateTicksPerEvent = 1;
00066 }
00067 
00071 GizmoPowermate::GizmoPowermate() {
00072         mPulseAsleep = true;
00073         mRotateCurDir = -1;
00074         mRotateCurTick = 0;
00075         mRotated = false;
00076         mRotateTicksPerEvent = 1;
00077 }
00078 
00082 GizmoPowermate::~GizmoPowermate() {
00083 }
00084 
00086 // Class Body
00088 
00101 void GizmoPowermate::changeLEDState(int StaticBrightness, int PulseSpeed, int PulseTable, int PulseAsleep, int PulseAwake) {
00102         struct input_event ev;
00103         memset(&ev, 0, sizeof(struct input_event));
00104         
00105         StaticBrightness &= 0xFF;
00106 
00107         if (PulseSpeed < 0)
00108                 PulseSpeed = 0;
00109         else if (PulseSpeed > 510)
00110                 PulseSpeed = 510;
00111         if (PulseTable < 0)
00112                 PulseTable = 0;
00113         else if (PulseTable > 2)
00114                 PulseTable = 2;
00115         PulseAsleep = !!PulseAsleep;
00116         PulseAwake = !!PulseAwake;
00117 
00118         ev.type = EV_MSC;
00119         ev.code = MSC_PULSELED;
00120         ev.value = StaticBrightness | (PulseSpeed << 8) | (PulseTable << 17) | (PulseAsleep << 19) | (PulseAwake << 20);
00121 
00122         if (write(mDeviceInfo.FileDescriptor, &ev, sizeof(struct input_event)) != sizeof(struct input_event))
00123                 cdbg << "Failed to Write to Griffin PowerMate [" << strerror(errno) << "]" << endl;
00124 }
00125 
00130 unsigned char GizmoPowermate::getLED() {
00131         return mLevel;
00132 }
00133 
00138 float GizmoPowermate::getLEDPercent() {
00139         return ((float) mLevel / 255.0f) * 100.0f;
00140 }
00141 
00146 bool GizmoPowermate::getLEDPulseAsleep() {
00147         return mPulseAsleep;
00148 } 
00149 
00154 std::string GizmoPowermate::getType() {
00155         return GIZMO_POWERMATE_TYPE;
00156 }
00157 
00162 bool GizmoPowermate::getRotated() {
00163         return mRotated;
00164 }
00165 
00171 bool GizmoPowermate::processEvent(GizmoEvent * pEvent) {
00172         if (!GizmoLinuxInputDevice::processEvent())
00173                 return false;
00174         
00175         GizmoEventPowermate * pPowermateEvent = static_cast<GizmoEventPowermate *>(pEvent);
00176         
00177         switch (pPowermateEvent->Type) {
00178         case EV_REL:
00179                 mRotateCurTick ++;
00180                 if ( (mRotateCurTick < mRotateTicksPerEvent) && (mRotateCurDir == pPowermateEvent->Value) )
00181                         return false;
00182                 mRotateCurDir = pPowermateEvent->Value;
00183                 mRotateCurTick = 0;
00184                 mRotated = true;
00185                 break;
00186         case EV_KEY:
00187                 if (pPowermateEvent->Value)
00188                         mClickTimer = UtilTime::getTicks();
00189                 else
00190                         pPowermateEvent->ClickTime = float(UtilTime::getTicks() - mClickTimer) / 1000000.0f;
00191                 setKeyState(pPowermateEvent->Code, pPowermateEvent->Value);
00192                 if (pPowermateEvent->Value)
00193                         mRotated = false;
00194                 break;
00195         default:
00196                 // do nothing
00197                 break;
00198         }
00199         
00200         return true;
00201 }
00202 
00209 void GizmoPowermate::pulseLED(int Level, int PulseSpeed, int PulseTable) {
00210         changeLEDState(Level, PulseSpeed, PulseTable, 1, 1);
00211         cdbg2 << "Pulsing LED [Speed " << PulseSpeed << " @ Table " << PulseTable << "]" << endl;
00212 }
00213 
00218 void GizmoPowermate::setLED(unsigned char Level) {
00219         if (Level == mLevel)
00220                 return;
00221         changeLEDState((int) Level, 255, 0, mPulseAsleep, 0);
00222         mLevel = Level;
00223 }
00224 
00229 void GizmoPowermate::setLEDPercent(float Percent) {
00230         if (Percent < 0.0f)
00231                 Percent = 0.0f;
00232         else if (Percent > 100.0f)
00233                 Percent = 100.0f;
00234         unsigned char Level = (unsigned char) (255.0f * (Percent / 100.0f));
00235         if (Level == mLevel)
00236                 return;
00237         setLED(Level);
00238 }
00239 
00244 void GizmoPowermate::setLEDPulseAsleep(bool Enabled) {
00245         mPulseAsleep = Enabled;
00246 } 
00247 
00252 void GizmoPowermate::setRotateSensitivity(int TicksPerEvent) {
00253         mRotateTicksPerEvent = TicksPerEvent;
00254         cdbg << "Powermate Rotate Sensitivity set to " << TicksPerEvent << " Ticks per Generated Event" << endl;
00255 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3