How can I use a 5 bit switch with DCS Bios? - Page 2 - ED Forums
 


Notices

Reply
 
Thread Tools Display Modes
Old 10-17-2017, 09:58 PM   #11
Ranma13
Member
 
Join Date: Feb 2013
Posts: 128
Reputation power: 5
Ranma13 has a spectacular aura aboutRanma13 has a spectacular aura aboutRanma13 has a spectacular aura about
Default

You really should have mentioned that from the beginning =/. The switch has a built-in multiplexer that outputs the value as binary data. You will need to convert this data from the pins back to a byte, then use that to send the appropriate message. Something like this untested code:

Code:
byte value;

void loop() {
  value = 0;

  value = digitalRead(pin1) << 0 | value;
  value = digitalRead(pin2) << 1 | value;
  value = digitalRead(pin3) << 2 | value;
  value = digitalRead(pin4) << 3 | value;
  value = digitalRead(pin5) << 4 | value;

  // At this point, value should be a number 1 to 20.
  Serial.print('UHF_PRESET_SEL ' + value + '\n');
}
You probably can't use this code directly, as I don't know if you can directly bit shift the HIGH and LOW values, but it should give you somewhere to start from. You will also need to figure out how the bits are arranged on the pins so that you're reading the correct pin for the least significant bit up to the most significant bit.

Last edited by Ranma13; 10-17-2017 at 10:01 PM.
Ranma13 is offline   Reply With Quote
Old 10-17-2017, 11:27 PM   #12
Trounce
Junior Member
 
Join Date: Dec 2014
Location: Japan and USA
Posts: 49
Reputation power: 3
Trounce is on a distinguished road
Default

Arghhh my brain is decomposing from this coding. I am going to shelve it till the weekend and see if a break can help out. I tried to re-upload my saved sketch and it is doing something different today.
Trounce is offline   Reply With Quote
Old 10-18-2017, 10:01 PM   #13
Boltz
Member
 
Boltz's Avatar
 
Join Date: Jul 2013
Location: United Kingdom
Posts: 426
Reputation power: 6
Boltz has a spectacular aura aboutBoltz has a spectacular aura aboutBoltz has a spectacular aura aboutBoltz has a spectacular aura aboutBoltz has a spectacular aura about
Default

Real ARC-164 preset channel switch outputs Gray code I believe
Boltz is offline   Reply With Quote
Old 10-19-2017, 01:10 AM   #14
Trounce
Junior Member
 
Join Date: Dec 2014
Location: Japan and USA
Posts: 49
Reputation power: 3
Trounce is on a distinguished road
Default

Boltz, I think you are right after looking at my truth table for the switch compared to wikipedia Gray code. The ARC-164 preset channel switch is a 5 digit Gray code. Pretty cool and I understand now why they would use it instead of normal binary.
Trounce is offline   Reply With Quote
Old 10-19-2017, 01:36 AM   #15
FragBum
Member
 
Join Date: Jun 2016
Location: Rockhampton QLD, AUS.
Posts: 708
Reputation power: 3
FragBum has a spectacular aura aboutFragBum has a spectacular aura aboutFragBum has a spectacular aura about
Default

Could you just read the whole port to a register or byte data object and &0x1f ??
__________________
Control is an illusion which usually shatters at the least expected moment.
FragBum is offline   Reply With Quote
Old 10-19-2017, 01:40 AM   #16
Trounce
Junior Member
 
Join Date: Dec 2014
Location: Japan and USA
Posts: 49
Reputation power: 3
Trounce is on a distinguished road
Default

I do not know that that means. But maybe soon, I might learn. Programming is a slow concept for me.
Trounce is offline   Reply With Quote
Old 10-19-2017, 01:49 AM   #17
FragBum
Member
 
Join Date: Jun 2016
Location: Rockhampton QLD, AUS.
Posts: 708
Reputation power: 3
FragBum has a spectacular aura aboutFragBum has a spectacular aura aboutFragBum has a spectacular aura about
Default

Ha it's been a while but if you read the whole 8bits of the port then and with 0x1f basically

0b00011111 == 0x1f (hex)

You should be left with the 5 lsb bits I'm getting back into AVR programming mind you that was in assembler but c++ will allow you to do this as well. Might be easier then reading 1 bit at a time.

<edit>

I see on Wicki there is a short for next loop to convert the 5 bit Gray to binary by exor-ing the mask value with the gray code value and subsequently shifting the mask value one place left until the mask =0x00.
__________________
Control is an illusion which usually shatters at the least expected moment.

Last edited by FragBum; 10-19-2017 at 02:37 AM. Reason: <edit>
FragBum is offline   Reply With Quote
Old 10-19-2017, 07:13 AM   #18
Ranma13
Member
 
