fork download
  1. // Viral Science www.viralsciencecreativity.com www.youtube.com/c/viralscience
  2. // Blynk IOT Smart Plant Monitoring System
  3.  
  4. /* Connections
  5. Relay. D3
  6. Btn. D7
  7. Soil. A0
  8. PIR. D5
  9. SDA. D2
  10. SCL. D1
  11. Temp. D4
  12. */
  13.  
  14. //Include the library files
  15. #include <LiquidCrystal_I2C.h>
  16. #define BLYNK_PRINT Serial
  17. #include <ESP8266WiFi.h>
  18. #define ssid "IOT"
  19. #define password "12345678901"
  20. #include <BlynkSimpleEsp8266.h>
  21. void setup(){
  22. WiFi.mode (WIFI_STA);
  23. WiFi.begin(ssid,password);
  24. serial.begin(115200);
  25. while (WiFi.status() !=WL_CONNECTED){
  26. delay(500);
  27. Serial.print("");
  28. }
  29. Serial.println("");
  30. Serial.print("Connected to");
  31. Serial.print ln (ssid);
  32. Serial.print("IP Address:");
  33. Serial.println(WiFi.localIP());
  34. }
  35.  
  36. //Initialize the LCD display
  37. LiquidCrystal_I2C lcd(0x3F, 16, 2);
  38.  
  39.  
  40. char auth[] = "RxtqS7d1XxRaWVkewEPtlFMblUvhlc7H"; //Enter your Blynk Auth token
  41. char ssid[] = "IOT"; //Enter your WIFI SSID
  42. char pass[] = "12345678901"; //Enter your WIFI Password
  43.  
  44. //(DHT sensor pin,sensor type) D4 DHT11 Temperature Sensor
  45. BlynkTimer timer;
  46.  
  47. //Define component pins
  48. #define soil A0 //A0 Soil Moisture Sensor
  49. #define PIR D5 //D5 PIR Motion Sensor
  50. #define Raindropsensor D4
  51. int PIR_ToggleValue;
  52.  
  53. void checkPhysicalButton();
  54. int relay1State = LOW;
  55. int pushButton1State = HIGH;
  56. #define RELAY_PIN_1 D3 //D3 Relay
  57. #define PUSH_BUTTON_1 D7 //D7 Button
  58. #define VPIN_BUTTON_1 V12
  59.  
  60. //Create three variables for pressure
  61. double T, P;
  62. char status;
  63.  
  64.  
  65.  
  66. void setup() {
  67. Serial.begin(9600);
  68. lcd.begin(2,16);
  69. lcd.backlight();
  70. pinMode(PIR, INPUT);
  71.  
  72. pinMode(RELAY_PIN_1, OUTPUT);
  73. digitalWrite(RELAY_PIN_1, LOW);
  74. pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  75. digitalWrite(RELAY_PIN_1, relay1State);
  76.  
  77.  
  78. Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  79.  
  80.  
  81. lcd.setCursor(0, 0);
  82. lcd.print(" Initializing ");
  83. for (int a = 5; a <= 10; a++) {
  84. lcd.setCursor(a, 1);
  85. lcd.print(".");
  86. delay(500);
  87. }
  88. lcd.clear();
  89. lcd.setCursor(11, 1);
  90. lcd.print("W:OFF");
  91. //Call the function
  92. timer.setInterval(100L, soilMoistureSensor);
  93. timer.setInterval(500L, checkPhysicalButton);
  94. }
  95.  
  96.  
  97. //Get the DHT11 sensor values
  98.  
  99.  
  100.  
  101. //Get the soil moisture values
  102. void soilMoistureSensor() {
  103. int value = analogRead(soil);
  104. value = map(value, 0, 1024, 0, 100);
  105. value = (value - 100) * -1;
  106.  
  107. Blynk.virtualWrite(V3, value);
  108. lcd.setCursor(0, 1);
  109. lcd.print("S:");
  110. lcd.print(value);
  111. lcd.print(" ");
  112.  
  113. }
  114.  
  115. //Get the PIR sensor values
  116. void PIRsensor() {
  117. bool value = digitalRead(PIR);
  118. if (value) {
  119. Blynk.logEvent("PIRMotion","WARNNG! Motion Detected!"); //Enter your Event Name
  120. WidgetLED LED(V5);
  121. LED.on();
  122. } else {
  123. WidgetLED LED(V5);
  124. LED.off();
  125. }
  126. }
  127.  
  128. BLYNK_WRITE(V6)
  129. {
  130. PIR_ToggleValue = param.asInt();
  131. }
  132.  
  133.  
  134. BLYNK_CONNECTED() {
  135. // Request the latest state from the server
  136. Blynk.syncVirtual(VPIN_BUTTON_1);
  137. }
  138.  
  139. BLYNK_WRITE(VPIN_BUTTON_1) {
  140. relay1State = param.asInt();
  141. digitalWrite(RELAY_PIN_1, relay1State);
  142. }
  143.  
  144. void checkPhysicalButton()
  145. {
  146. if (digitalRead(PUSH_BUTTON_1) == LOW) {
  147. // pushButton1State is used to avoid sequential toggles
  148. if (pushButton1State != LOW) {
  149.  
  150. // Toggle Relay state
  151. relay1State = !relay1State;
  152. digitalWrite(RELAY_PIN_1, relay1State);
  153.  
  154. // Update Button Widget
  155. Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
  156. }
  157. pushButton1State = LOW;
  158. } else {
  159. pushButton1State = HIGH;
  160. }
  161. }
  162.  
  163.  
  164. void loop() {
  165. if (PIR_ToggleValue == 1)
  166. {
  167. lcd.setCursor(5, 1);
  168. lcd.print("M:ON ");
  169. PIRsensor();
  170. }
  171. else
  172. {
  173. lcd.setCursor(5, 1);
  174. lcd.print("M:OFF");
  175. WidgetLED LED(V5);
  176. LED.off();
  177. }
  178.  
  179. if (relay1State == HIGH)
  180. {
  181. lcd.setCursor(11, 1);
  182. lcd.print("W:ON ");
  183. }
  184. else if (relay1State == LOW)
  185. {
  186. lcd.setCursor(11, 1);
  187. lcd.print("W:OFF");
  188. }
  189.  
  190.  
  191. Blynk.run();//Run the Blynk library
  192. timer.run();//Run the Blynk timer
  193.  
  194. }
  195. /* package whatever; // don't place package name! */
  196. /*
  197.  
  198.   This source code accompanies the article, "Using The Golay Error Detection And
  199.   Correction Code", by Hank Wallace. This program demonstrates use of the Golay
  200.   code.
  201.   Usage: G DATA Encode/Correct/Verify/Test
  202.   where DATA is the data to be encoded, codeword to be corrected,
  203.   or codeword to be checked for errors. DATA is hexadecimal.
  204.   Examples:
  205.   G 555 E encodes information value 555 and prints a codeword
  206.   G ABC123 C corrects codeword ABC123
  207.   G ABC123 V checks codeword ABC123 for errors
  208.   G ABC123 T tests routines, ABC123 is a dummy parameter
  209.   This program may be freely incorporated into your programs as needed. It
  210.   compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
  211.   */
  212. #include "stdio.h"
  213. #include "conio.h"
  214. #define POLY 0xAE3 /* or use the other polynomial, 0xC75 */
  215.  
  216. /* ====================================================== */
  217.  
  218. unsigned long golay(unsigned long cw)
  219. /* This function calculates [23,12] Golay codewords.
  220.   The format of the returned longint is
  221.   [checkbits(11),data(12)]. */
  222. {
  223. int i;
  224. unsigned long c;
  225. cw&=0xfffl;
  226. c=cw; /* save original codeword */
  227. for (i=1; i<=12; i++) /* examine each data bit */
  228. {
  229. if (cw & 1) /* test data bit */
  230. cw^=POLY; /* XOR polynomial */
  231. cw>>=1; /* shift intermediate result */
  232. }
  233. return((cw<<12)|c); /* assemble codeword */
  234. }
  235.  
  236. /* ====================================================== */
  237.  
  238. int parity(unsigned long cw)
  239. /* This function checks the overall parity of codeword cw.
  240.   If parity is even, 0 is returned, else 1. */
  241. {
  242. unsigned char p;
  243.  
  244. /* XOR the bytes of the codeword */
  245. p=*(unsigned char*)&cw;
  246. p^=*((unsigned char*)&cw+1);
  247. p^=*((unsigned char*)&cw+2);
  248.  
  249. /* XOR the halves of the intermediate result */
  250. p=p ^ (p>>4);
  251. p=p ^ (p>>2);
  252. p=p ^ (p>>1);
  253.  
  254. /* return the parity result */
  255. return(p & 1);
  256. }
  257.  
  258. /* ====================================================== */
  259.  
  260. unsigned long syndrome(unsigned long cw)
  261. /* This function calculates and returns the syndrome
  262.   of a [23,12] Golay codeword. */
  263. {
  264. int i;
  265. cw&=0x7fffffl;
  266. for (i=1; i<=12; i++) /* examine each data bit */
  267. {
  268. if (cw & 1) /* test data bit */
  269. cw^=POLY; /* XOR polynomial */
  270. cw>>=1; /* shift intermediate result */
  271. }
  272. return(cw<<12); /* value pairs with upper bits of cw */
  273. }
  274.  
  275. /* ====================================================== */
  276.  
  277. int weight(unsigned long cw)
  278. /* This function calculates the weight of
  279.   23 bit codeword cw. */
  280. {
  281. int bits,k;
  282.  
  283. /* nibble weight table */
  284. const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
  285.  
  286. bits=0; /* bit counter */
  287. k=0;
  288. /* do all bits, six nibbles max */
  289. while ((k<6) && (cw))
  290. {
  291. bits=bits+wgt[cw & 0xf];
  292. cw>>=4;
  293. k++;
  294. }
  295.  
  296. return(bits);
  297. }
  298.  
  299. /* ====================================================== */
  300.  
  301. unsigned long rotate_left(unsigned long cw, int n)
  302. /* This function rotates 23 bit codeword cw left by n bits. */
  303. {
  304. int i;
  305.  
  306. if (n != 0)
  307. {
  308. for (i=1; i<=n; i++)
  309. {
  310. if ((cw & 0x400000l) != 0)
  311. cw=(cw << 1) | 1;
  312. else
  313. cw<<=1;
  314. }
  315. }
  316.  
  317. return(cw & 0x7fffffl);
  318. }
  319.  
  320. /* ====================================================== */
  321.  
  322. unsigned long rotate_right(unsigned long cw, int n)
  323. /* This function rotates 23 bit codeword cw right by n bits. */
  324. {
  325. int i;
  326.  
  327. if (n != 0)
  328. {
  329. for (i=1; i<=n; i++)
  330. {
  331. if ((cw & 1) != 0)
  332. cw=(cw >> 1) | 0x400000l;
  333. else
  334. cw>>=1;
  335. }
  336. }
  337.  
  338. return(cw & 0x7fffffl);
  339. }
  340.  
  341. /* ====================================================== */
  342.  
  343. unsigned long correct(unsigned long cw, int *errs)
  344. /* This function corrects Golay [23,12] codeword cw, returning the
  345.   corrected codeword. This function will produce the corrected codeword
  346.   for three or fewer errors. It will produce some other valid Golay
  347.   codeword for four or more errors, possibly not the intended
  348.   one. *errs is set to the number of bit errors corrected. */
  349. {
  350. unsigned char
  351. w; /* current syndrome limit weight, 2 or 3 */
  352. unsigned long
  353. mask; /* mask for bit flipping */
  354. int
  355. i,j; /* index */
  356. unsigned long
  357. s, /* calculated syndrome */
  358. cwsaver; /* saves initial value of cw */
  359.  
  360. cwsaver=cw; /* save */
  361. *errs=0;
  362. w=3; /* initial syndrome weight threshold */
  363. j=-1; /* -1 = no trial bit flipping on first pass */
  364. mask=1;
  365. while (j<23) /* flip each trial bit */
  366. {
  367. if (j != -1) /* toggle a trial bit */
  368. {
  369. if (j>0) /* restore last trial bit */
  370. {
  371. cw=cwsaver ^ mask;
  372. mask+=mask; /* point to next bit */
  373. }
  374. cw=cwsaver ^ mask; /* flip next trial bit */
  375. w=2; /* lower the threshold while bit diddling */
  376. }
  377.  
  378. s=syndrome(cw); /* look for errors */
  379. if (s) /* errors exist */
  380. {
  381. for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
  382. {
  383. if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
  384. {
  385. cw=cw ^ s; /* remove errors */
  386. cw=rotate_right(cw,i); /* unrotate data */
  387. return(s=cw);
  388. }
  389. else
  390. {
  391. cw=rotate_left(cw,1); /* rotate to next pattern */
  392. s=syndrome(cw); /* calc new syndrome */
  393. }
  394. }
  395. j++; /* toggle next trial bit */
  396. }
  397. else
  398. return(cw); /* return corrected codeword */
  399. }
  400.  
  401. return(cwsaver); /* return original if no corrections */
  402. } /* correct */
  403.  
  404. /* ====================================================== */
  405.  
  406. int decode(int correct_mode, int *errs, unsigned long *cw)
  407. /* This function decodes codeword *cw in one of two modes. If correct_mode
  408.   is nonzero, error correction is attempted, with *errs set to the number of
  409.   bits corrected, and returning 0 if no errors exist, or 1 if parity errors
  410.   exist. If correct_mode is zero, error detection is performed on *cw,
  411.   returning 0 if no errors exist, 1 if an overall parity error exists, and
  412.   2 if a codeword error exists. */
  413. {
  414. unsigned long parity_bit;
  415.  
  416. if (correct_mode) /* correct errors */
  417. {
  418. parity_bit=*cw & 0x800000l; /* save parity bit */
  419. *cw&=~0x800000l; /* remove parity bit for correction */
  420.  
  421. *cw=correct(*cw, errs); /* correct up to three bits */
  422. *cw|=parity_bit; /* restore parity bit */
  423.  
  424. /* check for 4 bit errors */
  425. if (parity(*cw)) /* odd parity is an error */
  426. return(1);
  427. return(0); /* no errors */
  428. }
  429. else /* detect errors only */
  430. {
  431. *errs=0;
  432. if (parity(*cw)) /* odd parity is an error */
  433. {
  434. *errs=1;
  435. return(1);
  436. }
  437. if (syndrome(*cw))
  438. {
  439. *errs=1;
  440. return(2);
  441. }
  442. else
  443. return(0); /* no errors */
  444. }
  445. } /* decode */
  446.  
  447. /* ====================================================== */
  448.  
  449. void golay_test(void)
  450. /* This function tests the Golay routines for detection and correction
  451.   of various patterns of error_limit bit errors. The error_mask cycles
  452.   over all possible values, and error_limit selects the maximum number
  453.   of induced errors. */
  454. {
  455. unsigned long
  456. error_mask, /* bitwise mask for inducing errors */
  457. trashed_codeword, /* the codeword for trial correction */
  458. virgin_codeword; /* the original codeword without errors */
  459. unsigned char
  460. pass=1, /* assume test passes */
  461. error_limit=3; /* select number of induced bit errors here */
  462. int
  463. error_count; /* receives number of errors corrected */
  464.  
  465. virgin_codeword=golay(0x555); /* make a test codeword */
  466. if (parity(virgin_codeword))
  467. virgin_codeword^=0x800000l;
  468. for (error_mask=0; error_mask<0x800000l; error_mask++)
  469. {
  470. /* filter the mask for the selected number of bit errors */
  471. if (weight(error_mask) <= error_limit) /* you can make this faster! */
  472. {
  473. trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
  474.  
  475. decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
  476.  
  477. if (trashed_codeword ^ virgin_codeword)
  478. {
  479. printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
  480. weight(error_mask),error_mask);
  481. pass=0;
  482. }
  483.  
  484. if (kbhit()) /* look for user input */
  485. {
  486. if (getch() == 27) return; /* escape exits */
  487.  
  488. /* other key prints status */
  489. printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
  490. }
  491. }
  492. }
  493. printf("Golay test %s!\n",pass?"PASSED":"FAILED");
  494. }
  495.  
  496. /* ====================================================== */
  497.  
  498. void main(int argument_count, char *argument[])
  499. {
  500. int i,j;
  501. unsigned long l,g;
  502. const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
  503. " where DATA is the data to be encoded, codeword to be corrected,\n"
  504. " or codeword to be checked for errors. DATA is hexadecimal.\n\n"
  505. "Examples:\n\n"
  506. " G 555 E encodes information value 555 and prints a codeword\n"
  507. " G ABC123 C corrects codeword ABC123\n"
  508. " G ABC123 V checks codeword ABC123 for errors\n"
  509. " G ABC123 T tests routines, ABC123 is a dummy parameter\n\n";
  510.  
  511. if (argument_count != 3)
  512. {
  513. printf(errmsg);
  514. exit(0);
  515. }
  516.  
  517. if (sscanf(argument[1],"%lx",&l) != 1)
  518. {
  519. printf(errmsg);
  520. exit(0);
  521. }
  522.  
  523. switch (toupper(*argument[2]))
  524. {
  525. case 'E': /* encode */
  526. l&=0xfff;
  527. l=golay(l);
  528. if (parity(l)) l^=0x800000l;
  529. printf("Codeword = %lX\n",l);
  530. break;
  531.  
  532. case 'V': /* verify */
  533. if (decode(0,&i,&l))
  534. printf("Codeword %lX is not a Golay codeword.\n",l);
  535. else
  536. printf("Codeword %lX is a Golay codeword.\n",l);
  537. break;
  538.  
  539. case 'C': /* correct */
  540. g=l; /* save initial codeword */
  541. j=decode(1,&i,&l);
  542. if ((j) && (i))
  543. printf("Codeword %lX had %d bits corrected,\n"
  544. "resulting in codeword %lX with a parity error.\n",g,i,l);
  545. else
  546. if ((j == 0) && (i))
  547. printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
  548. else
  549. if ((j) && (i == 0))
  550. printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
  551. else
  552. if ((j == 0) && (i == 0))
  553. printf("Codeword %lX does not require correction.\n",g);
  554. break;
  555.  
  556. case 'T': /* test */
  557. printf("Press SPACE for status, ESC to exit test...\n");
  558. golay_test();
  559. break;
  560.  
  561. default:
  562. printf(errmsg);
  563. exit(0);
  564. }
  565. }
  566.  
  567. /* end of G.C */
  568.  
  569.  
  570. import java.util.*;
  571. import java.lang.*;
  572. import java.io.*;
  573.  
  574. /* Name of the class has to be "Main" only if the class is public. */
  575. class Ideone
  576. {
  577. public static void main (String[] args) throws java.lang.Exception
  578. {
  579. // your code goes here
  580. }
  581. }
