Arduino Cloud Light -- Boolean thunderstorm - boolean

I'm using an arduino to make a Cloud Light with an IR remote. I am trying to make one of the buttons simulate a thunder storm where the white LED's will randomly flash WITHIN the boolean statement under the specific code... Trouble is, I can only make it go through the flash period once... Here's the portion of code for the storm:
#include <IRremote.h>
// My modified code
int RECV_PIN = 11;
int red = 2;
int yellow = 3;
int green = 4;
int blue = 5;
int purple = 6;
int white = 7;
long randOn = random(10,200);
long guess = random(1000,20000);
#define powercode 16726725
#define stormcode 4294967295
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(purple, OUTPUT);
pinMode(white, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
int redbool = 0;
int orangebool = 0;
int yellowbool = 0;
int greenbool = 0;
int bluebool = 0;
int purplebool = 0;
int whitebool = 0;
int rgbbool = 0;
int rainbowbool = 0;
int sunbool = 0;
int rainbool = 0;
int stormbool = 0;
int quickbool = 0;
unsigned long last = millis();
void loop() {
if (irrecv.decode(&results)) {
//Sun
if (results.value == 16726725) {
if (millis() - last > 250) {
sunbool = !sunbool;
if (sunbool == 0) {
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
digitalWrite (blue, LOW);
digitalWrite (purple, LOW);
digitalWrite (white, LOW);
}
else {
digitalWrite (red, LOW);
digitalWrite (green, LOW);
digitalWrite (blue, LOW);
digitalWrite (purple, LOW);
digitalWrite (white, HIGH);
digitalWrite (yellow, HIGH);
}
}
last = millis();
}
//rain
if (results.value == 16745085) {
if (millis() - last > 250) {
rainbool = !rainbool;
if (rainbool == 0) {
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
digitalWrite (blue, LOW);
digitalWrite (purple, LOW);
digitalWrite (white, LOW);
}
else {
digitalWrite (red, LOW);
digitalWrite (green, LOW);
digitalWrite (yellow, LOW);
digitalWrite (purple, LOW);
digitalWrite (white, HIGH);
digitalWrite (blue, HIGH);
}
}
last = millis();
}
//storm
if (results.value == 16759365) {
if (millis() - last > 250) {
stormbool = !stormbool;
if (stormbool == 0) {
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
digitalWrite (blue, HIGH);
digitalWrite (purple, HIGH);
digitalWrite (white, LOW);
}
else {
digitalWrite (red, LOW);
digitalWrite (green, LOW);
digitalWrite (yellow, LOW);
digitalWrite (white, LOW);
digitalWrite (blue, HIGH);
digitalWrite (purple, HIGH);
digitalWrite(white, HIGH);
delay(randOn);
digitalWrite(white, LOW);
delay(randOn + randOn);
digitalWrite(white, HIGH);
delay(randOn);
digitalWrite(white, LOW);
}
}
last = millis();
}
//rainbow
if (results.value == 16712445) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
rainbowbool = !rainbowbool;
digitalWrite(white, LOW);
digitalWrite(red, rainbowbool ? HIGH : LOW);
digitalWrite(yellow, rainbowbool ? HIGH : LOW);
digitalWrite(green, rainbowbool ? HIGH : LOW);
digitalWrite(blue, rainbowbool ? HIGH : LOW);
digitalWrite(purple, rainbowbool ? HIGH : LOW);
}
last = millis();
}
//red
if (results.value == 16718565) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
redbool = !redbool;
digitalWrite(red, redbool ? HIGH : LOW);
}
last = millis();
}
//orange
if (results.value == 16726215) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
orangebool = !orangebool;
digitalWrite(red, orangebool ? HIGH : LOW);
digitalWrite(yellow, orangebool ? HIGH : LOW);
}
last = millis();
}
//yellow
if (results.value == 16718055) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
yellowbool = !yellowbool;
digitalWrite(yellow, yellowbool ? HIGH : LOW);
}
last = millis();
}
//green
if (results.value == 16751205) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
greenbool = !greenbool;
digitalWrite(green, greenbool ? HIGH : LOW);
}
last = millis();
}
//blue
if (results.value == 16753245) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
bluebool = !bluebool;
digitalWrite(blue, bluebool ? HIGH : LOW);
}
last = millis();
}
//purple
if (results.value == 16757325) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
purplebool = !purplebool;
digitalWrite(purple, purplebool ? HIGH : LOW);
}
last = millis();
}
//white
if (results.value == 16720605) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
whitebool = !whitebool;
digitalWrite(white, whitebool ? HIGH : LOW);
}
last = millis();
}
//Quick section
if (results.value == 16771095) { // TIVO button
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) {
quickbool =! quickbool;
digitalWrite(white, HIGH);
delay(guess);
digitalWrite(white, LOW);
delay(guess);
digitalWrite(white, HIGH);
delay(guess + guess);
digitalWrite(white, LOW);
}
last = millis();
}
irrecv.resume(); // Receive the next value
}
}
If I keep adding these delays and digitalWrites, I can't change "channels" or LED's to different colors, I'm stuck until the thing finishes! Thanks for helping me ahead of time!

