-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinAcceptor.kt
84 lines (81 loc) · 3.24 KB
/
CoinAcceptor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package ticketmachine.software
import isel.leic.utils.Time
// Constant values
const val DELAY2: Long = 2000
// Implements the iterface with the Coin Acceptor hardware module
object CoinAcceptor {
// Constant values for the masks used in the inputport of UsbPort
// CID stands for Coin ID
private const val CID_MASK = 0b00011100 // inputPort(4, 3, 2) of UsbPort
private const val COIN_MASK = 0b00100000 // inputPort(5) of UsbPort
// Constant values for the masks used in the outputport of UsbPort
private const val COIN_EJECT_MASK = 0b00010000 // outputPort(4) of UsbPort
private const val COIN_COLLECT_MASK = 0b00100000 // outputPort(5) of UsbPort
private const val COIN_ACCEPT_MASK = 0b01000000 // outputPort(6) of UsbPort
// Constant values
private const val FIRST_CID = 0 // Index 0
private const val LAST_CID = 6 // Actual last index is 5 but until keyword is exclusive, so it was
// incremented to 6
// Lists
val coinCode_List: List<Int> = listOf(5, 10, 20, 50, 100, 200, 0, 0) // Coins in cents
// Initializes this class
fun init() {
// Set COIN_ACCEPT value with logical '0'
HAL.clrBits(COIN_ACCEPT_MASK)
// Set COIN_EJECT value with logical '0'
HAL.clrBits(COIN_EJECT_MASK)
// Set COIN_COLLECT value with logical '0'
HAL.clrBits(COIN_COLLECT_MASK)
}
// Returns true if a new coin was inserted
fun hasCoin(): Boolean {
return HAL.isBit(COIN_MASK)
}
// Returns the true value of the inserted coin
fun getCoinValue(): Int {
// Check if a coin was inserted
if (hasCoin()) {
// Create a mutable variable that will represent the true value of the inserted coin (value frame)
val value = HAL.readBits(CID_MASK).shr(2)
// Check if coin value is in the accepted coin range
if (value in FIRST_CID until LAST_CID) {
return coinCode_List[value]
}
}
// No coin was inserted
return 0
}
// Informs the CoinAcceptor that the coin was accounted for
fun acceptCoin() {
if (hasCoin()) {
// Set COIN_ACCEPT value with logical '1'
HAL.setBits(COIN_ACCEPT_MASK)
// Checks if coin switch was returned to its original position
while (hasCoin());
// Set COIN_ACCEPT value with logical '0'
HAL.clrBits(COIN_ACCEPT_MASK)
}
}
// Returns all coins currently stored in the CoinAcceptor
fun ejectCoins() {
// Set COIN_EJECT value with logical '1'
HAL.setBits(COIN_EJECT_MASK)
// Ejects inserted coins
CoinDeposit.ejectInsertedCoins()
// Active only for 2 seconds
Time.sleep(DELAY2)
// Set COIN_EJECT value with logical '0'
HAL.clrBits(COIN_EJECT_MASK)
}
// Collects all coins currently stored in the CoinAcceptor
fun collectCoins() {
// Set COIN_COLLECT value with logical '1'
HAL.setBits(COIN_COLLECT_MASK)
// Collects stored coins
CoinDeposit.collectStoredCoins()
// Active only for 2 seconds
Time.sleep(DELAY2)
// Set COIN_COLLECT value with logical '0'
HAL.clrBits(COIN_COLLECT_MASK)
}
}