Success #stdin #stdout 0.04s 25868KB
stdin
Standard input is empty
stdout
// Viral Science www.viralsciencecreativity.com www.youtube.com/c/viralscience
// Blynk IOT Smart Plant Monitoring System

/* Connections
Relay. D3
Btn.   D7
Soil.  A0
PIR.   D5
SDA.   D2
SCL.   D1
Temp.  D4
*/

//Include the library files
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#define ssid "IOT"
#define password "12345678901"
#include <BlynkSimpleEsp8266.h>
void setup(){
  WiFi.mode (WIFI_STA);
  WiFi.begin(ssid,password);
  serial.begin(115200);
  while (WiFi.status() !=WL_CONNECTED){
    delay(500);
    Serial.print("");
  }
  Serial.println("");
  Serial.print("Connected to");
  Serial.print ln (ssid);
  Serial.print("IP Address:");
  Serial.println(WiFi.localIP());
}

//Initialize the LCD display
LiquidCrystal_I2C lcd(0x3F, 16, 2);


char auth[] = "RxtqS7d1XxRaWVkewEPtlFMblUvhlc7H";  //Enter your Blynk Auth token
char ssid[] = "IOT";  //Enter your WIFI SSID
char pass[] = "12345678901";  //Enter your WIFI Password

//(DHT sensor pin,sensor type)  D4 DHT11 Temperature Sensor
BlynkTimer timer;