you are correct that when using the delay function no other action can happen. You should try and make your code like the blinkWithoutDelay example.
so you could do something like this
// struct that represents the values for each led
struct LED{
int pin; // pin # the led is connected to
int state; // state of the led , on or off
long interval; // how long we should wait before toggling
long timeToggled; // last time we toggled
};
if(results.value == 16759365){
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
LED white = {0,HIGH,100,millis()};
LED yellow = {1,HIGH,10,millis()};
LED blue = {2,HIGH,420,millis()};
results = readIr(); // check the ir value again and keep looping until it changes
while(results.value == 16759365){
toggleLed(white);
toggleLed(yellow);
toggleLed(blue);
results = readIr(); // if ir value hasnt changed keep looping until it does
}
digitalWrite(0,LOW);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
}
void toggleLed(struct LED currentLed){
unsigned long currentMillis = millis();
if(currentMillis - currentLed.timeToggled > currentLed.interval) {
// save the last time you blinked the LED
currentLed.timeToggled = currentMillis;
// if the LED is off turn it on and vice-versa:
if (currentLed.state == LOW)
currentLed.state = HIGH;
else
currentLed.state = LOW;
// set the LED with the ledState of the variable:
digitalWrite(currentLed.pin, currentLed.state);
}
}

Related

Long time press and random time values

I want to trigger an event after tot seconds of long press on a touch screen.
I'm trying to achieve this goal with the following code.
The problem is that the time that has passed is, in some way, random.
private float timePressed = 0.0f;
private float timeLastPress = 0.0f;
public float timeDelayThreshold = 2.0f;
void Update() {
checkForLongPress(timeDelayThreshold);
}
void checkForLongPress(float tim) {
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
// If the user puts her finger on screen...
Debug.Log("Touch start");
timePressed = Time.time - timeLastPress;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
// If the user raises her finger from screen
timeLastPress = Time.time;
Debug.Log("Releasing Touch");
Debug.Log("Time passed --> " + timePressed);
if (timePressed > tim)
{
Debug.Log("Closing APP");
// Is the time pressed greater than our time delay threshold?
Application.Quit();
}
}
}
}
The Fact is that this condition "(timePressed > tim)" is never true and i do not understand why.
Time.time returns The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game..
Fixed pseudo code:
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
_timePressed = Time.time;
return;
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
var deltaTime = Time.time - _timePressed;
if (deltaTime > _maxTimeTreshold)
{
Application.Quit();
}
}

Kinect for Windows V2

