| 1 | //============================================================================ | 
|---|
| 2 | // | 
|---|
| 3 | //   SSSS    tt          lll  lll | 
|---|
| 4 | //  SS  SS   tt           ll   ll | 
|---|
| 5 | //  SS     tttttt  eeee   ll   ll   aaaa | 
|---|
| 6 | //   SSSS    tt   ee  ee  ll   ll      aa | 
|---|
| 7 | //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator" | 
|---|
| 8 | //  SS  SS   tt   ee      ll   ll  aa  aa | 
|---|
| 9 | //   SSSS     ttt  eeeee llll llll  aaaaa | 
|---|
| 10 | // | 
|---|
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony | 
|---|
| 12 | // and the Stella Team | 
|---|
| 13 | // | 
|---|
| 14 | // See the file "License.txt" for information on usage and redistribution of | 
|---|
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 16 | //============================================================================ | 
|---|
| 17 |  | 
|---|
| 18 | #include "DataGridWidget.hxx" | 
|---|
| 19 | #include "DrivingWidget.hxx" | 
|---|
| 20 |  | 
|---|
| 21 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 22 | DrivingWidget::DrivingWidget(GuiObject* boss, const GUI::Font& font, | 
|---|
| 23 | int x, int y, Controller& controller) | 
|---|
| 24 | : ControllerWidget(boss, font, x, y, controller), | 
|---|
| 25 | myGrayIndex(0) | 
|---|
| 26 | { | 
|---|
| 27 | const string& label = getHeader(); | 
|---|
| 28 |  | 
|---|
| 29 | const int fontHeight = font.getFontHeight(), | 
|---|
| 30 | bwidth = font.getStringWidth( "Gray code +") + 10, | 
|---|
| 31 | bheight = font.getLineHeight() + 4; | 
|---|
| 32 | int xpos = x, ypos = y, lwidth = font.getStringWidth( "Right (Driving)"); | 
|---|
| 33 | StaticTextWidget* t; | 
|---|
| 34 |  | 
|---|
| 35 | t = new StaticTextWidget(boss, font, xpos, ypos+2, lwidth, | 
|---|
| 36 | fontHeight, label, TextAlign::Left); | 
|---|
| 37 |  | 
|---|
| 38 | ypos += t->getHeight() + 20; | 
|---|
| 39 | myGrayUp = new ButtonWidget(boss, font, xpos, ypos, bwidth, bheight, | 
|---|
| 40 | "Gray code +", kGrayUpCmd); | 
|---|
| 41 | myGrayUp->setTarget(this); | 
|---|
| 42 |  | 
|---|
| 43 | ypos += myGrayUp->getHeight() + 5; | 
|---|
| 44 | myGrayDown = new ButtonWidget(boss, font, xpos, ypos, bwidth, bheight, | 
|---|
| 45 | "Gray code -", kGrayDownCmd); | 
|---|
| 46 | myGrayDown->setTarget(this); | 
|---|
| 47 |  | 
|---|
| 48 | xpos += myGrayDown->getWidth() + 10;  ypos -= 10; | 
|---|
| 49 | myGrayValue = new DataGridWidget(boss, font, xpos, ypos, | 
|---|
| 50 | 1, 1, 2, 8, Common::Base::F_16); | 
|---|
| 51 | myGrayValue->setTarget(this); | 
|---|
| 52 | myGrayValue->setEditable(false); | 
|---|
| 53 |  | 
|---|
| 54 | xpos = x + 30;  ypos += myGrayDown->getHeight() + 20; | 
|---|
| 55 | myFire = new CheckboxWidget(boss, font, xpos, ypos, "Fire", kFireCmd); | 
|---|
| 56 | myFire->setTarget(this); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 60 | void DrivingWidget::loadConfig() | 
|---|
| 61 | { | 
|---|
| 62 | uInt8 gray = 0; | 
|---|
| 63 | if(getPin(Controller::DigitalPin::One)) gray += 1; | 
|---|
| 64 | if(getPin(Controller::DigitalPin::Two)) gray += 2; | 
|---|
| 65 |  | 
|---|
| 66 | for(myGrayIndex = 0; myGrayIndex < 4; ++myGrayIndex) | 
|---|
| 67 | { | 
|---|
| 68 | if(ourGrayTable[myGrayIndex] == gray) | 
|---|
| 69 | { | 
|---|
| 70 | setValue(myGrayIndex); | 
|---|
| 71 | break; | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | myFire->setState(!getPin(Controller::DigitalPin::Six)); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 79 | void DrivingWidget::handleCommand( | 
|---|
| 80 | CommandSender* sender, int cmd, int data, int id) | 
|---|
| 81 | { | 
|---|
| 82 | switch(cmd) | 
|---|
| 83 | { | 
|---|
| 84 | case kGrayUpCmd: | 
|---|
| 85 | myGrayIndex = (myGrayIndex + 1) % 4; | 
|---|
| 86 | setPin(Controller::DigitalPin::One, (ourGrayTable[myGrayIndex] & 0x1) != 0); | 
|---|
| 87 | setPin(Controller::DigitalPin::Two, (ourGrayTable[myGrayIndex] & 0x2) != 0); | 
|---|
| 88 | setValue(myGrayIndex); | 
|---|
| 89 | break; | 
|---|
| 90 | case kGrayDownCmd: | 
|---|
| 91 | myGrayIndex = myGrayIndex == 0 ? 3 : myGrayIndex - 1; | 
|---|
| 92 | setPin(Controller::DigitalPin::One, (ourGrayTable[myGrayIndex] & 0x1) != 0); | 
|---|
| 93 | setPin(Controller::DigitalPin::Two, (ourGrayTable[myGrayIndex] & 0x2) != 0); | 
|---|
| 94 | setValue(myGrayIndex); | 
|---|
| 95 | break; | 
|---|
| 96 | case kFireCmd: | 
|---|
| 97 | setPin(Controller::DigitalPin::Six, !myFire->getState()); | 
|---|
| 98 | break; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 103 | void DrivingWidget::setValue(int idx) | 
|---|
| 104 | { | 
|---|
| 105 | int grayCode = ourGrayTable[idx]; | 
|---|
| 106 | // FIXME  * 8 = a nasty hack, because the DataGridWidget does not support 2 digit binary output | 
|---|
| 107 | myGrayValue->setList(0, (grayCode & 0b01) + (grayCode & 0b10) * 8); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 111 | uInt8 DrivingWidget::ourGrayTable[4] = { 0x03, 0x01, 0x00, 0x02 }; | 
|---|
| 112 |  | 
|---|