void ButtonGroup::readButtons() { _latchPin.write(Gpio::High); std::vector<bool> results; results.resize(_size, false); for(int i=0; i < _size; i++) { results[_size - i - 1] = _dataPin.read() == Gpio::High; _clockPin.write(Gpio::High); SystemUtils::delay(1); _clockPin.write(Gpio::Low); SystemUtils::delay(1); } _latchPin.write(Gpio::Low); for(int i = 0; i < _size; i++){ if(results[i] != _previous[i]) { if(_mapping.find(i) != _mapping.end()) { _eventPipe.send(ButtonEvent(results[i]? press:release, _mapping[i]+_offset)); } _previous[i] = results[i]; } } }
void CreditsEvent(SDL_Event* e, int* state) { if(e->type == SDL_KEYDOWN && e->key.keysym.sym == SDLK_ESCAPE) { *state = MENU_STATE; } if(ButtonEvent(&back, e)) { *state = MENU_STATE; } }
void ButtonEventSource::poll( unsigned int interval_ms /*= 5*/ ) { for (unsigned int i = 0; i < ARRAY_SIZE(pin_to_button_map); ++i) { PinMapEntry *ent = pin_to_button_map + i; int status = pinRead(ent->pin); if (status_table[i] != status) { status_table[i] = status; callback(ButtonEvent(ent->player, ent->button, status == LOW)); } } delay(interval_ms); }
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if ( wmode ) { // Perform W-Mode only functionality switch ( uMsg ) { case WM_SIZING: { SIZE border; GetBorderSize(hWnd, &border); RECT *rct = (RECT*)lParam; SIZE ws = { rct->right - rct->left - border.cx, rct->bottom - rct->top - border.cy }; if ( ws.cx < WMODE_MIN_WIDTH ) { ws.cx = WMODE_MIN_WIDTH; CorrectWindowWidth(wParam, &ws, rct, &border); } if ( ws.cy < WMODE_MIN_HEIGHT ) { ws.cy = WMODE_MIN_HEIGHT; CorrectWindowHeight(wParam, &ws, rct, &border); } if ( GetKeyState(VK_CONTROL) & 0x8000 && ws.cy != ws.cx * 3 / 4 ) { if ( wParam == WMSZ_TOP || wParam == WMSZ_BOTTOM ) { ws.cx = ws.cy * 4 / 3; CorrectWindowWidth(WMSZ_RIGHT, &ws, rct, &border); } else { ws.cy = ws.cx * 3 / 4; CorrectWindowHeight( (wParam == WMSZ_RIGHT || wParam == WMSZ_LEFT) ? WMSZ_BOTTOM : wParam, &ws, rct, &border); } } if ( isCorrectVersion ) // must be correct version to reference BWDATA { if ( ws.cx >= BW::BWDATA::GameScreenBuffer.width() - WMODE_SNAP_RANGE && ws.cx <= BW::BWDATA::GameScreenBuffer.width() + WMODE_SNAP_RANGE ) { ws.cx = BW::BWDATA::GameScreenBuffer.width(); CorrectWindowWidth( (wParam == WMSZ_TOP || wParam == WMSZ_BOTTOM) ? WMSZ_RIGHT : wParam, &ws, rct, &border); } if ( ws.cy >= BW::BWDATA::GameScreenBuffer.height() - WMODE_SNAP_RANGE && ws.cy <= BW::BWDATA::GameScreenBuffer.height() + WMODE_SNAP_RANGE ) { ws.cy = BW::BWDATA::GameScreenBuffer.height(); CorrectWindowHeight( (wParam == WMSZ_RIGHT || wParam == WMSZ_LEFT) ? WMSZ_BOTTOM : wParam, &ws, rct, &border); } } break; } // case WM_SIZING case WM_SIZE: { switch ( wParam ) { case SIZE_RESTORED: { RECT tempRect; GetClientRect(hWnd, &tempRect); windowRect.right = tempRect.right; windowRect.bottom = tempRect.bottom; WriteConfig("window", "width", tempRect.right); WriteConfig("window", "height", tempRect.bottom); break; } }// wParam switch break; } // case WM_SIZE case WM_MOVE: { RECT tempRect; GetWindowRect(hWnd, &tempRect); if ( tempRect.right > 0 && tempRect.bottom > 0 && tempRect.left < GetSystemMetrics(SM_CXFULLSCREEN) && tempRect.top < GetSystemMetrics(SM_CYFULLSCREEN) ) { windowRect.left = tempRect.left; windowRect.top = tempRect.top; WriteConfig("window", "left", tempRect.left); WriteConfig("window", "top", tempRect.top); } break; } // case WM_MOVE case WM_PAINT: if ( gbWantUpdate && pBits) { static DWORD dwLastUpdate = 0; DWORD dwNewTick = GetTickCount(); if ( dwLastUpdate + (IsIconic(hWnd) ? 200 : 20) > dwNewTick ) break; dwLastUpdate = dwNewTick; gbWantUpdate = false; // begin paint PAINTSTRUCT paint; HDC hdc = BeginPaint(hWnd, &paint); if ( isCorrectVersion ) // must be correct version, @todo: make independent { // Blit to the screen RECT cRect; GetClientRect(hWnd, &cRect); if ( cRect.right == BW::BWDATA::GameScreenBuffer.width() && cRect.bottom == BW::BWDATA::GameScreenBuffer.height() ) { BitBlt(hdc, 0, 0, BW::BWDATA::GameScreenBuffer.width(), BW::BWDATA::GameScreenBuffer.height(), hdcMem, 0, 0, SRCCOPY); } else { SetStretchBltMode(hdc, HALFTONE); //StretchBlt(hdc, 0, 0, cRect.right, cRect.bottom, hdcMem, 0, 0, BW::BWDATA::GameScreenBuffer->wid, BW::BWDATA::GameScreenBuffer->ht, SRCCOPY); StretchDIBits(hdc, 0, 0, cRect.right, cRect.bottom, 0, 0, BW::BWDATA::GameScreenBuffer.width(), BW::BWDATA::GameScreenBuffer.height(), pBits, (BITMAPINFO*)&wmodebmp, DIB_RGB_COLORS, SRCCOPY); } } // end paint EndPaint(hWnd, &paint); } // data break; case WM_NCMOUSEMOVE: SetCursorShowState(true); break; case WM_MOUSEMOVE: SetCursorShowState(false); lParam = FixPoints(lParam); break; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: { RECT clientRct; GetClientRect(hWnd, &clientRct); ClientToScreen(hWnd, (LPPOINT)&clientRct.left); ClientToScreen(hWnd, (LPPOINT)&clientRct.right); ClipCursor(&clientRct); lParam = FixPoints(lParam); break; } case WM_MBUTTONUP: case WM_LBUTTONUP: case WM_RBUTTONUP: ClipCursor(NULL); lParam = FixPoints(lParam); break; case WM_MOUSEWHEEL: case WM_RBUTTONDBLCLK: case WM_LBUTTONDBLCLK: case WM_MBUTTONDBLCLK: lParam = FixPoints(lParam); break; case WM_ACTIVATEAPP: if ( wOriginalProc ) return CallWindowProc(wOriginalProc, hWnd, WM_ACTIVATEAPP, (WPARAM)1, NULL); case WM_SETCURSOR: case WM_ERASEBKGND: return DefWindowProc(hWnd, uMsg, wParam, lParam); case WM_SYSKEYDOWN: case WM_KEYDOWN: if ( wParam == VK_MENU && !(lParam & 0x40000000)) { RECT rct; GetClientRect(hWnd, &rct); ClientToScreen(hWnd, (LPPOINT)&rct.left); ClientToScreen(hWnd, (LPPOINT)&rct.right); ClipCursor(&rct); gbHoldingAlt = true; } break; case WM_SYSKEYUP: case WM_KEYUP: if ( wParam == VK_MENU ) { ClipCursor(NULL); gbHoldingAlt = false; } break; } // switch } // if wmode if ( isCorrectVersion ) { // Perform BWAPI-added functionality switch ( uMsg ) { case WM_SYSKEYDOWN: if ( wParam == VK_RETURN && (lParam & 0x20000000) && !(lParam & 0x40000000) ) { SetWMode(BW::BWDATA::GameScreenBuffer.width(), BW::BWDATA::GameScreenBuffer.height(), !wmode); return TRUE; } break; case WM_MOUSEMOVE: if ( GetWindowLong(ghMainWnd, GWL_STYLE) & WS_SYSMENU ) // Compatibility for Xen W-Mode { BW::BWDATA::InputFlags |= 1; POINTS pt = MAKEPOINTS(lParam); BW::BWDATA::Mouse.x = pt.x; BW::BWDATA::Mouse.y = pt.y; return TRUE; } break; case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_RBUTTONDBLCLK: case WM_MBUTTONDOWN: case WM_MBUTTONUP: case WM_MBUTTONDBLCLK: if ( GetWindowLong(ghMainWnd, GWL_STYLE) & WS_SYSMENU ) // Compatibility for Xen W-Mode { ButtonEvent(uMsg - WM_MOUSEFIRST + BW_EVN_MOUSEFIRST, lParam); return TRUE; } break; case WM_SYSCOMMAND: if ( wParam == SC_MAXIMIZE ) { SetWMode(BW::BWDATA::GameScreenBuffer.width(), BW::BWDATA::GameScreenBuffer.height(), false); return TRUE; } break; } } // Register Broodwar thread name RegisterThreadName("Broodwar Main"); // Call the original WndProc if ( wOriginalProc ) return CallWindowProc(wOriginalProc, hWnd, uMsg, wParam, lParam); return DefWindowProc(hWnd, uMsg, wParam, lParam); }
/*----------------------------------------------------------------------------- * process bus telegrams */ static void ProcessBus(uint8_t ret) { TBusMsgType msgType; uint8_t i; bool msgForMe = false; uint8_t state; uint8_t mask8; uint8_t action; TBusDevRespInfo *pInfo; TBusDevRespActualValue *pActVal; TClient *pClient; static TBusTelegram sTxMsg; static bool sTxRetry = false; bool flag; uint8_t val8; if (sTxRetry) { sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; return; } if (ret == BUS_MSG_OK) { msgType = spBusMsg->type; switch (msgType) { case eBusDevReqReboot: case eBusDevReqInfo: case eBusDevReqActualValue: case eBusDevReqSetValue: case eBusDevReqSwitchState: case eBusDevReqSetAddr: case eBusDevReqEepromRead: case eBusDevReqEepromWrite: case eBusDevReqSetClientAddr: case eBusDevReqGetClientAddr: case eBusDevRespActualValueEvent: if (spBusMsg->msg.devBus.receiverAddr == MY_ADDR) { msgForMe = true; } break; case eBusButtonPressed1: case eBusButtonPressed2: case eBusButtonPressed1_2: msgForMe = true; break; default: break; } } else if (ret == BUS_MSG_ERROR) { ButtonTimeStampRefresh(); } if (msgForMe == false) { return; } switch (msgType) { case eBusDevReqReboot: /* use watchdog to reboot */ /* set the watchdog timeout as short as possible (14 ms) */ cli(); wdt_enable(WDTO_15MS); /* wait for reset */ while (1); break; case eBusButtonPressed1: ButtonEvent(spBusMsg->senderAddr, 1); break; case eBusButtonPressed2: ButtonEvent(spBusMsg->senderAddr, 2); break; case eBusButtonPressed1_2: ButtonEvent(spBusMsg->senderAddr, 1); ButtonEvent(spBusMsg->senderAddr, 2); break; case eBusDevReqInfo: /* response packet */ pInfo = &sTxMsg.msg.devBus.x.devResp.info; sTxMsg.type = eBusDevRespInfo; sTxMsg.senderAddr = MY_ADDR; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; pInfo->devType = eBusDevTypePwm4; strncpy((char *)(pInfo->version), ApplicationVersion(), sizeof(pInfo->version)); pInfo->version[sizeof(pInfo->version) - 1] = '\0'; sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqActualValue: /* response packet */ pActVal = &sTxMsg.msg.devBus.x.devResp.actualValue; sTxMsg.type = eBusDevRespActualValue; sTxMsg.senderAddr = MY_ADDR; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; pActVal->devType = eBusDevTypePwm4; PwmGetAll(pActVal->actualValue.pwm4.pwm, sizeof(pActVal->actualValue.pwm4.pwm)); val8 = 0; for (i = 0; i < NUM_PWM_CHANNEL; i++) { PwmIsOn(i, &flag); val8 |= flag ? 1 << i: 0; } pActVal->actualValue.pwm4.state = val8; sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqSetValue: if (spBusMsg->msg.devBus.x.devReq.setValue.devType != eBusDevTypePwm4) { break; } mask8 = spBusMsg->msg.devBus.x.devReq.setValue.setValue.pwm4.set; for (i = 0; i < NUM_PWM_CHANNEL; i++) { action = (0x3 << (i * 2) & mask8) >> (i * 2); switch (action) { case 0x00: /* no action, ignore pwm[] from telegram */ break; case 0x01: /* set current pwm, ignore pwm[] from telegram */ PwmOn(i, true); break; case 0x02: /* set to pwm[] from telegram */ PwmSet(i, spBusMsg->msg.devBus.x.devReq.setValue.setValue.pwm4.pwm[i]); PwmOn(i, true); break; case 0x03: /* off, ignore pwm[] from telegram */ PwmOn(i, false); break; default: break; } } /* response packet */ sTxMsg.type = eBusDevRespSetValue; sTxMsg.senderAddr = MY_ADDR; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqSwitchState: state = spBusMsg->msg.devBus.x.devReq.switchState.switchState; if ((state & 0x01) != 0) { SwitchEvent(spBusMsg->senderAddr, 1, true); } else { SwitchEvent(spBusMsg->senderAddr, 1, false); } if ((state & 0x02) != 0) { SwitchEvent(spBusMsg->senderAddr, 2, true); } else { SwitchEvent(spBusMsg->senderAddr, 2, false); } /* response packet */ sTxMsg.type = eBusDevRespSwitchState; sTxMsg.senderAddr = MY_ADDR; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; sTxMsg.msg.devBus.x.devResp.switchState.switchState = state; sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqSetAddr: sTxMsg.senderAddr = MY_ADDR; sTxMsg.type = eBusDevRespSetAddr; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; eeprom_write_byte((uint8_t *)MODUL_ADDRESS, spBusMsg->msg.devBus.x.devReq.setAddr.addr); sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqEepromRead: sTxMsg.senderAddr = MY_ADDR; sTxMsg.type = eBusDevRespEepromRead; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; sTxMsg.msg.devBus.x.devResp.readEeprom.data = eeprom_read_byte((const uint8_t *)spBusMsg->msg.devBus.x.devReq.readEeprom.addr); sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevReqEepromWrite: sTxMsg.senderAddr = MY_ADDR; sTxMsg.type = eBusDevRespEepromWrite; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; eeprom_write_byte((uint8_t *)spBusMsg->msg.devBus.x.devReq.readEeprom.addr, spBusMsg->msg.devBus.x.devReq.writeEeprom.data); sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; case eBusDevRespActualValueEvent: pClient = sClient; for (i = 0; i < sNumClients; i++) { if ((pClient->address == spBusMsg->senderAddr) && (pClient->state == eEventWaitForConfirmation)) { TBusDevActualValuePwm4 *p; uint16_t buf[NUM_PWM_CHANNEL]; PwmGetAll(buf, sizeof(buf)); val8 = 0; for (i = 0; i < NUM_PWM_CHANNEL; i++) { PwmIsOn(i, &flag); val8 |= flag ? 1 << i: 0; } p = &spBusMsg->msg.devBus.x.devResp.actualValueEvent.actualValue.pwm4; if ((memcmp(p->pwm, buf, sizeof(buf)) == 0) && (p->state == val8)) { pClient->state = eEventConfirmationOK; } break; } pClient++; } break; case eBusDevReqSetClientAddr: sTxMsg.senderAddr = MY_ADDR; sTxMsg.type = eBusDevRespSetClientAddr; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; for (i = 0; i < BUS_MAX_CLIENT_NUM; i++) { uint8_t *p = &spBusMsg->msg.devBus.x.devReq.setClientAddr.clientAddr[i]; eeprom_write_byte((uint8_t *)(CLIENT_ADDRESS_BASE + i), *p); } sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; GetClientListFromEeprom(); break; case eBusDevReqGetClientAddr: sTxMsg.senderAddr = MY_ADDR; sTxMsg.type = eBusDevRespGetClientAddr; sTxMsg.msg.devBus.receiverAddr = spBusMsg->senderAddr; for (i = 0; i < BUS_MAX_CLIENT_NUM; i++) { uint8_t *p = &sTxMsg.msg.devBus.x.devResp.getClientAddr.clientAddr[i]; *p = eeprom_read_byte((const uint8_t *)(CLIENT_ADDRESS_BASE + i)); } sTxRetry = BusSend(&sTxMsg) != BUS_SEND_OK; break; default: break; } }