In my project I need to detect multiple gestures, I made a .gbd file with 2 continuous gestures. I can detect the continuous gesture when the .gbd file had one gesture. When there is 2 gestures I am unable to read any one gesture. Do any one know the reason? Here I post my code. I used Body basics example from sdk, and added necessary functions separately to avoid memory leakage.
//separate function to initialize database, vgb frame source and vgb frame reader
void CBodyBasics::InitializeVGB()
{
HRESULT hr;
std::wstring sDbFile = L"ContinuousGestures.gbd";
hr = CreateVisualGestureBuilderDatabaseInstanceFromFile(sDbFile.c_str(), &m_pGestureDatabase);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Success DB\n");
}
else
{
OutputDebugString(L"Failure DB\n");
}
for (int i = 0; i < BODY_COUNT; i++)
{
// Source
hr = CreateVisualGestureBuilderFrameSource(m_pKinectSensor, 0, &m_pVGBFrameSource[i]);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Source created\n");
}
else
{
OutputDebugString(L"Soure creation failed\n");
}
// Reader
hr = m_pVGBFrameSource[i]->OpenReader(&m_pVGBFrameReader[i]);
if (SUCCEEDED(hr))
{
OutputDebugString(L"reader opened\n");
}
else
{
OutputDebugString(L"No reader\n");
}
}
}
//function to find the available gesture count, add the available gestures to source
void CBodyBasics::CheckGestureMultiple()
{
HRESULT hr;
hr = m_pGestureDatabase->get_AvailableGesturesCount(&iGestureCount);
//gesture count
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture #\n");
}
else
{
OutputDebugString(L"0 gesture\n");
}
StringCchPrintf(smessage, _countof(smessage), L"Gesture Count %d \n", iGestureCount);
OutputDebugString(smessage);
hr = m_pGestureDatabase->get_AvailableGestures(iGestureCount, m_pgesturelist);
//gesture list
if (SUCCEEDED(hr) && m_pgesturelist != nullptr)
{
OutputDebugString(L"received available gestures\n");
GestureType mtype;
WCHAR sName[260];
for (int i = 0; i < iGestureCount; ++i)
{
hr = m_pgesturelist[i]->get_GestureType(&mtype);
//gesture type
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture type\n");
if (mtype == GestureType_Discrete)
{
OutputDebugString(L"discrete\n");
}
else if (mtype == GestureType_Continuous)
{
OutputDebugString(L"continuous\n");
}
}
else
{
OutputDebugString(L"no gesture type\n");
}
hr = m_pgesturelist[i]->get_Name(260, sName);
//gesture name
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture name\n");
StringCchPrintf(smessage, _countof(smessage), L"%s \t %d \n", sName, i);
OutputDebugString(smessage);
}
else
{
OutputDebugString(L"no name\n");
}
}
}
else
{
OutputDebugString(L"not received gestures\n");
}
for (int i = 0; i < BODY_COUNT; ++i)
{
//add gestures to the source
hr = m_pVGBFrameSource[i]->AddGestures(iGestureCount, m_pgesturelist);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Gesture added\n");
}
else
{
OutputDebugString(L"not added\n");
}
}
}
//function to read the vgb frame and process if the gesture is detected
void CBodyBasics::ReadVGBFrameMultiple(int t_bodyCount)
{
HRESULT hr;
for (int i = 0; i < t_bodyCount; i++)
{
hr = m_pVGBFrameReader[i]->CalculateAndAcquireLatestFrame(&m_pVGBFrame);
if (SUCCEEDED(hr) && m_pVGBFrame != nullptr)
{
OutputDebugString(L"vgb frame\n");
BOOLEAN bGestureTracked = false;
hr = m_pVGBFrame->get_IsTrackingIdValid(&bGestureTracked);
if (SUCCEEDED(hr) && bGestureTracked)
{
OutputDebugString(L"valid gesture id and gesture tracked\n");
GestureType gesturetype;
m_pgesturelist[i]->get_GestureType(&gesturetype);
switch (gesturetype)
{
case GestureType::GestureType_Discrete:
hr = m_pVGBFrame->get_DiscreteGestureResult(m_pgesturelist[i], &m_pDiscreteResult);
if (SUCCEEDED(hr) && m_pDiscreteResult != nullptr)
{
OutputDebugString(L"discrete result\n");
BOOLEAN bDetected = false;
hr = m_pDiscreteResult->get_Detected(&bDetected);
if (SUCCEEDED(hr) && bDetected)
{
OutputDebugString(L"gesture detected\n");
StringCchPrintf(smessage, _countof(smessage), L"gesture detected %d\n", bDetected);
OutputDebugString(smessage);
float confidence = 0.0f;
hr = m_pDiscreteResult->get_Confidence(&confidence);
if (SUCCEEDED(hr) && confidence != 0.0f)
{
OutputDebugString(L"confidence is detected \t");
//if (confidence > 0.3f)
//{
StringCchPrintf(smessage, _countof(smessage), L"confidence value is %f\n", confidence);
OutputDebugString(smessage);
//}
}
else
{
OutputDebugString(L"no confidence\n");
}
}
else
{
OutputDebugString(L"gesture not detected\n");
}
}
else
{
OutputDebugString(L"no discrete result\n");
}
case GestureType::GestureType_Continuous:
hr = m_pVGBFrame->get_ContinuousGestureResult(m_pgesturelist[i], &m_pContinousResult);
if (SUCCEEDED(hr) && m_pContinousResult != nullptr)
{
OutputDebugString(L"continuous gesture\n");
WCHAR gesturename[100];
hr = m_pgesturelist[i]->get_Name(100, gesturename);
//if (gesturename == L"SwipeLeft")
//{
float progress = 0.0f;
OutputDebugString(L"get progress of continuous gesture \t");
hr = m_pContinousResult->get_Progress(&progress);
if (SUCCEEDED(hr) && progress != 0.0f)
{
StringCchPrintf(smessage, _countof(smessage), L"progress value is %f\n", progress);
OutputDebugString(smessage);
}
//}
}
else
{
OutputDebugString(L"no continuous gesture\n");
}
default:
break;
}
}
else
{
OutputDebugString(L"invalid gesture id\n");
}
}
else
{
OutputDebugString(L"no frame\n");
}
}
}
Is there wrong in my code? Do I need to modify my code? Can anyone help me?

