Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #include <stdint.h>
  5.  
  6. #include <IRremote.h>
  7. #include <LiquidCrystal.h>
  8.  
  9. #define TIMEOUT 120
  10.  
  11. LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
  12. IRrecv irrecv(2);
  13. decode_results irData;
  14.  
  15. //WDT timer
  16. volatile uint8_t frontWDT; //Fron-end wdt. This will reset current transcation, system WDT will reset the entire program.
  17.  
  18. //Input buffer
  19. volatile uint32_t inputBuffer; //User input buffer
  20. volatile uint8_t lastInput;
  21. volatile uint8_t lastValidInput; //In case of IR error
  22. volatile boolean bufferWriteComplete;
  23. void resetBuffer() {
  24. inputBuffer = 0;
  25. lastInput = 0x00;
  26. bufferWriteComplete = false;
  27. }
  28.  
  29. //User db
  30. volatile uint32_t userBalance[32];
  31. volatile uint32_t userPass[32];
  32.  
  33. void setup(){
  34. //Init begin
  35. cli();
  36.  
  37. //Debug interface
  38. Serial.begin(9600);
  39.  
  40. //Init IR
  41. irrecv.enableIRIn();
  42. irrecv.blink13(true);
  43.  
  44. //LCD = 1602
  45. lcd.begin(16, 2);
  46.  
  47. //Init timer: CTC, 1s
  48. TCCR1A = 0;
  49. TCCR1B = 0;
  50. OCR1A = 15624; //Compare = 1s
  51. TCCR1B |= (1 << WGM12);
  52. TCCR1B |= (1 << CS12)|(1 << CS10); //Scale = 1024
  53. TIMSK1 |= (1 << OCIE1A);
  54.  
  55. //Create IR interrupt
  56. attachInterrupt(digitalPinToInterrupt(2),ISR_IRRxc,CHANGE);
  57.  
  58. //Load user info
  59. for (uint8_t i = 0; i < 32; i++) {
  60. userPass[i] = i;
  61. userBalance[i] = 100;
  62. }
  63.  
  64. //Init down, enable interrupt
  65. sei();
  66. }
  67.  
  68. ISR(TIMER1_COMPA_vect){ //Front-end WDT ++
  69. frontWDT++;
  70. // Serial.println(frontWDT);
  71. }
  72.  
  73. void ISR_IRRxc() {
  74. if(irrecv.decode(&irData)) {
  75. switch (irData.value) {
  76. case 0xFFA25D: lastInput = 0x0A; break; //Press POWER
  77. case 0xFF6897: lastInput = 0x00; break; //Press digit 0 - 9
  78. case 0xFF30CF: lastInput = 0x01; break;
  79. case 0xFF18E7: lastInput = 0x02; break;
  80. case 0xFF7A85: lastInput = 0x03; break;
  81. case 0xFF10EF: lastInput = 0x04; break;
  82. case 0xFF38C7: lastInput = 0x05; break;
  83. case 0xFF5AA5: lastInput = 0x06; break;
  84. case 0xFF42BD: lastInput = 0x07; break;
  85. case 0xFF4AB5: lastInput = 0x08; break;
  86. case 0xFF52AD: lastInput = 0x09; break;
  87. default: lastInput = 0xFF; break; //Unknown key
  88. }
  89.  
  90. if (lastInput < 0x0A) { //Input digit, concat to buffer
  91. inputBuffer = inputBuffer * 10 + lastInput;
  92. lastValidInput = lastInput;
  93. }
  94. else if (lastInput == 0x0A) //Input POWER (ENTER), fire signal
  95. bufferWriteComplete = true;
  96.  
  97. Serial.println("IR Rxc interrupt: buffer = "+String(inputBuffer)+" New = "+String(lastInput));
  98. delay(250);
  99. irrecv.resume();
  100. }
  101. }
  102.  
  103. void writeLCD(String line0, String line1) {
  104. lcd.clear();
  105. lcd.setCursor(0, 0);
  106. lcd.print(line0);
  107. lcd.setCursor(0, 1);
  108. lcd.print(line1);
  109. }
  110.  
  111. void loop() {
  112. /* S_Reset */
  113. Serial.println("[State] RESET");
  114. writeLCD("RESET...","");
  115. delay(500);
  116.  
  117. /* S_Hello */
  118. Serial.println("[State] HELLO");
  119. writeLCD("HELLO","PRESS POWER");
  120.  
  121. frontWDT = 0;
  122. resetBuffer();
  123. while(frontWDT < TIMEOUT && !bufferWriteComplete) ; //Wait until user input finish or timeout
  124. if (frontWDT >= TIMEOUT) //Timeout, end current transcation
  125. return;
  126.  
  127. /* S_Acc */
  128. Serial.println("[State] Account");
  129. writeLCD("Account:","");
  130.  
  131. frontWDT = 0;
  132. resetBuffer();
  133. while(frontWDT < TIMEOUT && !bufferWriteComplete) {
  134. writeLCD("Account:",String(inputBuffer));
  135. delay(100);
  136. }
  137. if (frontWDT >= TIMEOUT) //Timeout, end current transcation
  138. return;
  139.  
  140. /* S_CheckAcc */
  141. Serial.println("[State] Check account");
  142. if (inputBuffer >= 32) { //There is only 32 users
  143. Serial.println("Invalid user account");
  144. writeLCD("Invalid account","");
  145. delay(4000);
  146. return;
  147. }
  148.  
  149. uint8_t userid = 0x1F & inputBuffer; //Using 0001 1111 to mask 6-bit userid space
  150. char username = userid + 0x41; //id 0 = A, 1 = B, and so on...
  151. Serial.println("User id: "+String(userid));
  152. Serial.println("User name: "+String(username));
  153.  
  154. /* S_Pass */
  155. for (uint8_t i = 0; i < 255; i++) {
  156. Serial.println("[State] Password");
  157. Serial.println("Try = "+String(i));
  158. writeLCD("Password:","");
  159.  
  160. frontWDT = 0;
  161. resetBuffer();
  162. while(frontWDT < TIMEOUT && !bufferWriteComplete) {
  163. writeLCD("Password:",String(inputBuffer));
  164. delay(100);
  165. }
  166. if (frontWDT >= TIMEOUT) //Timeout, end current transcation
  167. return;
  168.  
  169. /* S_CheckPass */
  170. Serial.println("[State] Check password");
  171. if (inputBuffer == userPass[userid]) { //Compare pass with user input
  172. Serial.println("Password OK");
  173. writeLCD("Password OK","");
  174. delay(2000);
  175. break; //Break the loop
  176. }
  177. else {
  178. Serial.println("Bad Password");
  179. writeLCD("Bad Password","");
  180. delay(2000);
  181. if (i >= 3) return; //Failed for more than 3 times, end current transcation
  182. }
  183. }
  184.  
  185. /* S_Mode */
  186. Serial.println("[State] Mode");
  187. writeLCD("$ "+String(userBalance[userid]),"1.Dep 2.Wtd");
  188.  
  189. boolean dw;
  190. frontWDT = 0;
  191. resetBuffer();
  192. while(frontWDT < TIMEOUT && lastInput != 0x01 && lastInput != 0x02 ) { //Wait until timeout or press 1 or 2
  193. }
  194. if (frontWDT >= TIMEOUT) //Timeout, end current transcation
  195. return;
  196.  
  197. dw = (lastValidInput == 0x01);
  198.  
  199. /* S_Amount */
  200. Serial.println("[State] Amount");
  201. String dwWord = dw ? "Deposit" : "Withdrawal";
  202. Serial.println("Mode: "+dwWord);
  203. writeLCD(dwWord+":","$ ");
  204.  
  205. frontWDT = 0;
  206. resetBuffer();
  207. while(frontWDT < TIMEOUT && !bufferWriteComplete) {
  208. writeLCD(dwWord+":","$ "+String(inputBuffer));
  209. delay(100);
  210. }
  211. if (frontWDT >= TIMEOUT) //Timeout, end current transcation
  212. return;
  213.  
  214. /* S_CheckAmount */
  215. Serial.println("[State] Check amount");
  216. Serial.println("Before transcation: "+String(userBalance[userid]));
  217. Serial.println("Transcation amount: "+String(inputBuffer));
  218. if (dw) {
  219. userBalance[userid] += inputBuffer; //User should not have that much money which could cause bank overflow, uint32_t = 2^32 = $4G
  220. }
  221. else {
  222. if (userBalance[userid] < inputBuffer) {
  223. Serial.println("Insufficient funds");
  224. writeLCD("Insufficient"," funds :(");
  225. delay(2000);
  226. }
  227. else {
  228. userBalance[userid] -= inputBuffer;
  229. }
  230. }
  231.  
  232. /* S_Display */
  233. Serial.println("[State] Display");
  234. Serial.println("After transcation: "+String(userBalance[userid]));
  235. writeLCD(String(username),"$ "+String(userBalance[userid]));
  236. delay(6000);
  237.  
  238. writeLCD("Thank you","Merci");
  239. delay(3000);
  240.  
  241. return;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement