Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1765 | linlun | 1 | |
| 2 | softPWM_ModuleNames = [ "softPWM" ]; |
||
| 3 | softPWM_Intervals = function() { return [ 1, 5, 10, 15, 20 ]; }; |
||
| 4 | softPWM_PWMLevels = function() { return [ 0, 25, 50, 75, 100 ]; }; |
||
| 5 | softPWM_PWMPeriod = function() { return [ 0, 128, 255, 5000, 10000, 32768, 65535 ]; }; |
||
| 6 | softPWM_PWMResolution = function() { return [ 0, 10, 100, 128, 255 ]; }; |
||
| 7 | //softPWM_StrengthLevels = function() { return [ 0, 50, 100, 150, 200, 255 ]; }; |
||
| 8 | //softPWM_StepsLevels = function() { return [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ]; }; |
||
| 9 | //softPWM_Directions = function() { return [ "Increase", "Decrease" ]; }; |
||
| 10 | //softPWM_Channels = function() { return [ 0 ]; }; |
||
| 11 | softPWM_Aliases = function() { return Module_GetAliasNames(softPWM_ModuleNames); }; |
||
| 12 | softPWM_AvailableIds = function() { return Module_GetAvailableIds(softPWM_ModuleNames); }; |
||
| 13 | |||
| 14 | function softPWM_Configure(alias_name, pwmPeriod, pwmResolution) |
||
| 15 | { |
||
| 16 | if (arguments.length < 3) |
||
| 17 | { |
||
| 18 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 19 | return false; |
||
| 20 | } |
||
| 21 | |||
| 22 | var aliases_data = Module_ResolveAlias(alias_name, softPWM_ModuleNames); |
||
| 23 | var found = false; |
||
| 24 | |||
| 25 | for (var name in aliases_data) |
||
| 26 | { |
||
| 27 | var variables = { |
||
| 28 | "Period" : pwmPeriod, |
||
| 29 | "Resolution" : pwmResolution }; |
||
| 30 | |||
| 31 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "CONFIG", variables)) |
||
| 32 | { |
||
| 33 | //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 34 | } |
||
| 35 | else |
||
| 36 | { |
||
| 37 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 38 | } |
||
| 39 | |||
| 40 | found = true; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!found) |
||
| 44 | { |
||
| 45 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | |||
| 49 | return true; |
||
| 50 | } |
||
| 51 | Console_RegisterCommand(softPWM_Configure, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, softPWM_Aliases(), softPWM_PWMPeriod(), softPWM_PWMResolution()); }); |
||
| 52 | |||
| 53 | function softPWM_SetPWMValue(alias_name, pwmvalue) |
||
| 54 | { |
||
| 55 | if (arguments.length < 2) |
||
| 56 | { |
||
| 57 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | var aliases_data = Module_ResolveAlias(alias_name, softPWM_ModuleNames); |
||
| 62 | var found = false; |
||
| 63 | |||
| 64 | for (var name in aliases_data) |
||
| 65 | { |
||
| 66 | |||
| 67 | var value = parseInt(pwmvalue*100).toString(); |
||
| 68 | var variables = { |
||
| 69 | "Id" : aliases_data[name]["specific"]["Channel"], |
||
| 70 | "Value" : value }; |
||
| 71 | |||
| 72 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Pwm", variables)) |
||
| 73 | { |
||
| 74 | //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 75 | } |
||
| 76 | else |
||
| 77 | { |
||
| 78 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 79 | } |
||
| 80 | |||
| 81 | found = true; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!found) |
||
| 85 | { |
||
| 86 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | return true; |
||
| 91 | } |
||
| 92 | Console_RegisterCommand(softPWM_SetPWMValue, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, softPWM_Aliases(), softPWM_PWMLevels()); }); |
||
| 93 | |||
| 94 | function softPWM_SetReportInterval(alias_name, interval) |
||
| 95 | { |
||
| 96 | if (arguments.length < 2) |
||
| 97 | { |
||
| 98 | Log("\033[31mNot enough parameters given.\033[0m\n"); |
||
| 99 | return false; |
||
| 100 | } |
||
| 101 | |||
| 102 | var aliases_data = Module_ResolveAlias(alias_name, Power_ModuleNames); |
||
| 103 | var found = false; |
||
| 104 | |||
| 105 | for (var name in aliases_data) |
||
| 106 | { |
||
| 107 | var variables = {"Time" : interval }; |
||
| 108 | |||
| 109 | if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Report_Interval", variables)) |
||
| 110 | { |
||
| 111 | //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n"); |
||
| 112 | } |
||
| 113 | else |
||
| 114 | { |
||
| 115 | Log("\033[31mFailed to send command to " + name + ".\033[0m\n"); |
||
| 116 | } |
||
| 117 | |||
| 118 | found = true; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!found) |
||
| 122 | { |
||
| 123 | Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n"); |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | Console_RegisterCommand(softPWM_SetReportInterval, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, softPWM_Aliases(), softPWM_Intervals()); }); |
||
| 130 | |||
| 131 | |||
| 132 | function softPWM_OnMessage(module_name, module_id, command, variables) |
||
| 133 | { |
||
| 134 | if (in_array(softPWM_ModuleNames, module_name)) |
||
| 135 | { |
||
| 136 | var aliases_data = Module_LookupAliases({ |
||
| 137 | "module_name" : module_name, |
||
| 138 | "module_id" : module_id, |
||
| 139 | "group" : false |
||
| 140 | }); |
||
| 141 | |||
| 142 | switch (command) |
||
| 143 | { |
||
| 144 | case "CONFIG": |
||
| 145 | { |
||
| 146 | for (var alias_name in aliases_data) |
||
| 147 | { |
||
| 148 | var last_value = {}; |
||
| 149 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 150 | |||
| 151 | if (last_value_string) |
||
| 152 | { |
||
| 153 | last_value = eval("(" + last_value_string + ")"); |
||
| 154 | } |
||
| 155 | last_value["Period"] = { "value" : variables["Period"], "timestamp" : get_time() }; |
||
| 156 | last_value["Resolution"] = { "value" : variables["Resolution"], "timestamp" : get_time() }; |
||
| 157 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 158 | } |
||
| 159 | break; |
||
| 160 | } |
||
| 161 | case "Pwm": |
||
| 162 | { |
||
| 163 | for (var alias_name in aliases_data) |
||
| 164 | { |
||
| 165 | if (aliases_data[alias_name]["specific"]["Channel"] != variables["Id"]) |
||
| 166 | { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | var last_value = {}; |
||
| 171 | var last_value_string = Storage_GetParameter("LastValues", alias_name); |
||
| 172 | |||
| 173 | if (last_value_string) |
||
| 174 | { |
||
| 175 | last_value = eval("(" + last_value_string + ")"); |
||
| 176 | } |
||
| 177 | var value = 0 + variables["Value"]; |
||
| 178 | value = value / 100; |
||
| 179 | value = parseFloat(value).toString(); |
||
| 180 | last_value["Level"] = { "value" : value , "timestamp" : get_time() }; |
||
| 181 | Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value)); |
||
| 182 | } |
||
| 183 | |||
| 184 | break; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | Module_RegisterToOnMessage("all", softPWM_OnMessage); |