Need help on rotating image view with animation on orientation change (4 side rotation) in android

i have a image view and i need to rotate(with IOS like animation) the image view on orientation change in android(portrait,landscape,reverse portrait and reverse landscape).
please advice
void rotateAndSet(int angle) {
if (currentAngle != angle || currentImage != currentBaseImage) { // This
// is
// to
// remove
// unnecessary
// drawing
currentAngle = angle;
currentImage = currentBaseImage;
myImg = decodeBase64(currentBaseImage);
matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
/*RotateAnimation animation = new RotateAnimation(0, 90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);
animation.setDuration(800);
ivFullScreen.startAnimation(animation); */
ivFullScreen.setLayoutParams(param);
ivFullScreen.setImageBitmap(rotated);
}
}
i got it
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
RotateAnimation animation = null;
if (event.values[0] < 4 && event.values[0] > -4) {
if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
// UP
orientation = ExifInterface.ORIENTATION_ROTATE_90;
Log.i("testme","up");
animation = getRotateAnimation(0);
degrees = 0;
} else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
// UP SIDE DOWN
Log.i("testme","up side down");
orientation = ExifInterface.ORIENTATION_ROTATE_270;
animation = getRotateAnimation(180);
degrees = 180;
}
} else if (event.values[1] < 4 && event.values[1] > -4) {
if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
// LEFT
Log.i("testme","left");
orientation = ExifInterface.ORIENTATION_NORMAL;
animation = getRotateAnimation(-270);
degrees =-270;
} else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
// RIGHT
Log.i("testme","right");
orientation = ExifInterface.ORIENTATION_ROTATE_180;
animation = getRotateAnimation(270);
degrees = 270;
}
}
if (animation != null) {
ivFullScreen.startAnimation(animation);
}
}
}
}

Minmax Algorithm for tic-tac-toe game in Objective-C

I am writing a minmax algorithm as the artificial intelligence for a tic-tac-toe game, I followed the similar instruction here, but the algorithm seems not intelligent enough, even though I tried to search deeper in the tree, can anyone help to analyze where goes wrong? thank you very much in advance!
- (int) miniMax:(int)depth : (UIImage*) player {
NSMutableArray *steps = [self generateMoves];
if (depth == 0 || [steps count] == 0) {
return [self evaluate];
}
int bestScore = player == myImg ? -1000000 : 1000000;
int currentScore = 0;
for (UIImageView *step in steps) {
step.image = player;
if (player == myImg) {
UIImage *opp = player == xImg ? oImg : xImg;
currentScore = [self miniMax:depth - 1 :opp];
if (currentScore > bestScore) {
bestScore = currentScore;
nextStep = step;
}
} else {
UIImage *opp = player == xImg ? oImg : xImg;
currentScore = [self miniMax:depth - 1 :opp];
if (currentScore < bestScore) {
bestScore = currentScore;
nextStep = step;
}
}
step.image = NULL;
}
return bestScore;
}
- (int) evaluate {
int score = 0;
score += [self evaluateLine:img0 :img1 :img2];
score += [self evaluateLine:img3 :img4 :img5];
score += [self evaluateLine:img6 :img7 :img8];
score += [self evaluateLine:img0 :img3 :img6];
score += [self evaluateLine:img1 :img4 :img7];
score += [self evaluateLine:img2 :img5 :img8];
score += [self evaluateLine:img2 :img4 :img6];
score += [self evaluateLine:img0 :img4 :img8];
return score;
}
- (int) evaluateLine:(UIImageView*)img1 :(UIImageView*)img2 :(UIImageView*)img3 {
int score = 0;
// first cell
if ([img1 image] == myImg) {
score = 1;
} else if ([img1 image] == oppImg){
score = -1;
}
// second cell
if ([img2 image] == myImg) {
if (score == 1) {
score = 10;
} else if (score == -1) {
return 0;
} else {
score = -1;
}
} else if ([img2 image] == oppImg){
if (score == -1) {
score = -10;
} else if (score == 1) {
return 0;
} else {
score = -1;
}
}
// third cell
if ([img3 image] == myImg) {
if (score > 0) {
score *= 10;
} else if (score < 0) {
return 0;
} else {
score = -1;
}
} else if ([img3 image] == oppImg){
if (score < 0) {
score *= 10;
} else if (score > 1) {
return 0;
} else {
score = -1;
}
}
return score;
}
What I use here is: if there exits the same image as the human player holds, the score plus 1. If there
are two or three player's image in a line or row or diagonal, the total score is 10 and 100 separately. If there exist both 'X' and 'O' in a same row, column or diagonal, the score is 0. Computer holds the negative score for these mentioned above.
Minimax assumes that the evaluation function is from the perspective of the first player. What you want is a function that always have a higher value if the first player is better.
It seems that your heuristic function adapts its value based on the current player (I am not an objective-C programmer). Change that function so that it does not know what myImg or oppImg is -- use xImg and oImg directly. Might just work.