Join Date: Feb 2013
Posts: 128
Reputation power: 5
Ranma13 has a spectacular aura aboutRanma13 has a spectacular aura aboutRanma13 has a spectacular aura about
Default

Quote:
Originally Posted by FragBum View Post
Ha it's been a while but if you read the whole 8bits of the port then and with 0x1f basically

0b00011111 == 0x1f (hex)

You should be left with the 5 lsb bits I'm getting back into AVR programming mind you that was in assembler but c++ will allow you to do this as well. Might be easier then reading 1 bit at a time.
There's no need to AND the value with 0x1f because you already start with 0x00 on each loop, which already has the first 3 bits as 0, and the remaining bits are set by the loop. You need to read 1 bit at a time because you need to read the logic level on the pin. There's no way to treat a group of pins as the bits in a byte without reading in each wire individually and processing it.

Quote:
I see on Wicki there is a short for next loop to convert the 5 bit Gray to binary by exor-ing the mask value with the gray code value and subsequently shifting the mask value one place left until the mask =0x00.
That's exactly what my code does, but I skip having a mask in favor of just shifting the read-in pin value directly, and it needs to use OR, not XOR. Let's say that the selected number is 13 (0b01101), and the bits from MSB to LSB are on pins 14 (LOW), 13 (HIGH), 12 (HIGH), 11 (LOW), and 10 (HIGH), then the logic broken out is:

Code:
   00000000 (starting value)
OR 00000001 (pin 10 is HIGH, shifted 0 places left)
          ^
-----------
   00000001

   00000001
OR 00000000 (pin 11 is LOW, shifted 1 place left)
         ^
-----------
   00000001

   00000001
OR 00000100 (pin 12 is HIGH, shifted 2 places left)
        ^
-----------
   00000101

   00000101
OR 00001000 (pin 13 is HIGH, shifted 3 places left)
       ^
-----------
   00001101

   00001101
OR 00000000 (pin 14 is LOW, shifted 4 places left)
      ^
-----------
   00001101

Last edited by Ranma13; 10-19-2017 at 07:43 AM.
Ranma13 is offline   Reply With Quote
Old 10-19-2017, 10:28 AM   #19
FragBum
Member
 
Join Date: Jun 2016
Location: Rockhampton QLD, AUS.
Posts: 708
Reputation power: 3
FragBum has a spectacular aura aboutFragBum has a spectacular aura aboutFragBum has a spectacular aura about
Default

Quote:
Originally Posted by Ranma13 View Post
There's no need to AND the value with 0x1f because you already start with 0x00 on each loop, which already has the first 3 bits as 0, and the remaining bits are set by the loop. You need to read 1 bit at a time because you need to read the logic level on the pin. There's no way to treat a group of pins as the bits in a byte without reading in each wire individually and processing it.
Thanks nice explanation.

All I was thinking is reading the whole 8 bit port into a register hence a good idea to throw away the bits your not interested in, otherwise an unexpected result may occur besides it's like a clock cycle or three.

Then doing some bit wrangling using registers as variables then pass the byte back on the stack nothing fancy like your code.

And the X-OR I just picked that up by the C example.

Code:
from Wickipedia 
/*
 * This function converts a reflected binary
 * Gray code number to a binary number.
 * Each Gray code bit is exclusive-ored with all
 * more significant bits.
 */
unsigned int grayToBinary(unsigned int num)
...
__________________
Control is an illusion which usually shatters at the least expected moment.
FragBum is offline   Reply With Quote
Old 10-19-2017, 12:17 PM   #20
Trounce
Junior Member
 
Join Date: Dec 2014
Location: Japan and USA
Posts: 49
Reputation power: 3
Trounce is on a distinguished road
Default

Below is the SwitchMultiPos code for DCS-Bios. Can you fellows help me write a new switch class that can be used with the Gray code switches?


Code:
	class SwitchMultiPos : PollingInput {
		private:
			const char* msg_;
			const byte* pins_;
			char numberOfPins_;
			char lastState_;
			char readState() {
				unsigned char i;
				for (i=0; i<numberOfPins_; i++) {
					if (digitalRead(pins_[i]) == LOW) return i;
				}
				return lastState_;
			}
			void pollInput() {
				char state = readState();
				if (state != lastState_) {
					char buf[7];
					utoa(state, buf, 10);
					if (tryToSendDcsBiosMessage(msg_, buf))
						lastState_ = state;
				}
			}
		public:
			SwitchMultiPos(const char* msg, const byte* pins, char numberOfPins) : lastState_(0) {
				msg_ = msg;
				pins_ = pins;
				numberOfPins_ = numberOfPins;
				unsigned char i;
				for (i=0; i<numberOfPins; i++) {
					pinMode(pins[i], INPUT_PULLUP);
				}
				lastState_ = readState();
			}
	};
Trounce is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 03:48 AM. vBulletin Skin by ForumMonkeys. Powered by vBulletin®.
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.