Demo entry 6787094

201401589

   

Submitted by anonymous on Apr 09, 2019 at 06:18
Language: C. Code size: 3.0 kB.

#include <stdio.h>
#include <wiringPi.h>

#define LED0	14	
#define LED1	26
#define LED2	27
#define LED3	24
#define LED4	23
#define LED5	21
#define LED6	22

/*        
 * 	  @@0
 *	@5   @1
 *	@    @
 *	  @@6
 *	@4   @2
 *	@    @
 *	  @@3 
 */

char char_to_dec(char);

int main(int argc, char* argv[])
{
	int LED[7] = {LED0, LED1, LED2, LED3, LED4, LED5, LED6};
	
	char arg1;

	printf("Blinktest!\n");


	wiringPiSetup();
	pinMode(LED0, OUTPUT);
	pinMode(LED1, OUTPUT);
	pinMode(LED2, OUTPUT);
	pinMode(LED3, OUTPUT);
	pinMode(LED4, OUTPUT);
	pinMode(LED5, OUTPUT);
	pinMode(LED6, OUTPUT);

	if (argc != 2 && argc != 3)
	{
		printf("not enough argument\n");
		return 0;
	}

	if (argv[1][1] != '\0')
		printf("invaild argument1 : %s\n", argv[1]);
	else
	{
		arg1 = argv[1][0];
		if (arg1 == '1')
		{
			char code[16] = {63, 6, 91, 79 ,102, 109, 125, 39, 127, 111, 119, 124, 57, 94, 121, 71};
			int i;
			
			for (i = 0; i < 16; i++)
			{
				/*
				int j;
				char bit = 0x40;
				for (j = 0; j < 7; j++)
				{				
					printf("%d", (code[i] & bit) == bit);
					digitalWrite(LED[j], (code[i] & bit) == bit);
					bit = bit >> 1;
				}
				printf(" : %d", code[i]);
				printf("\n");
				delay(500);
				*/
				
				digitalWrite(LED0, (code[i] & 0x1) == 0x1);
				digitalWrite(LED1, (code[i] & 0x2) == 0x2);
				digitalWrite(LED2, (code[i] & 0x4) == 0x4);
				digitalWrite(LED3, (code[i] & 0x8) == 0x8);
				digitalWrite(LED4, (code[i] & 0x10) == 0x10);
				digitalWrite(LED5, (code[i] & 0x20) == 0x20);
				digitalWrite(LED6, (code[i] & 0x40) == 0x40);

				delay(500);
				
			}
		}
		else if (arg1 == '2')
		{
			if (argc == 2 || argv[2][0] != '0' || argv[2][1] != 'x' || char_to_dec(argv[2][2]) == 255 || char_to_dec(argv[2][3]) == 255)
				printf("argument2 must be hex : %s\n", argv[2]);
			else
			{
				int j;
				char code;
				char bit = 0x40;
				code = char_to_dec(argv[2][2]);
				if(argv[2][3] != '\0')
				{
					code = code << 4;
					code = code + char_to_dec(argv[2][3]);
				}

				
				for (j = 0; j < 7; j++)
				{				
					printf("%d", (code & bit) == bit);
					bit = bit >> 1;
				}
				printf(" : %d\n", code);
				
				digitalWrite(LED0, (code & 0x1) == 0x1);
				digitalWrite(LED1, (code & 0x2) == 0x2);
				digitalWrite(LED2, (code & 0x4) == 0x4);
				digitalWrite(LED3, (code & 0x8) == 0x8);
				digitalWrite(LED4, (code & 0x10) == 0x10);
				digitalWrite(LED5, (code & 0x20) == 0x20);
				digitalWrite(LED6, (code & 0x40) == 0x40);

				delay(5000);
			}
		}
		else
		{
			printf("unknown command : %s\n", argv[1]);
		}
	}

	
	digitalWrite(LED0, LOW);
	digitalWrite(LED1, LOW);
	digitalWrite(LED2, LOW);
	digitalWrite(LED3, LOW);
	digitalWrite(LED4, LOW);
	digitalWrite(LED5, LOW);
	digitalWrite(LED6, LOW);

	printf("off\n");
	return 0;
}

char char_to_dec(char c)
{
	if (c >= 48 && c <= 57)
		c = c - 48;
	else if (c >= 65 && c <= 70)
		c = c - 55;
	else if (c >= 97 && c <= 102)
		c = c - 87;
	else
		c = 255;

	return c;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).