Use GSEvent to send touch event,but it's invalid

I code to send a touch event to my app by GSEvent,get mach_port_t by GSCopyPurpleSystemEventPort().
the sending function was launched after the ApplicationDidFinishLaunch:option have completed.the app is UIControlView app.
code as follow:
void handleMouseEventAtPoint(CGPoint point, int buttons)
{
// NOTE: Must store button state for comparision, port for
// mouse dragging and button up
static int buttons_;
static mach_port_t port_;
int diff = buttons_ ^ buttons;
bool twas = ((buttons_ & 0x1) != 0);
bool tis = ((buttons & 0x1) != 0);
buttons_ = buttons;
// Round point values to prevent subpixel coordinates
point.x = roundf(point.x);
point.y = roundf(point.y);
// Check for mouse button events
mach_port_t purple;
if ((diff & 0x10) != 0) {
// Simulate Headset button press
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & 0x4) != 0 ?
kGSEventHeadsetButtonDown :
kGSEventHeadsetButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if ((diff & buttonThree) != 0) {
// Simulate Home button press
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & buttonThree) != 0 ?
kGSEventMenuButtonDown :
kGSEventMenuButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if ((diff & buttonTwo) != 0)
{
// Simulate Sleep/Wake button press
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & buttonTwo) != 0 ?
kGSEventLockButtonDown :
kGSEventLockButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if (twas != tis || tis) {
// Main (left button) state changed, or was dragged
struct {
struct GSEventRecord record;
struct
{
struct GSEventRecordInfo info;
struct GSPathInfo path;
} data;
} event;
memset(&event, 0, sizeof(event));
event.record.type = kGSEventHand;
event.record.windowLocation = point;
event.record.timestamp = GSCurrentEventTimestamp();
event.record.infoSize = sizeof(event.data);
event.data.info.handInfo.type = twas == tis ?
kGSHandInfoTypeTouchDragged :
tis ?
kGSHandInfoTypeTouchDown :
kGSHandInfoTypeTouchUp;
event.data.info.handInfo._0x44 = 0x1;
event.data.info.handInfo._0x48 = tis ? 0x1 : 0x0;
event.data.info.pathPositions = 1;
event.data.path.pathIndex = 0x01;
event.data.path.pathIdentity = 0x02;
event.data.path.pathProximity = tis ? 0x03 : 0x00;
event.data.path.pathLocation = event.record.windowLocation;
if (twas != tis && tis)
{
// Button down and was not down before
port_ = 0;
CAWindowServer *server;
server = [CAWindowServer serverIfRunning];
char svrptrstr [255];
sprintf(svrptrstr, "%p", server);
NSLog(#"One!");
if (server = [CAWindowServer serverIfRunning])
//if (true)
{
NSLog(#"Two!");
//NSArray *displays([server displays]);
NSArray *displays = [server displays];
if (displays != nil && [displays count] != 0)
{
NSLog(#"Three!");
//CAWindowServer *display;
if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
{
NSLog(#"Four!");
port_ = [display clientPortAtPosition:point];
}
}
}
if (port_ == 0)
{
// Is SpringBoard
if (purple == 0)
{
purple = GSGetPurpleSystemEventPort();
port_ = purple;
}
}
}
FixRecord(&event.record);
GSSendEvent(&event.record, port_);
NSLog(#"Event sent!");
//GSSendSystemEvent(&event.record);
//GSSendEvent(&event.record, purple);
}
if (purple != 0 && PurpleAllocated)
{
mach_port_deallocate(mach_task_self(), purple);
NSLog(#"Deallocated mach_port!");
}
}
but the app launch on the jailbreak ipad2(iOS5.01),there is no any click result,if click event was done,the debug.log would be there.
who can tell me what i have miss?
I've seen this code somewhere else (I think it was on an app that translated mouse clicks to iPhone/iPad taps). The problem here is not the code or the port, but the event structure you are sending. This is the GSevent structure for iOS 3.0, not for iOS 5.0. It has changed, not only in structure but in values and tags. I suggest receiving UIEvents, translating them into GSEvents and looking at their content.