//Define component pins
#define soil A0     //A0 Soil Moisture Sensor
#define PIR D5      //D5 PIR Motion Sensor
#define Raindropsensor D4
int PIR_ToggleValue;

void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
#define RELAY_PIN_1       D3   //D3 Relay
#define PUSH_BUTTON_1     D7   //D7 Button
#define VPIN_BUTTON_1    V12 

//Create three variables for pressure
double T, P;
char status;



void setup() {
  Serial.begin(9600);
  lcd.begin(2,16);
  lcd.backlight();
  pinMode(PIR, INPUT);

 pinMode(RELAY_PIN_1, OUTPUT);
 digitalWrite(RELAY_PIN_1, LOW);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);


  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);


  lcd.setCursor(0, 0);
  lcd.print("  Initializing  ");
  for (int a = 5; a <= 10; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(500);
  }
  lcd.clear();
  lcd.setCursor(11, 1);
  lcd.print("W:OFF");
  //Call the function
  timer.setInterval(100L, soilMoistureSensor);
  timer.setInterval(500L, checkPhysicalButton);
}


//Get the DHT11 sensor values



//Get the soil moisture values
void soilMoistureSensor() {
  int value = analogRead(soil);
  value = map(value, 0, 1024, 0, 100);
  value = (value - 100) * -1;

  Blynk.virtualWrite(V3, value);
  lcd.setCursor(0, 1);
  lcd.print("S:");
  lcd.print(value);
  lcd.print(" ");

}

//Get the PIR sensor values
void PIRsensor() {
  bool value = digitalRead(PIR);
  if (value) {
    Blynk.logEvent("PIRMotion","WARNNG! Motion Detected!"); //Enter your Event Name
    WidgetLED LED(V5);
    LED.on();
  } else {
    WidgetLED LED(V5);
    LED.off();
  }  
  }

BLYNK_WRITE(V6)
{
 PIR_ToggleValue = param.asInt();  
}


BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}

void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}


void loop() {
    if (PIR_ToggleValue == 1)
    {
    lcd.setCursor(5, 1);
    lcd.print("M:ON ");
      PIRsensor();
      }
     else
     {
    lcd.setCursor(5, 1);
    lcd.print("M:OFF");
    WidgetLED LED(V5);
    LED.off();
     }

if (relay1State == HIGH)
{
  lcd.setCursor(11, 1);
  lcd.print("W:ON ");
  }
  else if (relay1State == LOW)
  {
    lcd.setCursor(11, 1);
    lcd.print("W:OFF");
    }
     
      
  Blynk.run();//Run the Blynk library
  timer.run();//Run the Blynk timer

  }
/* package whatever; // don't place package name! */
/*
    
    This source code accompanies the article, "Using The Golay Error Detection And
    Correction Code", by Hank Wallace. This program demonstrates use of the Golay
    code.
      Usage: G DATA Encode/Correct/Verify/Test
        where DATA is the data to be encoded, codeword to be corrected,
        or codeword to be checked for errors. DATA is hexadecimal.
      Examples:
        G 555 E      encodes information value 555 and prints a codeword
        G ABC123 C   corrects codeword ABC123
        G ABC123 V   checks codeword ABC123 for errors
        G ABC123 T   tests routines, ABC123 is a dummy parameter
    This program may be freely incorporated into your programs as needed. It
    compiles under Borland's Turbo C 2.0. No warranty of any kind is granted.
    */
    #include "stdio.h"
    #include "conio.h"
    #define POLY  0xAE3  /* or use the other polynomial, 0xC75 */
    
    /* ====================================================== */
    
    unsigned long golay(unsigned long cw)
    /* This function calculates [23,12] Golay codewords.
      The format of the returned longint is
      [checkbits(11),data(12)]. */
    {
      int i;
      unsigned long c;
      cw&=0xfffl;
      c=cw; /* save original codeword */
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return((cw<<12)|c);    /* assemble codeword */
    }
    
    /* ====================================================== */
    
    int parity(unsigned long cw)
    /* This function checks the overall parity of codeword cw.
      If parity is even, 0 is returned, else 1. */
    {
      unsigned char p;
    
      /* XOR the bytes of the codeword */
      p=*(unsigned char*)&cw;
      p^=*((unsigned char*)&cw+1);
      p^=*((unsigned char*)&cw+2);
    
      /* XOR the halves of the intermediate result */
      p=p ^ (p>>4);
      p=p ^ (p>>2);
      p=p ^ (p>>1);
    
      /* return the parity result */
      return(p & 1);
    }
    
    /* ====================================================== */
    
    unsigned long syndrome(unsigned long cw)
    /* This function calculates and returns the syndrome
      of a [23,12] Golay codeword. */
    {
      int i;
      cw&=0x7fffffl;
      for (i=1; i<=12; i++)  /* examine each data bit */
        {
          if (cw & 1)        /* test data bit */
            cw^=POLY;        /* XOR polynomial */
          cw>>=1;            /* shift intermediate result */
        }
      return(cw<<12);        /* value pairs with upper bits of cw */
    }
    
    /* ====================================================== */
    
    int weight(unsigned long cw)
    /* This function calculates the weight of
      23 bit codeword cw. */
    {
      int bits,k;
    
      /* nibble weight table */
      const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    
      bits=0; /* bit counter */
      k=0;
      /* do all bits, six nibbles max */
      while ((k<6) && (cw))
        {
          bits=bits+wgt[cw & 0xf];
          cw>>=4;
          k++;
        }
    
      return(bits);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_left(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw left by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 0x400000l) != 0)
                cw=(cw << 1) | 1;
              else
                cw<<=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long rotate_right(unsigned long cw, int n)
    /* This function rotates 23 bit codeword cw right by n bits. */
    {
      int i;
    
      if (n != 0)
        {
          for (i=1; i<=n; i++)
            {
              if ((cw & 1) != 0)
                cw=(cw >> 1) | 0x400000l;
              else
                cw>>=1;
            }
        }
    
      return(cw & 0x7fffffl);
    }
    
    /* ====================================================== */
    
    unsigned long correct(unsigned long cw, int *errs)
    /* This function corrects Golay [23,12] codeword cw, returning the
      corrected codeword. This function will produce the corrected codeword
      for three or fewer errors. It will produce some other valid Golay
      codeword for four or more errors, possibly not the intended
      one. *errs is set to the number of bit errors corrected. */
    {
      unsigned char
        w;                /* current syndrome limit weight, 2 or 3 */
      unsigned long
        mask;             /* mask for bit flipping */
      int
        i,j;              /* index */
      unsigned long
        s,                /* calculated syndrome */
        cwsaver;          /* saves initial value of cw */
    
      cwsaver=cw;         /* save */
      *errs=0;
      w=3;                /* initial syndrome weight threshold */
      j=-1;               /* -1 = no trial bit flipping on first pass */
      mask=1;
      while (j<23) /* flip each trial bit */
        {
          if (j != -1) /* toggle a trial bit */
            {
              if (j>0) /* restore last trial bit */
                {
                  cw=cwsaver ^ mask;
                  mask+=mask; /* point to next bit */
                }
              cw=cwsaver ^ mask; /* flip next trial bit */
              w=2; /* lower the threshold while bit diddling */
            }
    
          s=syndrome(cw); /* look for errors */
          if (s) /* errors exist */
            {
              for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
                {
                  if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
                    {
                      cw=cw ^ s;              /* remove errors */
                      cw=rotate_right(cw,i);  /* unrotate data */
                      return(s=cw);
                    }
                  else
                    {
                      cw=rotate_left(cw,1);   /* rotate to next pattern */
                      s=syndrome(cw);         /* calc new syndrome */
                    }
                }
              j++; /* toggle next trial bit */
            }
          else
            return(cw); /* return corrected codeword */
        }
    
      return(cwsaver); /* return original if no corrections */
    } /* correct */
    
    /* ====================================================== */
    
    int decode(int correct_mode, int *errs, unsigned long *cw)
    /* This function decodes codeword *cw in one of two modes. If correct_mode
      is nonzero, error correction is attempted, with *errs set to the number of
      bits corrected, and returning 0 if no errors exist, or 1 if parity errors
      exist. If correct_mode is zero, error detection is performed on *cw,
      returning 0 if no errors exist, 1 if an overall parity error exists, and
      2 if a codeword error exists. */
    {
      unsigned long parity_bit;
    
      if (correct_mode)               /* correct errors */
        {
          parity_bit=*cw & 0x800000l; /* save parity bit */
          *cw&=~0x800000l;            /* remove parity bit for correction */
    
          *cw=correct(*cw, errs);     /* correct up to three bits */
          *cw|=parity_bit;            /* restore parity bit */
    
          /* check for 4 bit errors */
          if (parity(*cw))            /* odd parity is an error */
            return(1);
          return(0); /* no errors */
        }
      else /* detect errors only */
        {
          *errs=0;
          if (parity(*cw)) /* odd parity is an error */
            {
              *errs=1;
              return(1);
            }
          if (syndrome(*cw))
            {
              *errs=1;
              return(2);
            }
          else
            return(0); /* no errors */
        }
    } /* decode */
    
    /* ====================================================== */
    
    void golay_test(void)
    /* This function tests the Golay routines for detection and correction
      of various patterns of error_limit bit errors. The error_mask cycles
      over all possible values, and error_limit selects the maximum number
      of induced errors. */
    {
      unsigned long
        error_mask,         /* bitwise mask for inducing errors */
        trashed_codeword,   /* the codeword for trial correction */
        virgin_codeword;    /* the original codeword without errors */
      unsigned char
        pass=1,             /* assume test passes */
        error_limit=3;      /* select number of induced bit errors here */
      int
        error_count;        /* receives number of errors corrected */
    
      virgin_codeword=golay(0x555); /* make a test codeword */
      if (parity(virgin_codeword))
        virgin_codeword^=0x800000l;
      for (error_mask=0; error_mask<0x800000l; error_mask++)
        {
          /* filter the mask for the selected number of bit errors */
          if (weight(error_mask) <= error_limit) /* you can make this faster! */
            {
              trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
    
              decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
    
              if (trashed_codeword ^ virgin_codeword)
                {
                  printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
                    weight(error_mask),error_mask);
                  pass=0;
                }
    
              if (kbhit()) /* look for user input */
                {
                  if (getch() == 27) return; /* escape exits */
    
                  /* other key prints status */
                  printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
                }
            }
        }
      printf("Golay test %s!\n",pass?"PASSED":"FAILED");
    }
    
    /* ====================================================== */
    
    void main(int argument_count, char *argument[])
    {
      int i,j;
      unsigned long l,g;
      const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
                 "  where DATA is the data to be encoded, codeword to be corrected,\n"
                 "  or codeword to be checked for errors. DATA is hexadecimal.\n\n"
                 "Examples:\n\n"
                 "  G 555 E      encodes information value 555 and prints a codeword\n"
                 "  G ABC123 C   corrects codeword ABC123\n"
                 "  G ABC123 V   checks codeword ABC123 for errors\n"
                 "  G ABC123 T   tests routines, ABC123 is a dummy parameter\n\n";
    
      if (argument_count != 3)
        {
          printf(errmsg);
          exit(0);
        }
    
      if (sscanf(argument[1],"%lx",&l) != 1)
        {
          printf(errmsg);
          exit(0);
        }
    
      switch (toupper(*argument[2]))
        {
          case 'E': /* encode */
            l&=0xfff;
            l=golay(l);
            if (parity(l)) l^=0x800000l;
            printf("Codeword = %lX\n",l);
            break;
    
          case 'V': /* verify */
            if (decode(0,&i,&l))
              printf("Codeword %lX is not a Golay codeword.\n",l);
            else
              printf("Codeword %lX is a Golay codeword.\n",l);
            break;
    
          case 'C': /* correct */
            g=l; /* save initial codeword */
            j=decode(1,&i,&l);
            if ((j) && (i))
              printf("Codeword %lX had %d bits corrected,\n"
                     "resulting in codeword %lX with a parity error.\n",g,i,l);
            else
              if ((j == 0) && (i))
                printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
              else
                if ((j) && (i == 0))
                  printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
                else
                  if ((j == 0) && (i == 0))
                    printf("Codeword %lX does not require correction.\n",g);
            break;
    
          case 'T': /* test */
            printf("Press SPACE for status, ESC to exit test...\n");
            golay_test();
            break;
    
          default:
            printf(errmsg);
            exit(0);
        }
    }
    
    /* end of G.C */


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	}
}